Use new Date() to generate a new Date object containing the current date and time.

var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
var yyyy = today.getFullYear();

today = mm + '/' + dd + '/' + yyyy;
document.write(today);

This will give you today's date in the format of mm/dd/yyyy.

Simply change today = mm +'/'+ dd +'/'+ yyyy; to whatever format you wish.

Answer from Samuel Meddows on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
When called as a constructor, returns a new Date object. When called as a function, returns a string representation of the current date and time.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-get-current-date-todays-date-in-js
JavaScript Get Current Date – Today's Date in JS
November 7, 2024 - In JavaScript, we can easily get the current date or time by using the new Date() object. By default, it uses our browser's time zone and displays the date as a full text string, such as "Fri Jun 17 2022 10:54:59 GMT+0100 (British Summer Time)" ...
🌐
W3Schools
w3schools.com › js › js_dates.asp
JavaScript Dates
Date methods and time zones are covered in the next chapters. JavaScript will (by default) output dates using the toString() method. This is a string representation of the date, including the time zone.
🌐
W3Schools
w3schools.com › jsref › jsref_tostring_date.asp
JavaScript Date toString() Method
cssText getPropertyPriority() ... d = new Date(); let text = d.toString(); Try it Yourself » · The toString() method returns a date object as a string....
🌐
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 - By default, it uses your browser's time zone and displays the date as a full-text string, including the current day, date, time, and time zone. To adjust a date by timezone in JavaScript, use the Date object and the toLocaleString() method.
🌐
Bugfender
bugfender.com › blog › javascript-date-and-time
The Definitive Guide to JavaScript Date and Time | Bugfender
February 18, 2025 - To get the current date, you can use the new Date() constructor without passing any arguments. This returns the current date and time in a long date-time representation​​. To format a JavaScript date as “yyyy-mm-dd”, you can use the ...
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Date and time
The string format should be: YYYY-MM-DDTHH:mm:ss.sssZ, where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › toString
Date.prototype.toString() - JavaScript | MDN
const event = new Date("August 19, 1975 23:15:30"); console.log(event.toString()); // Expected output: "Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)" // Note: your timezone may vary ... A string representing the given date (see description for the format).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › Date
Date() constructor - JavaScript | MDN
Any arguments given in a Date() ... or other primitive as an argument — it always returns a string representation of the current date and time. To offer protection against timing attacks and fingerprinting, the precision of new Date() might get rounded depending on browser ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-get-the-current-date-in-javascript
How to Get the Current Date in JavaScript ? - GeeksforGeeks
July 11, 2025 - The Date() object is used to access the current date in JavaScript. To get the current date in string format, we can use toDateString() method. The date.toDateString() method converts the given date object into the date portion into a string.
🌐
Scaler
scaler.com › home › topics › how to get the current date in javascript?
How to Get the Current Date in JavaScript - Scaler Topics
March 26, 2024 - The T serves as a literal to separate the date from the time, and the Z denotes zero hour offset also known as Zulu time (UTC). To get current date in javascript we will use the slice() string method.
🌐
SheCodes
shecodes.io › athena › 6172-how-to-get-current-date-in-javascript
[JavaScript] - How to Get Current Date in JavaScript - | SheCodes
Use the JavaScript Date Object and the toDateString() Method to get the current date in JavaScript.
🌐
TutorialsTeacher
tutorialsteacher.com › javascript › javascript-date
JavaScript Date: Create, Convert, Compare Dates in JavaScript
Use the new keyword in JavaScript to get the Date object. ... Date(); //Returns current date and time string //or var currentDate = new Date(); //returns date object of current date and time
🌐
W3Schools
w3schools.com › js › js_date_methods.asp
JavaScript Get Date Methods
In JavaScript, date objects are created with new Date(). new Date() returns a date object with the current date and time. ... The get methods above return Local time. Universal time (UTC) is documented at the bottom of this page.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › toDateString
Date.prototype.toDateString() - JavaScript | MDN
const event = new Date(1993, 6, 28, 14, 39, 7); console.log(event.toString()); // Expected output: "Wed Jul 28 1993 14:39:07 GMT+0200 (CEST)" // Note: your timezone may vary console.log(event.toDateString()); // Expected output: "Wed Jul 28 1993" ... A string representing the date portion of ...
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.

🌐
W3Resource
w3resource.com › javascript-exercises › javascript-basic-exercise-3.php
JavaScript: Display the current date in various format - w3resource
This JavaScript program retrieves the current date and formats it in multiple ways (mm-dd-yyyy, mm/dd/yyyy, dd-mm-yyyy, dd/mm/yyyy). It uses the Date object to get the current day, month, and year, and then adds leading zeros if necessary to ensure proper formatting. Finally, it logs the formatted date strings ...