.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();
Answer from Mark Walters on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › now
Date.now() - JavaScript | MDN
You can use Date.now() to get the current time in milliseconds, then subtract a previous time to find out how much time elapsed between the two calls.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-get-current-date-todays-date-in-js
JavaScript Get Current Date – Today's Date in JS
November 7, 2024 - But if you already have a library installed on your project or you're using it for other operations in your application, you can use it. In JavaScript, we can easily get the current date or time by using the new Date() object.
Discussions

Getting current date and time in JavaScript - Stack Overflow
I have a script that prints the current date and time in JavaScript, but the DATE is always wrong. Here is the code: var currentdate = new Date(); var datetime = "Last Sync: " + currentdate. More on stackoverflow.com
🌐 stackoverflow.com
How do I get the current date in JavaScript? - Stack Overflow
Moment is overkill for just getting the current date. 2014-10-27T11:15:16.31Z+00:00 ... Or moment().format("L") to respect the current locale. 2017-04-18T14:56:51.267Z+00:00 ... @DanDascalescu Actually, the Javascript base specification for DateTime is that bad. More on stackoverflow.com
🌐 stackoverflow.com
Working with Dates in JavaScript in an Easy Way
be aware that the date it pulls is based on your computers time. if you have the ability, pull the date from the server to keep it correct no matter what your user will do to their clock. More on reddit.com
🌐 r/learnjavascript
3
0
November 15, 2023
Getting the current Datetime in javascript - Mirth Community
Hi Everyone, I don't know if this forum is the right place to put this. Sometime you will need the current date time in javascript this is a real pain. I mean you can get it very easy like this: More on forums.mirthproject.io
🌐 forums.mirthproject.io
February 7, 2011
🌐
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.
🌐
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.
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 - The simplest and most effective method to get the current date into a JavaScript string is using the toLocalDateString() method from the Date object.
Find elsewhere
🌐
Reddit
reddit.com › r/learnjavascript › working with dates in javascript in an easy way
r/learnjavascript on Reddit: Working with Dates in JavaScript in an Easy Way
November 15, 2023 -

Source: https://www.webdevmonk.com/tutorials/js-tutorial-in-45minutes.html#14

You can create a new Date object by calling the Date constructor with no arguments, which will create a Date object set to the current date and time:

let currentDate = new Date();

Once you have a Date object, you can use its methods to get and set the date and time.

let currentDate = new Date();

console.log(currentDate.getFullYear()); // returns the year (e.g. 2022)

console.log(currentDate.getMonth()); // returns the month (0-11)

console.log(currentDate.getDate()); // returns the day of the month (1-31)

console.log(currentDate.getHours()); // returns the hour (0-23)

console.log(currentDate.getMinutes()); // returns the minute (0-59)

console.log(currentDate.getSeconds()); // returns the second (0-59)

currentDate.setFullYear(2021); // sets the year to 2021

currentDate.setMonth(11); // sets the month to December (11)

currentDate.setDate(25); // sets the day of the month to 25

currentDate.setHours(12); // sets the hour to 12 (noon)

currentDate.setMinutes(0); // sets the minute to 0

currentDate.setSeconds(0); // sets the second to 0

You can also create a Date object for a specific date and time by passing arguments to the Date constructor.

let birthday = new Date(1995, 0, 1); // January 1st, 1995

let meeting = new Date(2022, 11, 16, 13, 30, 0); // December 16th, 2022, 1:30 PM

🌐
CoreUI
coreui.io › answers › how-to-get-the-current-date-in-javascript
How to get the current date in JavaScript · CoreUI
October 1, 2025 - From my expertise, the most ... representing the current moment in the user’s local timezone. Use the new Date() constructor to get the current date and time....
🌐
Medium
medium.com › @python-javascript-php-html-css › fetching-the-current-date-in-javascript-d290022df852
Obtaining the Current Date in JavaScript
August 24, 2024 - Yes, use Date.now() to get the current timestamp in milliseconds. ... Convert both dates to timestamps using getTime() and then compare the numeric values. How can I get the day of the week for a specific date?
🌐
Mirth Community
forums.mirthproject.io › home › mirth connect › support
Getting the current Datetime in javascript - Mirth Community
February 7, 2011 - I use the following code: // Get current date with formatting var curdate = DateUtil.getCurrentDate('yyyyMMddhhmm'); //Formatting options // yyyy year (four-digit) // M, MM month, month with leading 0 // d, dd day, day with leading 0 // HH hour, 24-hour-format // hh hour, 12-hour-format // ...
🌐
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 - // Get current date const today = new Date(); // Extract day, month, and year let day = today.getDate(); let month = today.getMonth() + 1; let year = today.getFullYear(); // Add leading zero to day and month if needed day = day < 10 ?
🌐
Sentry
sentry.io › sentry answers › javascript › how do i get the current date in javascript?
How do I get the Current Date in JavaScript? | Sentry
December 15, 2022 - You can also create a new Date object, which represents a single moment in time, and save the date as a UTC timezone timestamp: ... const currentTime = new Date(); console.log(currentTime.toUTCString()); // Mon, 28 Nov 2022 11:24:21 GMT
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › Date
Date() constructor - JavaScript | MDN
When called as a function, it returns a string representing the current time. const date1 = new Date("December 17, 1995 03:24:00"); // Sun Dec 17 1995 03:24:00 GMT... const date2 = new Date("1995-12-17T03:24:00"); // Sun Dec 17 1995 03:24:00 GMT... console.log(date1.getTime() === date2.getTime()); // Expected output: true
🌐
Uri
students.cs.uri.edu › ~forensics › courses › CSC101 › readings › javascript › date.html
Current Date
The now.getDay gets the day from the now object that we created in the header script. It will return a numerical value between 0 and 6 which will represent the day of the week. The dayName[now.getDay()] command uses the dayName array that we created in the header script to display the correct ...
🌐
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 - Using the Date() object from the examples above outputs the system time on the machine you are currently using. 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.
🌐
Vultr Docs
docs.vultr.com › javascript › examples › display-current-date
JavaScript Program to Display Current Date | Vultr Docs
December 18, 2024 - Use JavaScript date methods to customize the display format. Manipulate the output to match a more common date format like "Day, Month Date, Year". ... let date = new Date(); let dayOfWeek = date.toLocaleString('default', { weekday: 'long' }); let month = date.toLocaleString('default', { month: 'long' }); let dayOfMonth = date.getDate(); let year = date.getFullYear(); let formattedDate = `${dayOfWeek}, ${month} ${dayOfMonth}, ${year}`; console.log(formattedDate); Explain Code
🌐
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.