Just leverage the built-in toISOString method that brings your date to the ISO 8601 format:
let yourDate = new Date()
yourDate.toISOString().split('T')[0]
Where yourDate is your date object.
Edit: @exbuddha wrote this to handle time zone in the comments:
const offset = yourDate.getTimezoneOffset()
yourDate = new Date(yourDate.getTime() - (offset*60*1000))
return yourDate.toISOString().split('T')[0]
Answer from Darth Egregious on Stack OverflowJust leverage the built-in toISOString method that brings your date to the ISO 8601 format:
let yourDate = new Date()
yourDate.toISOString().split('T')[0]
Where yourDate is your date object.
Edit: @exbuddha wrote this to handle time zone in the comments:
const offset = yourDate.getTimezoneOffset()
yourDate = new Date(yourDate.getTime() - (offset*60*1000))
return yourDate.toISOString().split('T')[0]
You can do:
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
console.log(formatDate('Sun May 11,2014'));
Usage example:
console.log(formatDate('Sun May 11,2014'));
Output:
2014-05-11
Demo on JSFiddle: http://jsfiddle.net/abdulrauf6182012/2Frm3/
Convert date in JavaScript to yyyy-mm-dd format
Weird JavaScript date problem: new Date('MM/dd/yyyy') !== new Date('yyyy-MM-dd')
How can I format a date to MM/dd/yyyy in JavaScript? - TestMu AI Community
How to convert date to dd-mm-yyyy in react
Videos
Some day, someone will invent a JavaScript compiler that handles dates in a consistent manner. Sadly, today is not that day. I'm having an issue where two identical dates that are parsed from differently formatted strings are producing two different results.
For example,
new Date('08/01/2014')will result in a value of "Fri Aug 01 2014 00:00:00 GMT-0400 (Eastern Daylight Time)", as expected.
However,
new Date('2014-08-01')gives "Thu Jul 31 2014 20:00:00 GMT-0400 (Eastern Daylight Time)". An offset of 4 hours. Basically, what happens is that using the MM/dd/yyyy format uses the local timezone and yyyy-MM-dd uses UTC.
Now the weirder part: appending midnight, "00:00:00", to the date value gives the expected result regardless of which of the two formats are used.
This happens in Chrome, Firefox, and IE. I have not tested Safari and Opera but, since they both use Webkit, I expect the same problem.
I can code around this easily enough but does anyone know the reasoning behind this?
Call the toISOString() method:
var dt = new Date("30 July 2010 15:05 UTC");
document.write(dt.toISOString());
// Output:
// 2010-07-30T15:05:00.000Z
toISOString() will return current UTC time only not the current local time. If you want to get the current local time in yyyy-MM-ddTHH:mm:ss.SSSZ format then you should get the current time using following two methods
Method 1:
console.log(new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString());
Method 2:
console.log(new Date(new Date().getTime() - new Date().getTimezoneOffset() * 60000).toISOString());
I'm going insane. I had this feature working perfectly. Took 2 weeks vacation. I come back and it's broken.
I know y'all will say this is impossible, but I was getting that format by using `date.toLocaleDateString('en-CA')`. I know the spec says that format is "dd/MM/yyyy", which isn't what I want, but I was giving me the format in the title, I swear to God.
This is such a stupid little thing but I've already spent hours on SO. It's just endless threads about people confused about datetimes and the differences between timezones and offsets and no one's talking about this silly little thing.
I have my new date: Wed Feb 08 2023 00:00:00 GMT-0500 (Eastern Standard Time)
I just need a simple operation to flip the string around so it becomes 2023-03-08
That is it. Please help
p.s. the date is always set to midnight user local time so offset can be ignored. Whatever day they're experiencing is the day to be formatted. Thank you
I hope this is what you want:
const today = new Date();
const yyyy = today.getFullYear();
let mm = today.getMonth() + 1; // Months start at 0!
let dd = today.getDate();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
const formattedToday = dd + '/' + mm + '/' + yyyy;
document.getElementById('DATE').value = formattedToday;
How do I get the current date in JavaScript?
I honestly suggest that you use moment.js. Just download moment.min.js and then use this snippet to get your date in whatever format you want:
<script>
$(document).ready(function() {
// set an element
$("#date").val( moment().format('MMM D, YYYY') );
// set a variable
var today = moment().format('D MMM, YYYY');
});
</script>
Use following chart for date formats:
