Jquery new Date return wrong result - javascript

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)

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)))

Date.UTC failed to show correctly

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();
}

Convert MMM YY string to date javascript

I have a value JUN 16 as string. I need to convert it to a date object. I need to find out last date of the month JUNE year 2016.
This is how I am converting a Date("MMM-YY") to Date("YYYY-MM-DD")
Input : Nov-17
run : new Date("Nov-17")
Output : Sat Nov 17 2001 00:00:00 GMT+0530 (India Standard Time)
now by adding a prefix to your date like below
input : 1-Nov-17 //add prefix to your string '1-'
run : new Date("1-Nov-2017")
output: Wed Nov 01 2017 00:00:00 GMT+0530 (India Standard Time)
Moment
run : moment("1-Nov-17").endOf('month').format("YYYY-MM-DD") //End day of the month
output: "2017-11-30"
I have a value "JUN 16" as string .I need to convert to date object
This is how you do it:
var date = new Date(2016, 5, 16);
console.log(date);
i need to find out last date of the month JUNE year 2016
And this is how you can do that:
// 5 is June
var date = new Date(2016, 5 + 1, 0);
console.log(date.getDay())
This tells you that the day is 4 which is "Thursday"

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.

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