不得不感叹一下,聪明的程序员写的代码真是让人惊奇
找了一圈格式化代码的方式,下面的这个使用了一个 slice 函数,真是厉害
https://stackoverflow.com/questions/42358990/format-date-received-in-milliseconds-in-yyyy-mm-dd-hh-mm-in-jquery-javascript
不过里面的一行代码解决,但没有完全格式化为 yyyyMMddHHmmss 的格式自己做了一些修改:
//获取明天指定小时时间 function getTomorrowtimeFormat (hour){ var mydate = new Date(); mydate.setHours(hour); mydate.setDate(mydate.getDate()+1); var year = mydate.getFullYear(); var month = mydate.getMonth()+1; var day = mydate.getDate(); var hour = mydate.getHours(); var minutes = mydate.getMinutes(); var seconds = mydate.getSeconds(); var timestr = year + '' + ('0' + month).slice(-2)+ '' + ('0' + day).slice(-2)+ '' + ('0' + hour).slice(-2)+ '' + ('0' + minutes).slice(-2)+ ''+ ('0' + seconds).slice(-2); return timestr; } //获取今天指定小时时间 function getTodaytimeFormat (hour){ var mydate = new Date(); mydate.setHours(hour); var year = mydate.getFullYear(); var month = mydate.getMonth()+1; var day = mydate.getDate(); var hour = mydate.getHours(); var minutes = mydate.getMinutes(); var seconds = mydate.getSeconds(); var timestr = year + '' + ('0' + month).slice(-2)+ '' + ('0' + day).slice(-2)+ '' + ('0' + hour).slice(-2)+ '' + ('0' + minutes).slice(-2)+ ''+ ('0' + seconds).slice(-2); return timestr; }