Javascript new Date() returns different date - javascript

I did create a date object out of '31/12/2018':
new Date('2018', '12', '31')
It does however create something completely different that I would expect.
Date {Thu Jan 31 2019 00:00:00 GMT+0100 (Central European Standard Time)}
What's happening?

Months are indexed starting from 0. Use 11 for December, not 12 :
new Date(2018, 11, 31)
(and yes, there should be numbers instead of strings, which makes it a little less confusing)
From the MDN :
month
Integer value representing the month, beginning with 0 for
January to 11 for December.

Months start with 0 in JavaScript. January is 0; December is 11. 12 represents January the following year. You'll want to use 11 instead of 12:
new Date('2018', '11', '31')
-> Mon Dec 31 2018 00:00:00 GMT+0100 (Central European Standard Time)

You've forget that months in JS starts with 0 instead 1.
Please use
new Date('2018', '11', '31')
in your case.

Related

JavaScript newDate(Date.UTC) Results in the Wrong Month [duplicate]

using Mozilla Firefox Firebug:
var myDate = new Date(2012, 9, 23, 0,0,0,0);
myDate;
Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}
Why does javascript create the date with the wrong month?
No, javascript's Date months start with 0, so 9 is a 10th month and it is October
Reference:
new Date(year, month [, day, hour, minute, second, millisecond]);
[...]
month
Integer value representing the month, beginning with 0 for January to 11 for December.
In the javascript world months begin with zero!
kind of weird to me.
Anyhow, 9 is NOT September, but rather 9 is October.
Use a string as a parameter to avoid that weird behavior of Date constructor.
Example:
const myDate = new Date('2021-08-13'); // Result: Fri Aug 13 2021 02:00:00 GMT+0200...
In javascript Date object mounts are starting from ( 0 to 11 ) its funny :)
just always write
new Date(yea,month - 1,seconds ,millisecond)

moment(?).year(?).week(?).weekday(?).format('MM/DD/YYYY') not working in December

The issue is specific to moment.year()
I am trying to get the beginning date of the next week based on the current date. It works well except for the last month of the year. The example should explain.
new Date(moment(1577379939000).year(2020).week(1).weekday(0).format('MM/DD/YYYY'))
Sun Dec 29 2019 00:00:00 GMT-0500 (Eastern Standard Time)
This gives me Sun Dec 29 2019 00:00:00 GMT-0500 (Eastern Standard Time) 29th dec 2019 as begining of the next week(sunday)
This is correct
1577379939000 is 26th of December 2019(Thursday).
However, if the input is 1577466306000 which is 27th of December 2019(Friday)
it gives me 2020 December date, not 2019
new Date(moment(1577466306000).year(2020).week(1).weekday(0).format('MM/DD/YYYY'))
Sun Dec 27 2020 00:00:00 GMT-0500 (Eastern Standard Time)
If it is moment.js error. is there any workaround?
Try to use weekYear instead of year
I think this is working as intended. After you use .year(2020) you're changing the dates to December 2020. This is the calendary for that month:
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
In the United States, moment.js defines weeks to start on Sunday, and week 1 is the week containing January 1. So the week beginning with December 27 is actually week 1 of 2021.
That means that when you call .week(1), it doesn't change anything for December 27, and returns a date in December 2020 when you call .weekday(0).
But December 26 is in week 26 of 2020, so calling .week(1) changes the date to Saturday in week 1 of 2020, which is January 4. Then calling .weekday(0) returns the Sunday before that, which is December 29, 2019.
As mentioned in a comment, the way to get the first day of the next week after a given date, just use .weekday(7).
new Date(moment(1577466306000).weekday(7))
I'm not sure why you're specifying a particular year or week number.

Why isn't Date(2015, 11, 1) the first of December? [duplicate]

