On 1 jan 0099 there was Thrusday but it return. Friday
days = new Date(" January 1 ,0099")
day = days.getDay()
alert(day);
RESULT
5
But it should return 4
Basically, it appears Javascript won't construct a Date in the year 99:
year
Integer value representing the year.
Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#Syntax
You can try with different formats, 99 always appears to map to 1999. Likely this was implemented as a workaround and/or “convenience” for Y2K dates, perhaps even inherited from Java.
I'm not sure if there's a better workaround, but this works:
let d = new Date(100, 0, 1);
d.setFullYear(99);
Related
This question already has answers here:
JavaScript function to add X months to a date
(24 answers)
Adding months to a Date in JavaScript [duplicate]
(2 answers)
Closed 2 years ago.
I'm trying to find a method that reliably subtracts 1 month from a javascript date object.
I have this code:
var shippedDate = new Date('12/31/2020');
var tempDate = new Date(shippedDate.setMonth(shippedDate.getMonth() - 1)); //subtract 1 month
alert(tempDate);
The value in tempDate after this code runs is 12/1/2020 when it should actually be 11/30/2020.
I checked my math with this online date calculator: https://www.timeanddate.com/date/dateadded.html?m1=12&d1=31&y1=2020&type=sub&ay=&am=1&aw=&ad=&rec=
Thanks.
December has 31 days so when you subtract 1 month, you get 31 November which doesn't exist, so it rolls over to 1 December.
You can test the date (day in month) to see if it's the same, and if not, set the date to 0 so it goes to the last day of the previous month.
Also, setDate modifies the Date object so no need to create a new one:
function subtractMonth(date, months) {
let d = date.getDate();
date.setMonth(date.getMonth() - months);
if (date.getDate() != d) {
date.setDate(0);
}
return date;
}
let d = new Date(2020, 11, 31); // 31 Dec 2020
console.log(subtractMonth(d, 1).toString()); // 30 Nov 2020
This has side effects so that sequentially subtracting 2 months may give a different result to subtracting 2 months in one go.
Also in regard to new Date('12/31/2020'), see see Why does Date.parse give incorrect results?
PS
I answered this before I remembered that there were plenty of questions about adding months that also cover subtracting. So I marked this question as a duplicate and rather than delete this answer, left it for posterity.
If you wish to vote for an answer, please go to one of the duplicates and vote for an answer there. :-)
On my own experience, I may qualify all around Date calculation in javascript as completely unbearable pain.
Avoid as possible own crafted function to any Date manipulation. There are too many traits to lose mind at all. Timezones, wrong clocks, timezone on your own host vs. timezone on server, unexpected toString conversion according to local host timezone/clock.
If you rally need to make some dates calculation use battle tested library, like date-fns, moment.js, etc.
By the way your example almost correct, you just have chosen not suitable time to try to test it. The only one that I see problematic it's using setMonth that mutate original shippedDate.
If I'm executing this:
var date = new Date("10.31");
date.setFullYear(-125);
the output of date is Sun Oct 31 -125 00:00:00 GMT+0200 (W. Europe Summer Time)
If I check this on wolframalpha the day seems to be tuesday.
Can someone explain why not the same day is displayed by both source?
The reason for the difference between JavaScript and wolframalpha website is that JavaScript is calculating the years mathematically, so it includes the year zero. Try to set the year to zero in JavaScript and you will see that it works. However, there is no such a thing as year zero, and the year before year 1 is year 1 BC. Try to set the year to zero on wolframalpha website and you get an error, while it automatically converts all negative years to years BC. This is the correct behavior.
To get the BC years in JavaScript, add 1 to every year below 1. So year 0 becomes 1BC, and year -125 becomes 126BC. In JavaScript this gives you Sunday, and 126BC on wolframalpha website gives you Sunday too. 125BC gives you Tuesday on wolframalpha website, and -124 gives you the same in JavaScript.
var date = new Date();
date.setFullYear(-124);
date.setMonth(9);
date.setDate(31);
console.log(date.toString());
date.setFullYear(-125);
console.log(date.toString());
negative years in javascript do produce a BC date but it's kind of a poor design. Wolfram Alpha is probably correct. See this answer for more: Why does Date accept negative values?
Javascript dates start in 1970.
Let's do a quick count.
(new Date()).setYear(-125); //returns -66085584766591 (milliseconds from time 0)
//Let's convert those milliseconds in years...
//-66085584766591 = -2095,56 (years???)
As you can see, you can't rely on negative dates in Javascript.
This question already has answers here:
getMonth in javascript gives previous month
(6 answers)
Closed 5 years ago.
In a project I need to convert in javascript a timestamp into a human readable string, however the number of month was incorrect so I performed the following test in a javascript console :
How to explain why there is a one month difference ?
Thank you in advance
Javascript dates start at 0 for months
example:
0: Jan
1: Feb
2: Mar
etc
In javascript months start from 0. So January is 0, and december is 11
docs
Return value An integer number, between 0 and 11, representing the
month in the given date according to local time. 0 corresponds to
January, 1 to February, and so on.
Javascript's date.getMonth starts the month count from 0. So you need to add +1.
var date = new Date();
console.log(date.getMonth() + 1);
I'm trying to validate a uk date using this code:
function ukdate(d) {
var p = new Date(d.split('/')[2], d.split('/')[1] -1, d.split('/')[0]);
if(p.toString() !== 'Invalid Date') {
return p;
}
}
http://jsfiddle.net/GE3xU/1/
so if I try ukdate('31/12/1981') it correctly returns "The Dec 31 1981". However if i try ukdate('12/31/1981') it returns "Tue Jul 12 1983".
Why is this happening? I'm expecting the second test to return invalid date because 31 is not a valid month.
JavaScript is converting your date for you.
In simple examples, you can get the last day of a given month by asking for the 0th day of the following month. Similarly, the "32nd of August" would be corrected to the 1st of September.
Months work similarly. The 13th month of a given year is the 1st month of the next. The 0th month of a year is December of the previous.
31 % 12 = 7, hence July, and floor(31/12) = 2 hence the year being shifted forward by two.
This is intended behaviour for JavaScript.
May I interest you in <input type="date" />? It uses whatever format is defined on the user's computer (ie. it is "locale-aware"), which is already excellent for user experience. On top of that, supporting browsers will render a calendar date picker, especially useful on phones too. Internally, the date is in "standard" YYYY-mm-dd format.
The month value is divided by 12 and added to the year, then the remainder is used as the actual month value.
See the spec
Let ym be y + floor(m /12).
Let mn be m modulo 12.
I'm developing with node.js, and I'm trying to create a date object on the server.
when coding:
var birthyear = 2000;
var birthmonth = 7;
var birthday = 24;
var date = new Date(birthyear, birthmonth, birthday);
console.log(date);
OUTPUT:
Thu Aug 24 2000 00:00:00 GMT+0300 (Jerusalem Daylight Time)
As you can see, I'm getting August instead of July.
How can I fix that issue ?
The month argument in the Date() constructor doesn't start at 1 for January, but instead at 0. Therefore, supplying the month value of 7 gives you the eight month, which is August.
From MDN:
month: Integer value representing the month, beginning with 0 for January to 11 for December.
Months in JS start at 0
so it's a quite an easy fix:
var date = new Date(birthyear, birthmonth-1, birthday);
DEMO
Months in the JavaScript Date() constructor are 0-indexed, meaning that month 7 actually is August. Use the value 6 for July.
Annoyingly enough, Date is rather inconsistent about which fields are 0-indexed and which are 1-indexed, and in fact the same field can be either one depending on the context. You should always refer to documentation when you have to use dates.
Yea, that's weird, but months should be counted starting with
0