Copy("0" + this.getDate()).slice(-2)
for the date, and similar:
Copy("0" + (this.getMonth() + 1)).slice(-2)
for the month.
Answer from Hugo on Stack OverflowHow do I get Month and Date of JavaScript in 2 digit format? - Stack Overflow
date - why does javascript getMonth count from 0 and getDate count from 1? - Stack Overflow
javascript - Get month name from Date - Stack Overflow
I just found that Javascript's built in Date.getMonth() method is zero indexed, meaning it returns "0" for January, "1", for February, etc.
Videos
Copy("0" + this.getDate()).slice(-2)
for the date, and similar:
Copy("0" + (this.getMonth() + 1)).slice(-2)
for the month.
Why not use padStart ?
padStart(targetLength, padString) where
targetLengthis2padStringis0
Copy// Source: https://stackoverflow.com/a/50769505/2965993
var dt = new Date();
year = dt.getFullYear();
month = (dt.getMonth() + 1).toString().padStart(2, "0");
day = dt.getDate().toString().padStart(2, "0");
console.log(year + '/' + month + '/' + day);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
This will always return 2 digit numbers even if the month or day is less than 10.
Notes:
- This will only work with Internet Explorer if the js code is transpiled using babel.
getFullYear()returns the 4 digit year and doesn't requirepadStart.getMonth()returns the month from 0 to 11.- 1 is added to the month before padding to keep it 1 to 12.
getDate()returns the day from 1 to 31.- The 7th day will return
07and so we do not need to add 1 before padding the string.
- The 7th day will return
I assume it's because it would be easier to reference in an array of names, i.e.
var months = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];
var d = new Date();
var namedMonth = months[d.getMonth()];
If getMonth() returned 1-12, then programmers would have to do d.getMonth()-1 everytime they wanted a fancy named month.
Days of the month don't have specific "names" per se. The getDate() returns 1-(28-31). We usually just refer to them by their number.
The same concept as getMonth() applies for getDay() also, which returns 0-6 based on the day of the week
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var namedDay = days[d.getDay()];
All this returns something like:
console.log("Month: month[" + d.getMonth() + "]: " + namedMonth);
//Month: month[3]: April
console.log("Day: days[" + d.getDay() + "]: " + namedDay);
// Day: days[4] : Thursday
Coming a bit late to this, but the correct answer is here:
https://stackoverflow.com/a/41992352/134120
They (the creators of JavaScript) copied the functionality from the corresponding class in Java (which in turn seems to have been copied from C). And so we're propagating the mistakes of the past
It is now possible to do this with the ECMAScript Internationalization API:
Copyconst date = new Date(2009, 10, 10); // 2009-11-10
const month = date.toLocaleString('default', { month: 'long' });
console.log(month);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
'long' uses the full name of the month, 'short' for the short name, and 'narrow' for a more minimal version, such as the first letter in alphabetical languages.
You can change the locale from browser's 'default' to any that you please (e.g. 'en-us'), and it will use the right name for that language/country.
With toLocaleStringapi you have to pass in the locale and options each time. If you are going do use the same locale info and formatting options on multiple different dates, you can use Intl.DateTimeFormat instead:
Copyconst formatter = new Intl.DateTimeFormat('fr', { month: 'short' });
const month1 = formatter.format(new Date());
const month2 = formatter.format(new Date(2003, 5, 12));
console.log(`${month1} and ${month2}`); // current month in French and "juin".
Run code snippetEdit code snippet Hide Results Copy to answer Expand
For more information see my blog post on the Internationalization API.
Shorter version:
Copyconst monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const d = new Date();
document.write("The current month is " + monthNames[d.getMonth()]);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Note (2019-03-08) - This answer by me which I originally wrote in 2009 is outdated. See David Storey's answer for a better solution.
Of coures, Date.getDate() (which returns the number day) is not zero indexed (1-31). Also, empty lists in Javascript are truthy, but empty strings evaluate to false. The. Fuck.
JS is so convenient and I definitely try not to take it for granted, but its quirks are truly something else. Anyone got some other good ones?