This question already has answers here:
Incorrect date shown in new Date() in JavaScript
(3 answers)
Closed 5 years ago.
When I convert that date to ISO string I get the 30th of november 2015. Why wouldn't it be first of december? I have googled and I know that month is 0-indexed and that overflows lead to the next day/month/year. But I cannot explain myself that behaviour and when I google it I find unrelated topics.
Thing is if You type:
var date = new Date(2015, 11, 1);
console.log(date);
You will get output based on your timezone, for me it's:
Tue Dec 01 2015 00:00:00 GMT+0100 (Central Europe Standard Time)
Function toISOString will always output time in UTC. So in this case you will get this date minus one hour.
2015-11-30T23:00:00.000Z
If you check the MDN page you'll see that:
Note: Where Date is called as a constructor with more than one argument, the specifed arguments represent local time. If UTC is desired, use new Date(Date.UTC(...)) with the same arguments.
Your users have different local timezones.
For me new Date(2015, 11, 1) gives Tue Dec 01 2015 00:00:00 GMT+0100 (Romance Standard Time) (I'm in the timezone Central European Time which is GMT+1).
Hence you can follow the MDN hint and use Date.UTC inside of your date call instead:
var date = new Date(2015, 11, 1);
console.log(date.toString());
// "Tue Dec 01 2015 00:00:00 GMT+0100 (Romance Standard Time)"
// 00:00:00 in GMT+1 but 23:00:00 in GMT+0
console.log(date.toISOString());
// "2015-11-30T23:00:00.000Z"
// ^^ 30th of november - that's a nogo!
var utcDate = new Date(Date.UTC(2015, 11, 1));
console.log(utcDate.toString());
// "Tue Dec 01 2015 01:00:00 GMT+0100 (Romance Standard Time) 15:49:26.146"
// 01:00:00 in GMT+1 but 00:00:00 in GMT+0
console.log(utcDate.toISOString());
// "2015-12-01T00:00:00.000Z"
// ^^ The first! Not the 30th!
var date = new Date(Date.UTC(2015, 11, 1));
console.log(date.toISOString());
Output:
2015-12-01T02:00:00.000Z

Date Format Issues

So I have this date returned from an API:
"2014-08-07T00:00Z"
And the results from new Date("2014-08-07T00:00Z") equal Wed Aug 06 2014 20:00:00 GMT-0400 (EDT) and .getDay() on that date gives me 3.
Why is it going from August 7th, to 6th, and the getDay returns 3?
Basically I'm trying to turn the API return date into english.
days[d.getDay()]+", "+months[d.getMonth()]+" "+getOrdinal(d.getDay())+" "+formatAMPM(d)
(aka "Wednesday, August 3rd 8:00 pm")
console.log(d,data[i].startDate, d.getDay());
//yields
// Wed Aug 06 2014 20:00:00 GMT-0400 (EDT) "2014-08-07T00:00Z" 3
days is just an array of text days, as is months, getOrdinal is a function that gives the text st or nd or rd on the day, and formatAMPM is pretty obvious.
.getDay() returns the day of the week. I think you'd expect .getDate()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate
That's why you're seeing that it is a Wednesday.
The result for me of new Date("2014-08-07T00:00Z") is Wed Aug 06 2014 18:00:00 GMT-0600 (MDT). This is because dates are converted to your local timezone when constructed
I don't think that there is any problem here.
Your date is 2014-08-07T00:00Z, with Z meaning Zulu Time Zone (equivalent to UTC), and using new Date() on it will convert to you local time, here GMT-4, that is why you are getting 4 hours difference.
And For the "3", the getDay() method returns the day of the week, wednesday in your case.

javascript is creating date wrong month

using Mozilla Firefox Firebug:
var myDate = new Date(2012, 9, 23, 0,0,0,0);
myDate;
Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}
Why does javascript create the date with the wrong month?
No, javascript's Date months start with 0, so 9 is a 10th month and it is October
Reference:
new Date(year, month [, day, hour, minute, second, millisecond]);
[...]
month
Integer value representing the month, beginning with 0 for January to 11 for December.
In the javascript world months begin with zero!
kind of weird to me.
Anyhow, 9 is NOT September, but rather 9 is October.
Use a string as a parameter to avoid that weird behavior of Date constructor.
Example:
const myDate = new Date('2021-08-13'); // Result: Fri Aug 13 2021 02:00:00 GMT+0200...
In javascript Date object mounts are starting from ( 0 to 11 ) its funny :)
just always write
new Date(yea,month - 1,seconds ,millisecond)

Categories

Resources