I need convert string "Nov 1 2018 11:07AM" to date, before compare with Date.Now and extract to minutes difference, can you help me?
2 Answers
For date manipulation go for moments.js library (https://momentjs.com/).
A basic example of converting you date in YYYY-MM-DD format.
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>
Comments
Solution :
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
}