datetime to timestamp javascript

Viewed 6364

i have this time format :

DateTime(2015, 5, 11, 12, 0, 0)

i would like to know if i can convert it into a time stamp.

i have made this convert function from ISO 8601 to Timestamp and i would like to know if i can adapt it to this time format :

var myDate = new Date("2017-07-31T15:30:00+0000");
var offset = myDate.getTimezoneOffset() * 60 * 1000;

var withOffset = myDate.getTime();
var withoutOffset = withOffset - offset;

console.log(myDate.getTimezoneOffset()*60 * 1000)
console.log('with Offset  ' + withOffset);
console.log('without Offset (timeStamp of your timezone) ' +withoutOffset);

3 Answers

Parsing dates is a pain in JavaScript as there's no extensive native support. However you could do something like the following by relying on the Date(year, month, day [, hour, minute, second, millisecond]) constructor signature of the Date object.

var dateString = '17-09-2013 10:08',
dateTimeParts = dateString.split(' '),
timeParts = dateTimeParts[1].split(':'),
dateParts = dateTimeParts[0].split('-'),
date;

date = new Date(dateParts[2], parseInt(dateParts[1], 10) - 1, dateParts[0], timeParts[0], timeParts[1]);

console.log(date.getTime()); //1379426880000
console.log(date); //Tue Sep 17 2013 10:08:00 GMT-0400
Related