0

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
  • Will that date always be in that format? Commented Nov 2, 2018 at 16:55
  • Yes, this value always this format. Commented Nov 2, 2018 at 16:57

2 Answers 2

5

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>

Sign up to request clarification or add additional context in comments.

Comments

0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.