Semantically, you're probably looking for the one-liner

new Date().toLocaleString()

which formats the date in the locale of the user.

If you're really looking for a specific way to format dates, I recommend the moment.js library.

Answer from Andrew Mao on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Date
Date - JavaScript - MDN Web Docs - Mozilla
Consider using Temporal for new code and migrate existing code over to it if possible (check the browser compatibility. We will be writing a usage guide soon! A JavaScript date is fundamentally specified as the time in milliseconds that has elapsed since the epoch, which is defined as the midnight ...
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Guide โ€บ Representing_dates_times
Representing dates & times - JavaScript - MDN Web Docs
JavaScript does not have a date data type. However, you can use the Date object and its methods to work with dates and times in your applications. The Date object has a large number of methods for setting, getting, and manipulating dates. It does not have any properties.
Discussions

How to get datetime in JavaScript? - Stack Overflow
How to get date time in JavaScript with format 31/12/2010 03:55 AM? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Convert datetime to valid JavaScript date - Stack Overflow
I have a datetime string being provided to me in the following format: ... When attempting to parse it into a JavaScript date() object it fails. What is the best way to convert this into a format that JavaScript can understand? ... Which is what I was using. It appears this may be a browser issue. I've made a http://jsfiddle... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do we work with the date and datetime in JS?
Depending on how the date data is being used, could possibly just store it as a string. That'll bite you if you need to do any kind of math on it though. More on reddit.com
๐ŸŒ r/webdev
32
8
December 14, 2021
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
๐ŸŒ
JavaScript.info
javascript.info โ€บ tutorial โ€บ the javascript language โ€บ data types
Date and time
Node.js has microtime module and other ways. Technically, almost any device and environment allows to get more precision, itโ€™s just not in Date.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_obj_date.asp
JavaScript Date Reference
<a> <abbr> <address> <area> <article> <aside> <audio> <b> <base> <bdo> <blockquote> <body> <br> <button> <canvas> <caption> <cite> <code> <col> <colgroup> <datalist> <dd> <del> <details> <dfn> <dialog> <div> <dl> <dt> <em> <embed> <fieldset> <figcaption> <figure> <footer> <form> <head> <header> <h1> - <h6> <hr> <html> <i> <iframe> <img> <ins> <input> button <input> checkbox <input> color <input> date <input> datetime <input> datetime-local <input> email <input> file <input> hidden <input> image <input> month <input> number <input> password <input> radio <input> range <input> reset <input> sear
๐ŸŒ
Toptal
toptal.com โ€บ software โ€บ definitive-guide-to-datetime-manipulation
How to get the Timestamp And Date in Javascript: JS DateTime | Toptalยฎ
January 16, 2026 - It offers convenient methods to get the current time in JavaScript, store a date in a variable, perform date arithmetic, and format the date based on the userโ€™s locale. Due to differences between browser implementations and incorrect handling of Daylight Savings Time (DST), depending on the Date object for mission-critical applications is not recommended and you should probably be using a DateTime library like Luxon, date-fns, or dayjs. (Whatever you use, avoid the once-popular Moment.jsโ€”often simply called moment, as it appears in codeโ€”since itโ€™s now deprecated.)
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_dates.asp
JavaScript Dates
February 4, 2026 - JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Website JS Syllabus JS Study Plan JS Interview Prep JS Bootcamp JS Certificate JS Reference ... Date objects are static. The "clock" is not "running". The computer clock is ticking, date objects are not. By default, JavaScript will use the browser's time zone and display a date as a full text string:
Find elsewhere
Top answer
1 of 8
49

The failure is up to the browser implementation's handling of Date.parse and how they allow for date strings not conform the date standards

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 calendar date extended format.
The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

So we can force your string into this

Copyconst getDateFromString = str => {
  const [date,time] = str.split(" ");
  // reformat string into YYYY-MM-DDTHH:mm:ss.sssZ
  str = `${date}T${time}.000Z`
  return new Date(str);
};
let date = getDateFromString('2011-07-14 11:23:00');
console.log(date)
Run code snippetEdit code snippet Hide Results Copy to answer Expand


Older answer:

As for your format, some OSX browsers insist on the dashes being slashes.

This works everywhere including Safari 5 and Firefox 5 on OS X.

UPDATE: Fx Quantum (54) has no need for the replace, but Safari 11 is still not happy unless you convert as below

Copyvar date_test = new Date("2011-07-14 11:23:00".replace(/-/g,"/"));
console.log(date_test);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

2 of 8
5

One can use the getmonth and getday methods to get only the date.

Here I attach my solution:

Copyvar fullDate = new Date(); console.log(fullDate);
var twoDigitMonth = fullDate.getMonth() + "";
if (twoDigitMonth.length == 1)
    twoDigitMonth = "0" + twoDigitMonth;
var twoDigitDate = fullDate.getDate() + "";
if (twoDigitDate.length == 1)
    twoDigitDate = "0" + twoDigitDate;
var currentDate = twoDigitDate + "/" + twoDigitMonth + "/" + fullDate.getFullYear(); console.log(currentDate);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

๐ŸŒ
Bugfender
bugfender.com โ€บ blog โ€บ javascript-date-and-time
The Definitive Guide to JavaScript Date and Time | Bugfender
February 18, 2025 - The JavaScript Date Object allows you to manipulate, format, and compare JavaScript dates and times. It is used for converting between Date JS objects and epoch timestamps and performing other advanced operationsโ€‹.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_date_methods.asp
JavaScript Get Date Methods
JS Examples JS HTML DOM JS HTML ... Prep JS Bootcamp JS Certificate JS Reference ... In JavaScript, date objects are created with new Date()....
๐ŸŒ
Reddit
reddit.com โ€บ r/webdev โ€บ how do we work with the date and datetime in js?
r/webdev on Reddit: How do we work with the date and datetime in JS?
December 14, 2021 -

As far as I know the database stores the datetimes in UTC format.

We have some datepickers and datetimepickers, we want to show the exact time and date the user selected, no matter where the user is, so we don't care about timezones, however when parsing the dates, such as

let date = new Date('2021-12-14')

Sometimes it'll parse as December 13th instead of 14th depending on my timezone.
How can I parse it without caring about the timezone, and how should I send it to the backend?

As I said this is completely timezone-agnostic, I just want to keep the exact correct date, same applies with the time for timepickers, I don't want to convert to local, I just want to keep the exact time the user selected for everything.

๐ŸŒ
CSS-Tricks
css-tricks.com โ€บ everything-you-need-to-know-about-date-in-javascript
Everything You Need to Know About Date in JavaScript | CSS-Tricks
September 22, 2022 - If you want to format several Date objects with the same formatting options, you should use Intl.DateTimeFormat instead: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat With this, your example would be: const localDateString = new Intl.DateTimeFormat("en-US", { year: 'numeric', month: 'long', weekday: 'long' }).format; ... This is great! Thank you for this, this clarified a lot of stuff regarding dates. Even though I love moment.js I might start implementing my own date manipulations!
Top answer
1 of 16
790

.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
549

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.

๐ŸŒ
date-fns
date-fns.org
date-fns - modern JavaScript date utility library
date-fns provides the most comprehensive yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.
๐ŸŒ
openHAB Community
community.openhab.org โ€บ setup, configuration and use โ€บ beginners
JavaScript Date from DateTime item - Beginners - openHAB Community
April 18, 2025 - Dear all, I am seeking to convert the state of a DateTime item to a JavaScript Date object, ignoring the time component of the DateTime. Is there a simple one-liner to do this? So far I failed. Thanks!
๐ŸŒ
TutorialsTeacher
tutorialsteacher.com โ€บ javascript โ€บ javascript-date
JavaScript Date: Create, Convert, Compare Dates in JavaScript
Learn how to work with the Dates in JavaScript. JavaScript provides Date object to work with date & time including days, months, years, hours, minutes, seconds and milliseconds.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ understanding-date-and-time-in-javascript
Understanding Date and Time in JavaScript | DigitalOcean
August 25, 2021 - JavaScript comes with the built in Date object and related methods. This tutorial will go over how to format and use date and time in JavaScript.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ date-time
JavaScript Date and Time
July 6, 2020 - The Date object provides the date and time information and also provides various methods. A JavaScript date defines the EcmaScript epoch that represents milliseconds since 1 January 1970 UTC.
๐ŸŒ
Phrase
phrase.com โ€บ home โ€บ resources โ€บ blog โ€บ what is the best javascript date and time library?
What Is the Best JavaScript Date and Time Library? | Phrase
January 7, 2025 - ๐Ÿ”— Resource ยป Check out our ... ready for users across the globe. ... Datejs is an open-source JavaScript Date Library for parsing, formatting, and processing DateTime objects....
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Date โ€บ parse
Date.parse() - JavaScript - MDN Web Docs
// Standard date-time string format const unixTimeZero = Date.parse("1970-01-01T00:00:00Z"); // Non-standard format resembling toUTCString() const javaScriptRelease = Date.parse("04 Dec 1995 00:12:00 GMT"); console.log(unixTimeZero); // Expected output: 0 console.log(javaScriptRelease); // Expected output: 818035920000 ยท js ยท