Take the following code
var d = new Date();
d.setFullYear(0);
alert(d);
What year is year 0000? After all, year 0 isn't actually a thing, since we went from 1BC to 1AD. Is year 0 actually 1BC and year -1 actually 2BC?
The ES262 specification says:
20.3.1.3 Year Number
ECMAScript uses a proleptic Gregorian calendar to map a day number to a year number and to determine the month
and date within that year.
If you look up proleptic Gregorian calendar on Wikipedia, you'll find:
For these calendars one can distinguish two systems of numbering years BC. Bede and later historians did not use the Latin zero, nulla, as a year (see Year zero), so the year preceding AD 1 is 1 BC. In this system the year 1 BC is a leap year (likewise in the proleptic Julian calendar). Mathematically, it is more convenient to include a year 0 and represent earlier years as negative, for the specific purpose of facilitating the calculation of the number of years between a negative (BC) year and a positive (AD) year.
Therefore it is up to your interpretation wether year 0 exists or not.
Related
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);
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.
I'm using moment.js to handle my date time values in my web application. I'm using follow format: DD.MM.YYYY for example 29.09.2016. Now I have some input fields, where I can type in the day of birth of a person. When I type a short year between 62 and 99, it converts it like this for example: input = 29.09.82 it converts it after pressing tab to 29.09.2082 (puts 20 befor the 82). When I write a short year between 00 and 62 in converts it like this example: input = 29.09.55 would be converted to 29.09.1955 (puts 19 before the 55). So the first range will be convertet to a year in the 21th century and the second range would be convertet to a year in the 20th century (this seems to be default, I don't know why). Now I would like to prevent converting to a year, which is in the future (so not more than 2016, and just 2016 when the short year is 16). How can I do this? At this moment I have something like this:
if(value.isValid() && isDayOfBirth) {
value.format(myFormat);
}
Is there a way to do this or to check it? I didn't found anything in the http://momentjs.com/docs/. Thanks.
This should work.
You take the current year and check if the parsed year is greater than that.
If so, you subtract 100 years (bringing you back to 20th century).
var now = moment(),
year = now.year(); //Get current year
if (parsedDate.year() > year) {
parsedDate.subtract(100, "year");
}
am a new for moment.js
i want 3 type of outputs
Get all months from a year
get all weeks from a month in Moment js
get all days from a week
How can i do this using moment.js?
I have tried for get all month
moment().months(2011);// it's working for my year is 2011
but i have only last 2 digits of year.
moment().months(11);// It's give wrong values or my year is 11
I also read the document, they said
moment().months() Accepts numbers from 0 to 11. If the range is exceeded, it will bubble up to the year.
This problem also accrued when i use get days and weeks
How can i solve this problem in moment.js?
Use a date as parameter (12-12-2011) find year month, month week, and week day numerical values, if you need words (Monday, December) values just use format of moment.js and translations.
A bit incorrect what you do with moment().months(2011) - moment() return date time now, months(value) add value months to your moment check and see:
moment().months(2011).format("LLLL"); //result Tuesday, August 27, 2182 ...
Now read a bit about year last week here variations between (52/53).
Now the solution for your problem,
get all months of the year, dude seriously (12 months) anyway:
moment("12-26-2011", "MM-DD-YYYY").month() + 1;
get weeks of the year not of the month (you will be confuzed using week of month)
moment("12-26-2011", "MM-DD-YYYY").week();
or try this (month week):
var curr_month = moment("12-26-2011", "MM-DD-YYYY").month();
var prev_month_last_week = moment("01-01-2011", "MM-DD-YYYY").add(curr_month -1, "month").week();
var your_week_per_month = moment("12-26-2011", "MM-DD-YYYY").week() - prev_month_last_week; //from 1 to 4:
Day of the week:
moment("12-26-2011", "MM-DD-YYYY").day();
Hope it's helpful.
I'm working on a jQuery credit card expiration date validation script. Credit cards expire after the last day of the expiration month. For instance, if the card expires on 8/2013 then it's good through 8/31/2013.
In the past on the server side I've determined the last day of the month by adding 1 to the current month, then subtracting 1 day.
Today I noticed that when creating a new date, if 0 is applied to the 3rd parameter of the JavaScript Date() object, the resulting date will be the end-of-month day. But I've been unable to locate any online documentation to affirm this observation.
Here is some sample code.
var month = 10;
var year = 2013;
var expires = new Date(year, month, 0);
alert(expires);
And here is a jsFiddle example that I created.
This is a bit confusing, because I thought in JavaScript months were zero based. I've tested this in Chrome, Firefox, IE, and Safari, and the behavior appears consistent. The returned date consistently displays the last day of the month. This looks like a lucky find, but I'd really like to understand what is happening here.
Am I safe to run with this approach to assigning an end of month date, and if so is there some online documentation that I can point to which affirms this? Thanks.
Months are zero-based. That creates an end-of-month date in the previous month. Month 10 is November, so creating a date with day 0 in November gives you the end of October (month 9).
That is, day 0 in November means "the day before 1 November", which is the last day of October. Day -1 in November would be 30 October.