111 Votes

JavaScript: Get current Date in YYYY-MM-DD Format

Question by Guest | 2015-10-04 at 18:58

Is there are any very simple method to get the current date in the format YYYY-MM-DD using JavaScript? I mean the date as 4-digit year and 2-digit month and day with leading zeros?

All solutions, I have found so far, are very cumbersome and need 

much code. Isn't there any quick and easy way out of there?

ReplyPositiveNegative
12Best Answer24 Votes

For making it simple, you can just use a little trick by using the function  .toISOString() with cutting the result. Here is how to do it:

var d = new Date().toISOString().slice(0,10); 

alert(d);

First of all, we are creating a new date object with "new Date()" containing the current time. After that, we are converting the time with .toISOString() into the format YYYY-MM-DDTHH:mm:ss.sssZ. And because we only need the first part of this string, we are last using .slice(0,10) to get the first 10 characters of the string.

By the way, if you would like to have more flexibility, you can have a look at the tutorial about determining time and date using JavaScript.
2015-10-04 at 20:29

ReplyPositive Negative
Reply

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

PHP: Determine Week Number

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.