Date.UTC failed to show correctly - javascript

This Meteor client method tries to return Thu Mar 09 2017 00:00:00 GMT+1100 (AEDT) but it it returning Thu Mar 09 2017 11:00:00 GMT+1100 (AEDT) instead.
How can it be fixed? thx
dateToISO: (date) => { // date format in YYYY-MM-DD like "2017-03-09"
const dArr = date.split('-');
return new Date(Date.UTC(parseInt(dArr[0]), parseInt(dArr[1]) - 1, parseInt(dArr[2]), 0, 0, 0, 0));
}

Well, as I try to reproduce it, that actually seems like a bug in the Date.UTC function.
I was trying to do:
console.log("Unmanipulate: " + new Date(Date.UTC(97, 4, 13, 0, 0)));
console.log("Manipulate: " + new Date(Date.UTC(97, 4, 13, -3, 0)));
Which return:
Unmanipulate: Tue May 13 1997 03:00:00 GMT+0300 (IDT)
Manipulate: Tue May 13 1997 00:00:00 GMT+0300 (IDT)
Look like the time zone is pass to the hour params, which is really weird. maybe I'm missing something.
You can read more about the spec of this function here.
I would recommend use Moment.js, which make it really easy to deal with dates in js, like this:
function dateToISO (date) {
return moment(date).utc().format();
}

Related

Date(), Date.UTC() strange behavior with scope operator

Is that me doing something wrong or some known bug of using scope operator ([...arr]) with Date()/Date.UTC() constructor?
What confuses me:
x = [2015,5,1]; //(3) [2015, 5, 1]
new Date(2015, 5, 1); //Just as expected, Mon Jun 01 2015 00:00:00 GMT+0300 (Eastern European Summer Time)
new Date([...x]); //Fri May 01 2015 00:00:00 GMT+0300 (Eastern European Summer Time)
new Date(Date.UTC([...x])) //Invalid Date
p.s. I know, the latter is ambiguous, since Date() with more than 1 argument already returns UTC date
Date.UTC parameter is not an array.
Remove the array like :
x = [2015, 5, 1];
var d = new Date(Date.UTC(...x))
console.log(d)
Because you are passing the same array x as argument. Use Rest parameters. Because Date.UTC doesnot accept array as argument
UTC() takes comma-delimited date and time parameters
x = [2015,5,1]; //(3) [2015, 5, 1]
new Date(2015, 5, 1); //Just as expected, Mon Jun 01 2015 00:00:00 GMT+0300 (Eastern European Summer Time)
console.log(new Date(Date.UTC(...x)))

Why are those dates different?

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.

Compare dates issue - javascript

I need to compare dates in javascript.
After attempt many ways...
I choose:
var endDate = new Date(secondDate.getYear(), secondDate.getMonth(), secondDate.getDate(), 0, 0, 0,0);
var startDate = new Date(firstDate.getYear(), firstDate.getMonth(), firstDate.getDate(), 0, 0, 0, 0);
if (endDate.getTime() >= startDate.getTime()) {
isValid = true;
}
else {
isValid = false;
}
In my situation:
---startDate = Tue Apr 01 1997 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 01/04/1997)
---endDate = Thu Jul 26 114 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 26/07/2014)
You see? startDate is small then endDate, right?
But:
---endDate.getTime() returns: -58551904800000
---startTime.getTime() returns: 859845600000
so, endDate.getTime() >= startDate.getTime() returns false...
In other situation, it works well:
---startDate: Sat Jul 21 114 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 21/07/2014)
---endDate: Sat Jul 28 114 00:00:00 GMT+0200 (Jerusalem Standard Time) (i.e, 28/07/2014)
---startDate.getTime() returns -58552336800000
---endDate.getTime() returns -58551732000000
so, endDate.getTime() >= startDate.getTime() returns true...
It seems like that javascript functions have another behavior for dates after year 2000.
What should I do? which code will be match to all of the optional situations?
Thanks.
Yeah like ghusse said, there is a problem with your end time if you fixed it so it was 2014 you would get a result such as 1406329200000 instead of -58551904800000
I found a solution, after I read Josh and ghusse answers and advice:
Use getFullYear(), instead getYear(), and all will work O.K.
Apparently, you have a problem with your end dates :
Thu Jul 26 114 00:00:00 GMT+0200
Does not mean 21/07/2014 but 21/07/114
According to the doc, here are 2 correct ways of creating your date:
var endDate = new Date(21, 6, 2014);
// Or a string corresponding to a version of ISO8601
var endDate = new Date('2014-07-21T00:00:00z+3');

Jquery new Date return wrong result

I am using mvc 4. When I use bellow code in script
new Date(2012, 12, 16, 03, 20, 00)
it return as
Wed Jan 16 2013 03:20:00 GMT+0530 (India Standard Time)
Please help me, I am not able to understand why does this happen, it should return
Sun Dec 16 2012 03:20:00 GMT+0530 (India Standard Time)
Months are 0 based. When you pass it 12, it's essential month "13", which Javascript will interpret as January of the next year.
So to get the date you want, the code would be new Date(2012,11, 16, 03, 20, 00)

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