3

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);

2

3 Answers 3

10

did you try

Date.parse(your date here)/1000

Date.parse(new Date(2015, 5, 11, 12, 0, 0))/1000

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

3 Comments

Cool to see 'advanced' native Date functionality!
sounds like a cool feature but... Date.parse(2015, 5, 11, 12, 0, 0)/1000 (Date is may 11th 2015 at 12:00:00) and i have this timestamp : 1420070400 which is refer to january 1st 2015 at 01am ... sound like a problem... there is a way to fix it ?
use this Date.parse(new Date(2015, 5, 11, 12, 0, 0))/1000
1

you can use the library momentjs to convert it.

Here you are assigning an instance of momentjs to CurrentDate:

var CurrentDate = moment();

Here just a string, the result from default formatting of a momentjs instance:

var CurrentDate = moment().format();

And here the number of seconds since january of... well, unix timestamp:

var CurrentDate = moment().unix();

momentjs guide

Comments

1

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

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.