Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The correct Javascript Date.parse(...) format string?

Tags:

What is a culture-invariant way of constructing a string such that the Javascript Date() constructor can parse it and create the proper date object?

I have tried these format strings which don't work (using C# to generate the strings):

clientDate.ToString();
// gives: "11/05/2009 17:35:23 +00:00"

clientDate.ToString("MMM' 'dd', 'yyyy' 'h':'mm':'ss' 'tt");
// works on an English server
// but on a French server, gives: "mai 11, 2009 5:35:23"
// Javascript won't parse that.

clientDate.ToString("MM'-'dd'-'yyyy' 'HH':'mm':'ss")
// gives: 05-11-2009 17:35:23

What is the universal format??

like image 320
Jeff Meatball Yang Avatar asked Jul 06 '09 19:07

Jeff Meatball Yang


People also ask

What is the format of date in JavaScript?

The preferred Javascript date formats are: Dates Only — YYYY-MM-DD. Dates With Times — YYYY-MM-DDTHH:MM:SSZ.

What is date parse in JavaScript?

parse() 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 unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31). Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.

How do you parse a string in JavaScript?

Use the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

How convert dd mm yyyy string to date in JavaScript?

To convert a dd/mm/yyyy string to a date:Pass the year, month minus 1 and the day to the Date() constructor. The Date() constructor creates and returns a new Date object.


1 Answers

According to MDC:

Given a string representing a time, parse returns the time value. It accepts the IETF standard (RFC 1123 Section 5.2.14 and elsewhere) date syntax: "Mon, 25 Dec 1995 13:30:00 GMT". It understands the continental US time-zone abbreviations, but for general use, use a time-zone offset, for example, "Mon, 25 Dec 1995 13:30:00 GMT+0430" (4 hours, 30 minutes east of the Greenwich meridian). If you do not specify a time zone, the local time zone is assumed. GMT and UTC are considered equivalent.

If you can’t generate this format using english locale, try to use Date.UTC

like image 73
Maciej Łebkowski Avatar answered Nov 26 '22 06:11

Maciej Łebkowski