I know when constructing a Date
object in JavaScript with a dateString
parameter, the string must be something that parse()
can recognize.
What date format can parse
recognize?
For example:
var postDate = new Date("2011-03-08T23:52:38");
works in Chrome and Internet Explorer, but fails on an iPhone (returns Jan 1, 1970).
I cannot find any formal documentation on the .parse()
method, or the constructor, about what the parameter should be.
The format yyyy-mm-ddThh:nn:ss
doesn't work. What is the allowed format string?
Using the Date. One way to check if a string is date string with JavaScript is to use the Date. parse method. Date. parse returns a timestamp in milliseconds if the string is a valid date.
The ISO 8601 date format is as follows: YYYY-MM-DDTHH:mm:ss. sssZ. The characters represent the following data: YYYY – the current year, in four digits (e.g. 2020)
The MDC documentation of Date.parse()
states (quoting) :
It accepts the IETF standard (RFC 1123 Section 5.2.14 and elsewhere) date syntax:
"Mon, 25 Dec 1995 13:30:00 GMT"
.
OP Edit:
.NET syntax to create this datetime string:
/*
* r (RFC1123Pattern)
* ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
* Mon, 15 Jun 2009 20:45:30 GMT
*/
dateTime.ToUniversalTime().ToString("r", CultureInfo.InvariantCulture); //"r" = RFC1123Pattern
Edit: The
r
(RFC1123 pattern) always appends "GMT", even though the time isn't GMT (it's local). You need to call.ToUniversalTime()
first, in order to have the time actually be GMT.
Using the format that is produced by Date's toJSON method will work. This is the same as the toISOString method.
The date format is YYYY-MM-DDTHH:mm:ss.sssZ
Note: The time zone is always UTC as denoted by the suffix "Z".
var d = new Date();
console.log(d.toJSON());
console.log(d.toJSON() === d.toISOString());
console.log(Date.parse(d.toJSON()) === Date.parse(d.toISOString()));
You may find that the date shown isn't the same as on your clock; remember the time zone is UTC.
References:
Date.prototype.toJSON()
Date.prototype.toISOString()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With