Why are those dates different? - javascript

Here the console output:
new Date(2016, 08, 22)
Thu Sep 22 2016 00:00:00 GMT+0200 (CEST)
new Date("2016, 08, 22")
Mon Aug 22 2016 00:00:00 GMT+0200 (CEST)
Different months but why ?

Javascript months are 0 based in the numeric case, but in the string parsing 08 is mapped to August as it is a string translation of "August" in standard date format.
Date is being invoked in different ways as mentioned here
new Date();
new Date(value);
new Date(dateString);
new Date(year,month[, day[, hour[, minutes[, seconds[, milliseconds]]]]]);
In your case, 3 & 4 formats are being called.
>> new Date(2016, 08, 22)
>> Thu Sep 22 2016 00:00:00 GMT+0530 (IST)
>> new Date(2016, 01, 22)
>> Mon Feb 22 2016 00:00:00 GMT+0530 (IST)
>> new Date(2016, 0, 22)
>> Fri Jan 22 2016 00:00:00 GMT+0530 (IST)
>> new Date("2016-08-22")
>> Mon Aug 22 2016 05:30:00 GMT+0530 (IST)
>> new Date("2016/08/22")
>> Mon Aug 22 2016 00:00:00 GMT+0530 (IST)
#RobG's input from the comments:
...parsing ofstrings other than ISO 8601 extended format is entirely
implementation dependent. The result of parsing "2016, 08, 22" could be anything, including an invalid Date.

The second Date constructor you use is intended to parse a (known) string representation of a date, like "Dec 25, 1995". The format you pass in is not a standard one, so even though the result is close to the correct date (and could be fixed by correcting the month value, as pointed out by DhruvPathak), it should not be used as results my differ depending on the runtime/browser.

Related

new Date() render the wrong date format

i am struggling with my code, new Date() convert my value to next day 1 hour
new Date('2013-03-27T23:59:59.999Z') // => Thu Mar 28 2013 00:59:59 GMT+0100
As a Solution:
new Date('2013-03-27T23:59:59.999Z'.replace(/-/g, '\/').replace(/T.+/, '')) // => Wed Mar 27 2013 00:00:00 GMT+0100
Any suggestions how to get the correct date?

Javascript vs Date

I have some interesting problem and can't find the solution. Look at this:
var d1 = new Date("07 31 2014");
document.write(d1);
document.write('<br />');
var d2 = new Date(1406746800 * 1000);
document.write(d2);
when I run this script I get this result:
Thu Jul 31 2014 00:00:00 GMT+0500 (UZT)
Thu Jul 31 2014 00:00:00 GMT+0500 (UZT)
then after I changed my time zone I get this result:
Thu Jul 31 2014 00:00:00 GMT-0800 (AKDT)
Wed Jul 30 2014 11:00:00 GMT-0800 (AKDT)
as you can see the second result is Jul 30, but first result is Jul 31. I think they must both be equal to 31 Jul. I know this problem depends on the timezone but is there a solution?
So the constructor parameter is:
an Integer value representing the number of milliseconds since 1
January 1970 00:00:00 UTC
So given your integer value, that represents (for me, in BST):
Wed Jul 30 2014 20:00:00 GMT+0100
Which is
Wed Jul 30 2014 19:00:00 UTC
And your timezone is GMT-8, so thats the above -8 which gives:
Wed Jul 30 2014 11:00:00 GMT-0800 AKDT
The date string constructor constructs the date in your local timezone. You can see what the value should be by doing this:
new Date("07 31 2014").getTime() / 1000
Which returns 1406761200, not 1406746800

How to compare dates to see where adjacent times meet?

