How do I get a date in YYYY-MM-DD format?

OneStig

Normally if I wanted to get the date I could just do something like

var d = new Date(); console.log(d);

The problem with doing that, is when I run that code, it returns:

Mon Aug 24 2015 4:20:00 GMT-0800 (Pacific Standard Time)

How could I get the Date() method to return a value in a "MM-DD-YYYY" format so it would return something like:

8/24/2015

Or, maybe MM-DD-YYYY H:M

8/24/2016 4:20

leaksterrr

Just use the built-in .toISOString() method like so: toISOString().split('T')[0]. Simple, clean and all in a single line.

var date = (new Date()).toISOString().split('T')[0];
document.getElementById('date').innerHTML = date;
<div id="date"></div>

Please note that the timezone of the formatted string is UTC rather than local time.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get the current Date/time in DD/MM/YYYY HH:MM format?

How do I get the next day's date in JS in YYYY-MM-DD format?

How do I display a date format in dd/MM/yyyy to ddd, dd/MM/yyyy (Mon, 1/4/2014) format with this logic?

How do I convert String date to Date format date in dd-MM-yyyy format in java

How would I transform minute date ( MM/DD/YYYY HH:MM) into the format (MM/DD/YYYY)

What date format is this and how do I convert it into a MM-DD-YYYY format and vice versa

how can I convert date(YYYY, MM, DD) format to YYYY/MM/DD with python?

how to i change the format of date from dd-mm-yyyy to dd/mm/yyyy in a csv file

How to get only the git date format as YYYY/MM/DD?

How to get Time from Mysql Date Format DD/MM/YYYY?

How to get dd-mm-yyyy format date in datatable laravel

How do you get last weeks date in swift in YYYY-MM-DD format?

How do I extract current date, month and year in the format yyyy-mm-dd in karate dsl?

How do I convert "dd.mm.yyyy" string to date format in golang?

How can I put Date type parameter as a String in GET request? Format "yyyy-MM-dd"

How can i get data in format "YYYY-MM-DD 00:00:00.0" using class Date?

How can i get date string from Date in "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ" format?

How do I format {{$timestamp}} as MM/DD/YYYY in Postman?

How do I change format datetime in HTML to dd/mm/yyyy

How to format date in MM/DD/YYYY in Delphi

django how to format date to DD/MM/YYYY

How can I convert date format from YYYY-MM-DD to YYYY-MM in sql

How do I cast dd/mm/yyyy string into date in BigQuery?

How do I check date validity in MM/dd/yyyy in Java?

How do I enter dates in ISO 8601 date format (YYYY-MM-DD) in Excel (and have Excel recognize that format as a date value)?

How do I get the 1-day previous date of a dd/mm/yyyy string in PHP?

How do I get 'date-1' formatted as mm-dd-yyyy using PowerShell?

Java How to get date format : dd/MM/yyyy HH:mm:ss.SS” with Date variable

How can I avoid getting this error while parsing an input date with this format dd/mm/yyyy to yyyy-mm-dd?