Creating new date using Javascript gives Invalid Date in IE11 - javascript

I'm creating a new date object in Javascript and it throws Invalid Date error in IE11.
It runs fine on Chrome and Firefox though.
Any idea what can be wrong?
new Date()
[date] Thu Sep 22 2016 12:24:33 GMT+0530 (India Standard Time)[date] Thu Sep 22 2016 12:24:33 GMT+0530 (India Standard Time)
[functions]
__proto__[date] Invalid Date
screenshot: http://screencast.com/t/hN4Kt8FEwdXu

Where do you see that it throws? The console displays a Date instance. The __proto__ property of this instance is an invalid date, but you should not care about that at all, it's part of the internal implementation.
Try new Date().toString(), you should get a valid string representation of your date, which means that all is fine.

It actually returns a date object where as the __proto__ (prototype) alone says "Invalid Date" which is as per the ES5 Date specification which goes like this,
The Date prototype object is itself a Date object (its [[Class]] is "Date") whose [[PrimitiveValue]] is NaN.
Source URL : http://es5.github.io/#x15.9.5
Hope this helps

Related

Javascript Date constructor depending by timezone [duplicate]

This is what I get in chrome console. I pass "2016-09-05"(YYYY-MM-DD) as the date and it shows me Sept 4,2016 as the date.
Another constructor shows the right date
Passing it comma separated needs some tokenizing + parsing + making month zero indexed which I want to avoid
If you omit the time in the new Date(string) constructor, UTC time is assumed. So the displayed value is actually correct. Use new Date('2016-09-05T00:00') to create the date object in local time.
Edit: while some browsers seem to support the yyyy-MM-dd HH:mm format, it's not officially supported, and, as mentioned in the comments, this doesn't work in Safari. I've updated the answer to use a T instead of a space between date and time.
Per the ECMAScript® 2023 Language Specification:
Date-only forms:
YYYY
YYYY-MM
YYYY-MM-DD
It also includes “date-time” forms that consist of one of the above
date-only forms immediately followed by one of the following time
forms with an optional UTC offset representation appended:
THH:mm
THH:mm:ss
THH:mm:ss.sss
You could use the Solution from UTC Date Conversion.
Which basicall does the following:
console.log(new Date("2014-12-23"));
console.log(convertDateToUTC(new Date("2014-12-23")));
function convertDateToUTC(date) {
return new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), date.getUTCHours(), date.getUTCMinutes(), date.getUTCSeconds());
}
The output would be like that in the console (for me at least :D)
Tue Dec 23 2014 01:00:00 GMT+0100 (Mitteleuropäische Zeit)
Tue Dec 23 2014 00:00:00 GMT+0100 (Mitteleuropäische Zeit)
use setFullYear the syntax is Date.setFullYear(year,month,day)
mydate = new Date();
mydate.setFullYear(2016, 08, 05);

What format do I use with moment to give me same string as `new Date()`?

I'm using moment() and want to know if there's a shortcut to give me the same date string as new Date() would give me.
I have to do some timezone conversions so using moment.js makes that easier, but I need the same date format: Thu Oct 06 2016 23:08:53 GMT-0700 (PDT) as the native Date object would give me.
I would assume there is a shortcut for this, but I can't find it.
moment.tz('America/New_York').format('???')
I could not find a shortcut however this gives the string I need:
moment.tz('America/New_York').format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ (z)')
from the docs: http://momentjs.com/docs/#/displaying/
Returns: Fri Oct 07 2016 02:25:17 GMT-0400 (EDT)
if you have no idea which timezone the browser is currently in, you can try moment.tz.guess(), it isn't 100% correct for timezone offset that has multiple names though:
var d = moment()._d;
d.toString().replace(/\([\w ]+\)$/, moment.tz(d, moment.tz.guess()).format('(z)'));
The date format you're referring to, depends on the culture/language of your browser. That aside, if you want the same format as the standard new Date().toString() returns, you just use the toString() method on the moment object, without any arguments:
moment().toString();
Source: http://momentjs.com/docs/#/displaying/as-string/

Javascript date string passed to Date constructor gives strange results

