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 Overflow
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-basic-exercise-3.php
JavaScript: Display the current date in various format - w3resource
// Get the current date var today = new Date(); // Get the day of the month var dd = today.getDate(); // Get the month (adding 1 because months are zero-based) var mm = today.getMonth() + 1; // Get the year var yyyy = today.getFullYear(); // Add leading zero if the day is less than 10 if (dd < 10) { dd = '0' + dd; } // Add leading zero if the month is less than 10 if (mm < 10) { mm = '0' + mm; } // Format the date as mm-dd-yyyy and log it today = mm + '-' + dd + '-' + yyyy...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
YYYY is the year, with four digits (0000 to 9999), or as an expanded year of + or - followed by six digits. The sign is required for expanded years. -000000 is explicitly disallowed as a valid year. MM is the month, with two digits (01 to 12). Defaults to 01.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-date-now-how-to-get-the-current-date-in-javascript
JavaScript Date Now – How to Get the Current Date in JavaScript
June 15, 2020 - The toUTCString() method returns the date in UTC timezone format: today.toUTCString(); // "Sat, 13 Jun 2020 18:30:00 GMT" The toLocaleDateString() method returns the date in a locality-sensitive format: today.toLocaleDateString(); // "6/14/2020" ...
🌐
Sling Academy
slingacademy.com › article › javascript-get-and-format-current-date-time
JavaScript: Get current date time in yyyy/MM/dd HH:mm:ss format - Sling Academy
We’ll write code from scratch without using any third-party libraries. ... getSeconds() methods to get the current year, month, date, hour, minute, and second, respectively. To make sure the month, date, hour, minute, and second always are in the two-character format, we will use the string method
🌐
PhoenixNAP
phoenixnap.com › home › kb › sysadmin › how to use javascript to get the current date and time
How to Use JavaScript to Get the Current Date and Time
December 17, 2025 - The toJSON() method returns a string representation of the date and time in a standardized format. It provides a YYYY-MM-DDTHH:MM:SS.MSSZ format where T separates the date from the time, and Z denotes the zero-hour offset (UTC).
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-current-formatted-date-dd-mm-yyyy-in-javascript
How to Get Current Formatted Date dd/mm/yyyy in JavaScript? - GeeksforGeeks
July 11, 2025 - '0' + month : month; // Format the date as dd/mm/yyyy const formattedDate = `${day}/${month}/${year}`; console.log(formattedDate); ... The Intl.DateTimeFormat() constructor is a built-in JavaScript object that enables language-sensitive date ...
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-format-date-yyyymmdd
Format a Date as YYYY-MM-DD using JavaScript | bobbyhadz
Passing a locale of sv to the toLocaleDateString method returns a Date string formatted as YYYY-MM-DD. ... All we had to do to get the date formatted as YYYYMMDD was remove the hyphens.
Find elsewhere
🌐
W3Schools
w3schools.com › js › js_date_formats.asp
JavaScript Date Formats
The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format:
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-format-javascript-date-as-yyyy-mm-dd
How To Format JavaScript Date as yyyy-mm-dd? - GeeksforGeeks
June 24, 2025 - Here's how you can use toLocaleDateString() instead, which provides a more accurate result, taking into account the local time zone. Example: In this example, we will use the iso string method to format the JavaScript date as yyyy-mm-dd ... ...
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-format-javascript-date-as-yyyy-mm-dd.php
How to Format JavaScript Date as YYYY-MM-DD
// Create a date object from a ...ng("default", { day: "2-digit" }); // Generate yyyy-mm-dd date string var formattedDate = year + "-" + month + "-" + day; console.log(formattedDate); // Prints: 2022-05-04...
🌐
Medium
medium.com › @Saf_Bes › get-current-date-in-format-yyyy-mm-dd-with-javascript-9c898d1d5b22
Get current date in format yyyy-mm-dd with Javascript | by Saf Bes | Medium
April 10, 2020 - Get current date in format yyyy-mm-dd with Javascript · To get the current date in javascript, I use Intl.DateTimeFormat · new Intl.DateTimeFormat("fr-CA", {year: "numeric", month: "2-digit", day: "2-digit"}).format(Date.now()) the expected output : 2019-11-20 ·
🌐
freeCodeCamp
freecodecamp.org › news › javascript-get-current-date-todays-date-in-js
JavaScript Get Current Date – Today's Date in JS
November 7, 2024 - var date = moment(); var currentDate = date.format('D/MM/YYYY'); console.log(currentDate); // "17/06/2022"
🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-date
JavaScript Date: Create, Convert, Compare Dates in JavaScript
</p> <div className="card code-panel"> <div className="card-header example-title">Example: Date Conversion in Different Formats</div> <div className="panel-body"><pre className="csharpcode"><code>var date = new Date('2015-02-10T10:12:50.5000z'); date; 'Default format:' date.toDateString();'Tue Feb 10 2015' date.toLocaleDateString();'2/10/2015' date.toGMTString(); 'GMT format' date.toISOString(); '2015-02-10T10:12:50.500Z' date.toLocaleString();'Local date Format ' date.toLocaleTimeString(); 'Locale time format ' date.toString('YYYY-MM-dd'); 'Tue Feb 10 2015 15:42:50' date.toTimeString(); '15:42:50' date.toUTCString(); 'UTC format '
🌐
Squash
squash.io › how-to-format-javascript-date-as-yyyy-mm-dd
How to Format JavaScript Dates as YYYY-MM-DD
Save the above code in a JavaScript file, for example, formatDate.js, and run it using Node.js: ... This will output the current date in the YYYY MM DD format.
🌐
GitHub
gist.github.com › Ivlyth › c4921735812dd2c0217a
format javascript date to format "YYYY-mm-dd HH:MM:SS" · GitHub
const ISO = (timeStamp=Date.now()) => { return new Date(timeStamp - (new Date().getTimezoneOffset() * 60 * 1000)).toISOString().slice(0,-5).split('T') } console.log('one->',ISO(new Date('2020-09-26 11:28:55').getTime())) console.log('now->',ISO()) one-> [ '2020-09-26', '11:28:55' ] now-> [ '2021-09-26', '20:13:36' ] js 时间格式化 date format YYYY-mm-dd HH:MM:SS ·
🌐
Latenode
community.latenode.com › other questions › javascript
Convert date in JavaScript to yyyy-mm-dd format - JavaScript - Latenode Official Community
December 3, 2024 - I possess a date string in the form Sun May 11, 2014. What is the best way to transform it into the format 2014-05-11 using JavaScript? function formatDate(inputDate) { const dateObj = new Date(inputDate); const year = dateObj.getFullYear(); const month = String(dateObj.getMonth() + 1).padStart(2, '0'); const day = String(dateObj.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; } const date = 'Sun May 11, 2014'; console.log(formatDate(date)); The code snippet...
Top answer
1 of 16
789

.getMonth() returns a zero-based number so to get the correct month you need to add 1, so calling .getMonth() in may will return 4 and not 5.

So in your code we can use currentdate.getMonth()+1 to output the correct value. In addition:

  • .getDate() returns the day of the month <- this is the one you want
  • .getDay() is a separate method of the Date object which will return an integer representing the current day of the week (0-6) 0 == Sunday etc

so your code should look like this:

var currentdate = new Date(); 
var datetime = "Last Sync: " + currentdate.getDate() + "/"
                + (currentdate.getMonth()+1)  + "/" 
                + currentdate.getFullYear() + " @ "  
                + currentdate.getHours() + ":"  
                + currentdate.getMinutes() + ":" 
                + currentdate.getSeconds();

JavaScript Date instances inherit from Date.prototype. You can modify the constructor's prototype object to affect properties and methods inherited by JavaScript Date instances

You can make use of the Date prototype object to create a new method which will return today's date and time. These new methods or properties will be inherited by all instances of the Date object thus making it especially useful if you need to re-use this functionality.

// For todays date;
Date.prototype.today = function () { 
    return ((this.getDate() < 10)?"0":"") + this.getDate() +"/"+(((this.getMonth()+1) < 10)?"0":"") + (this.getMonth()+1) +"/"+ this.getFullYear();
}

// For the time now
Date.prototype.timeNow = function () {
     return ((this.getHours() < 10)?"0":"") + this.getHours() +":"+ ((this.getMinutes() < 10)?"0":"") + this.getMinutes() +":"+ ((this.getSeconds() < 10)?"0":"") + this.getSeconds();
}

You can then simply retrieve the date and time by doing the following:

var newDate = new Date();
var datetime = "LastSync: " + newDate.today() + " @ " + newDate.timeNow();

Or call the method inline so it would simply be -

var datetime = "LastSync: " + new Date().today() + " @ " + new Date().timeNow();
2 of 16
548

To get time and date you should use

    new Date().toLocaleString();

>> "09/08/2014, 2:35:56 AM"

To get only the date you should use

    new Date().toLocaleDateString();

>> "09/08/2014"

To get only the time you should use

    new Date().toLocaleTimeString();

>> "2:35:56 AM"

Or if you just want the time in the format hh:mm without AM/PM for US English

    new Date().toLocaleTimeString('en-US', { hour12: false, 
                                             hour: "numeric", 
                                             minute: "numeric"});
>> "02:35"

or for British English

    new Date().toLocaleTimeString('en-GB', { hour: "numeric", 
                                             minute: "numeric"});

>> "02:35"

Read more here.

🌐
Bugfender
bugfender.com › blog › javascript-date-and-time
The Definitive Guide to JavaScript Date and Time | Bugfender
February 18, 2025 - To format a JavaScript date as “yyyy-mm-dd”, you can use the Date object and extract the year, month, and day components, then concatenate them into a string with the desired format.