I have dates currently formatted in the following way:
[ [ Tue Jun 17 2014 09:00:00 GMT-0400 (EDT),
Tue Jun 17 2014 10:00:00 GMT-0400 (EDT) ] ]
[ [ Thu Jun 19 2014 09:30:00 GMT-0400 (EDT),
Thu Jun 19 2014 11:30:00 GMT-0400 (EDT) ] ]
[ [ Tue Jun 17 2014 10:00:00 GMT-0400 (EDT),
Tue Jun 17 2014 11:00:00 GMT-0400 (EDT) ] ]
These dates are actually "sessions", and I need to see where certain sessions are adjacent to each other. For example, in this specific case, the first array of dates has a end time of 10AM while the last array of dates has a start time for 10AM. How can I computationally find this situation?
The one approach I have is to first sort the array sets from earliest time to to latest time, and then compare each of the start/end date pairs to see if they are the same, but I can't seem to get it through. Any ideas are welcome!
Turn the strings into Unix timestamps with Date.parse() (if these are actually Date objects, then use the .getTime() method) and then order the sessions with Array.prototype.sort(). Here's an example where the sessions are ordered by start time:
var sessions = [
["Tue Jun 17 2014 09:00:00 GMT-0400 (EDT)", "Tue Jun 17 2014 10:00:00 GMT-0400 (EDT)"],
["Thu Jun 19 2014 09:30:00 GMT-0400 (EDT)", "Thu Jun 19 2014 11:30:00 GMT-0400 (EDT)"],
["Tue Jun 17 2014 10:00:00 GMT-0400 (EDT)", "Tue Jun 17 2014 11:00:00 GMT-0400 (EDT)"]
];
for (var i = 0; i < sessions.length; i++) {
sessions[i].startTime= Date.parse(sessions[i][0]);
}
sessions.sort(function(a, b) { return a.startTime-b.startTime; });

Strange Date( )'s created with Strings containing NULL bytes

I know that JavaScript doesn't NULL terminate strings like C or C++ do, but I ran into a case which I can't explain.
Look at the following code (executed in Node.js v0.10.5) :
> new Date('123')
Fri Jan 01 123 00:00:00 GMT+0100 (CET) // UNIX epoch : -58285702800000
> new Date('123\056')
Fri Jan 01 123 00:00:00 GMT+0100 (CET) // UNIX epoch : -58285702800000
> new Date('123\0456')
Tue Jun 01 123 00:00:00 GMT+0200 (CEST) // UNIX epoch : -58272660000000
> new Date('123\0567')
Thu Jul 01 123 00:00:00 GMT+0200 (CEST) // UNIX epoch : -58270068000000
> new Date('123\0999')
Fri Jan 01 123 00:00:00 GMT+0100 (CET) // UNIX epoch : -58285702800000
> new Date('123\0555')
Sat May 01 123 00:00:00 GMT+0200 (CEST) // UNIX epoch : -58275338400000
> new Date('123\0655')
Sat Jan 01 12355 00:00:00 GMT+0100 (CET) // UNIX epoch : 327718911600000
I'm not sure what's happening here, can someone explain it to me ?
It would seem that sometimes, the integers after a NULL byte define the month of the date, but the month not always correspond to the following number.
Those are 3-digit octal escapes, not null bytes. So for example '123\0456' is realy '123%6'.

JavaScript Node.js Date Comparison and Array sort by Date

First, does JavaScript have no basic Date comparision the way Python might?
In my Node.js script, I have the following lines:
console.log(Date(2012,11,10) < Date(2012, 11, 9))
console.log(Date(2012,11,10) > Date(2012, 11, 9))
However, both of these lines return false.
It appears there are tons of questions about "Date Comparison in JavaScript".
However, for virtually all of them someone responds with their home-made solution for comparing dates, some of them quite long and/or "hacky".
Is there really no inherent or idiomatic way to simply compare dates in JavaScript.
Do any Node libraries support it?
You need to use the new keyword
console.log(new Date(2012,11,10) < new Date(2012, 11, 9))
console.log(new Date(2012,11,10) > new Date(2012, 11, 9))
As Elias Van Ootegem pointed out it's a standard to return a string if the new keyword is omitted:
When Date is called as a function rather than as a constructor, it returns a String representing the current time (UTC).
NOTE
The function call Date(…) is not equivalent to the object creation expression new Date(…) with the same arguments.
Source: 15.9.2 The Date Constructor Called as a Function
I just ran into date comparison problem myself, and though I can see #dev-null's answer does work for dates constructed as new Date(2012,11,10), it doesn't seem work for parsed dates, at least in my case:
[vps#palladin]~$ node -v
v4.3.2
[vps#palladin]~$ node
> new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)') == new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)')
false
> new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)') < new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)')
false
> new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)') > new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)')
false
The only way that I found to reliably compare J/S Date objects is using getTime() function, and compare the results from both.
> new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)').getTime() == new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)').getTime()
true
> new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)').getTime() < new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)').getTime()
false
> new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)').getTime() > new Date('Wed Mar 09 2016 17:31:58 GMT-0800 (PST)').getTime()
false

Categories

Resources