Why do I get such differing results with similarly formatted date strings when creating a new date?
CHROME (43.0.2357.134 m) console:
new Date('2014-12-25')
Wed Dec 24 2014 17:00:00 GMT-0700 (Mountain Standard Time)
[Chrome assumes Z with 00:00 (utc) and returns that local time]
new Date('2014-1-25')
Sat Jan 25 2014 00:00:00 GMT-0700 (Mountain Standard Time)
[What?! exact same format (I thought) but returns 25 instead of 24....see next...]
new Date('2014-01-25')
Fri Jan 24 2014 17:00:00 GMT-0700 (Mountain Standard Time)
[...oh, the leading 0 makes it use the logic it used in the first example]
new Date('2014/12/25')
Thu Dec 25 2014 00:00:00 GMT-0700 (Mountain Standard Time)
[using / instead of - results in what I believe most would expect(?): a local time
on the same date specified]
FIREFOX (39.0) console:
new Date('2014-12-25')
Date 2014-12-25T00:00:00.000Z
[different from Chrome]
new Date('2014-1-25')
Invalid Date
[not recognized in Firefox, unlike Chrome]
new Date('2014-01-25')
Date 2014-01-25T00:00:00.000Z
[different from Chrome]
new Date('2014/12/25')
Date 2014-12-25T07:00:00.000Z
The lesson seems to be: IF you're going to use strings in the Date constructor, make sure it's formatted correctly (per ECMAScript standard):
YYYY-MM-DDTHH:mm:ss.sssZ
CHROME:
new Date('2014-12-25T00:00:00.000-07:00')
Thu Dec 25 2014 00:00:00 GMT-0700 (Mountain Standard Time)
FIREFOX:
new Date('2014-12-25T00:00:00.000-07:00')
Date 2014-12-25T07:00:00.000Z
The ECMAScript standard says in 15.9.3.2
If Type(v) is String, then
Parse v as a date, in exactly the same manner as for the parse method (15.9.4.2);
And in 15.9.4.2 it says:
The function first attempts to parse the format of the String according
to the rules called out in Date Time String Format (15.9.1.15). If the
String does not conform to that format the function may fall back to any
implementation-specific heuristics or implementation-specific date formats.
...which says to me, if you don't provide that exact format, Chrome and Firefox and everyone else can interpret the date string how they deem right. (note: there is some leeway on the format, for example, the value of an absent sss field is “000”. see section 15.9.1.15 for more details)

Javascript newDate(datestring) seems to be changing my time zone

So I have a datestring that looks like
2014-08-02T19:00:00
Which should come out to Aug 02 2014 7PM (EST) but when I create a date object it seems to change it to
Sat Aug 02 2014 15:00:00 GMT-0400 (Eastern Daylight Time)
Basically it seems to be assuming that the datestring was in GMT-0000. Is there a way for me to tell it the GMT of the datestring so it doesn't automatically adjust it?
That's because your datestring is in GMT.
If your datestring actually is in EST, it should look like this:
2014-08-02T19:00:00-04:00
Specification: http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15
Do not use the Date constructor to parse strings (it uses Date.parse).
An ISO 8601 format string with not timezone will be parsed as UTC by some browsers (e.g. Firefox per ES5) and local by others (e.g. Chrome per the ES 6 draft) and fail completely in yet others to return NaN (e.g. IE 8). Parse the string yourself so that it is always one or the other.
The following will parse it as UTC:
function parseISOUTC(s) {
var b = s.split(/\D+/);
return new Date(Date.UTC(b[0], --b[1], b[2], b[3], b[4], b[5]));
}
console.log(parseISOUTC('2014-08-02T19:00:00').toISOString()); // 2014-08-02T19:00:00.000Z
Given the change in specification between ES5 and the edition 6 draft, event parsing the one format specified by ECMA-262 in the very latest browsers will remain problematic for some time.

please explain this javascript debugger output regarding Date constructor

I am trying to instantiate a date so that the code works in Chrome and IE (et al). Ideally I'd like to find a simple statement rather than a UDF, if it's possible. Is it not possible to Date.parse the string value in javascript when the time chunk is represented as T00:00:00?
Here's what I have in the Immediate Window in Visual Studio; caldate contains a string representation of a date returned by the back-end database; passing that string to Date.parse() returns a timestamp, 1371441600000, and passing that timestamp to the Date() constructor returns both Mon Jun 17 00:00:00 EDT 2013 and [prototype]: Invalid Date.
?caldate
"2013-06-17T00:00:00"
?Date.parse(caldate);
1371441600000
?new Date( Date.parse(caldate) );
Mon Jun 17 00:00:00 EDT 2013
[prototype]: Invalid Date
The Invalid Date is normal. That is just what the debugger prints for the proto object of a Date. I believe this is because the debugger calls the toString method on the proto object without supplying the actual Date instance, and so the toString method returns "Invalid Date".
I suggest you read the MDN documentation on Date.
You can just use new Date(caldate) to create a Date from your string.

Categories