Nick Nick - 7 days ago 11
jQuery Question

Parse Date and Time - javascript

I'm currently trying to parse the date and time as a number.

Any help would be great

Date.parse(11 + " " + 25 + ", " + 2016)


Would return
1480032000000


How would I incorporate time into this?

I've tried something along the lines of
Date.parse(2 + " " + 2 + ", " + 2016 + " " + 09:00:00 GMT)
but it doesn't work.

Any help would be great!

Thanks

Answer

The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognised or, in some cases, contains illegal date values (e.g. 2015-02-31).

Pulled from the mozilla documentation:

Date.parse('Wed, 09 Aug 1995 00:00:00 GMT');

Returns 807926400000 no matter the local time zone as GMT (UTC) is provided.

Date.parse('Wed, 09 Aug 1995 00:00:00');

Returns 807937200000 in time zone GMT-0300, and other values in other time zones, since there is no time zone specifier in the argument and it is not ISO format, so is treated as local.

Date.parse('Thu, 01 Jan 1970 00:00:00 GMT');

Returns 0 no matter the local time zone as a time zone GMT (UTC) is provided.

Date.parse('Thu, 01 Jan 1970 00:00:00');

Returns 14400000 in time zone GMT-0400, and other values in other time zones, since no time zone is provided and the string is not in ISO format, therefore the local time zone is used.

Date.parse('Thu, 01 Jan 1970 00:00:00 GMT-0400');

Returns 14400000 no matter the local time zone as a time zone GMT (UTC) is provided.

Date.parse('11/25/2016 00:00:00 GMT');

returns:

1480032000000

Find the documentation here.

Comments