Javascript Date.UTC() function is off by a month?
The month is zero-based for JavaScript.
Days and years are one-based.
Go figure.
UPDATE
The reason this is so, from the creator of JavaScript, is
JS had to "look like Java" only less so, be Java's dumb kid brother or boy-hostage sidekick. Plus, I had to be done in ten days or something worse than JS would have happened.
http://www.jwz.org/blog/2010/10/every-day-i-learn-something-new-and-stupid/#comment-1021
javascript UTC timestamp convert to Date 1 month off
Javascript months are zero based
This is a duplicate question
Check this thread for more insight Javascript Date.UTC() function is off by a month?
Date.UTC output is wrong
Month should be a number between 0 and 11, not 1 and 12. See the documentation
Is the Javascript date object always one day off?
Notice that Eastern Daylight Time is -4 hours
and that the hours on the date you're getting back are 20
.
20h + 4h = 24h
which is midnight of 2011-09-24. The date was parsed in UTC (GMT) because you provided a date-only string without any time zone indicator. If you had given a date/time string w/o an indicator instead (new Date("2011-09-24T00:00:00")
), it would have been parsed in your local timezone. (Historically there have been inconsistencies there, not least because the spec changed more than once, but modern browsers should be okay; or you can always include a timezone indicator.)
You're getting the right date, you just never specified the correct time zone.
If you need to access the date values, you can use getUTCDate()
or any of the other getUTC*()
functions:
var d,
days;
d = new Date('2011-09-24');
days = ['Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat'];
console.log(days[d.getUTCDay()]);
What is the source of this off-by-one error in a JavaScript date?
Other than mixing UTC and local dates, the way you're adding 3 months will cause an incorrect response for dates like 31 March, where adding 3 months simply by incrementing the month number results in a date for 1 July. See Adding months to a Date in JavaScript.
So validate('07,2020')
will return false if run on 31 March.
To fix that, when adding months, check that the updated date is still on the same day in the month, otherwise it's rolled over so set it to the last day of the previous month.
function validate(s) { let testDate = addMonths(new Date(), 3); let [m, y] = s.split(/\D/); return testDate < new Date(y, m-1);};
function addMonths(date, months) { let d = date.getDate(); date.setMonth(date.getMonth() + +months); // If rolled over to next month, set to last day of previous month if (date.getDate() != d) { date.setDate(0); } return date;}
// Sampleconsole.log('On ' + new Date().toDateString() + ':');['07/2020', '04/2020'].forEach( s => console.log(s + ' - ' + validate(s)));
Why Date.UTC(2015, 08, 31) == Date.UTC(2015, 09, 01) in javascript?
The month parameter is zero-indexed. So 8 is actually september which only has 30 days. So it flows over to october.
Reference: Date.UTC()
How to convert a Date to UTC?
The
toISOString()
method returns a string in simplified extended ISO
format (ISO 8601), which is always 24 or 27 characters long
(YYYY-MM-DDTHH:mm:ss.sssZ
or±YYYYYY-MM-DDTHH:mm:ss.sssZ
,
respectively). The timezone is always zero UTC offset, as denoted by
the suffix "Z
".
Source: MDN web docs
The format you need is created with the .toISOString()
method. For older browsers (ie8 and under), which don't natively support this method, the shim can be found here:
This will give you the ability to do what you need:
var isoDateString = new Date().toISOString();
console.log(isoDateString);
Javascript date - month always increment
Javascript Date
's month starts from 0. So 7 is actually 8th Month which is August.
month
Integer value representing the month, beginning with 0 for January to 11 for December.
Related Topics
How to Get All Arguments of a Function as Single Object Inside That Function
Mixing Jsf El in a JavaScript File
How to Prevent Closing Browser Window
Access Elements of Parent Window from Iframe
The Best Way to Synchronize Client-Side JavaScript Clock with Server Date
In R, How to Display Value on the Links/Paths of Sankey Graph
Differencebetween Compile and Link Function in Angularjs
Passing a Function with Parameters as a Parameter
Differencebetween 'Let' and 'Const' Ecmascript 2015 (Es6)
Dt: Link Binding Is Lost After Re-Rendering the Table
How to Include Newlines in Labels in D3 Charts
Best Way to Display Flash Notices in Rails
JavaScript Parse Float Is Ignoring the Decimals After My Comma
Conditionalpanel JavaScript Conditions in Shiny: Is There R %In% Operator in JavaScript