我需要转换字符串“2018年11月1日上午11点07分”,在与Date.Now比较之前,提取到分钟差,你能帮我吗?
发布于 2018-11-02 17:56:44
解决办法:
var strDate = 'Nov 5 2018 12:55AM'
var arrDate = strDate.split(' ')
arrDate[arrDate.length -1] = convert(arrDate[arrDate.length -1])
var newstrDate = arrDate.join(' ')
var date = new Date(newstrDate)
var today = new Date()
var diffMs = date - today // milliseconds between now and the custom date
var diffMins = Math.round((diffMs / 1000) / 60); // minutes until the custom date
console.log(diffMins)
function convert(hours) {
var type = hours.indexOf('PM') == -1 ? 'AM' : 'PM'
hours = hours.replace(type,'')
var dictionary = {
01: '13',
02: '14',
03: '15',
04: '16',
05: '17',
06: '18',
07: '19',
08: '20',
09: '21',
10: '22',
11: '23',
12: '24'
}
if(type == 'PM'){
var arrHours = hours.split(':')
arrHours[0] = dictionary[arrHours[0]]
hours = arrHours.join(':')
}
return hours
}
发布于 2018-11-02 16:57:05
对于日期操作,请使用moments.js
库(https://momentjs.com/)。
以YYYY-MM-DD
格式转换日期的基本示例。
var m = moment('Nov 1 2018 11:07AM',"MMM DD YYYY hh:mmA");
console.log(m.format('YYYY-MM-DD')); // 2018-11-01
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
https://stackoverflow.com/questions/53122787
复制相似问题