You may want to try

var d = new Date();
d.toLocaleString();       // -> "2/1/2013 7:37:08 AM"
d.toLocaleDateString();   // -> "2/1/2013"
d.toLocaleTimeString();  // -> "7:38:05 AM"

Documentation

Answer from Ye Lin Aung on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
The format is as follows: ... 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).
Top answer
1 of 16
277

You may want to try

var d = new Date();
d.toLocaleString();       // -> "2/1/2013 7:37:08 AM"
d.toLocaleDateString();   // -> "2/1/2013"
d.toLocaleTimeString();  // -> "7:38:05 AM"

Documentation

2 of 16
219

A JavaScript Date has several methods allowing you to extract its parts:

getFullYear() - Returns the 4-digit year
getMonth() - Returns a zero-based integer (0-11) representing the month of the year.
getDate() - Returns the day of the month (1-31).
getDay() - Returns the day of the week (0-6). 0 is Sunday, 6 is Saturday.
getHours() - Returns the hour of the day (0-23).
getMinutes() - Returns the minute (0-59).
getSeconds() - Returns the second (0-59).
getMilliseconds() - Returns the milliseconds (0-999).
getTimezoneOffset() - Returns the number of minutes between the machine local time and UTC.

There are no built-in methods allowing you to get localized strings like "Friday", "February", or "PM". You have to code that yourself. To get the string you want, you at least need to store string representations of days and months:

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

Then, put it together using the methods above:

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var d = new Date();
var day = days[d.getDay()];
var hr = d.getHours();
var min = d.getMinutes();
if (min < 10) {
    min = "0" + min;
}
var ampm = "am";
if( hr > 12 ) {
    hr -= 12;
    ampm = "pm";
}
var date = d.getDate();
var month = months[d.getMonth()];
var year = d.getFullYear();
var x = document.getElementById("time");
x.innerHTML = day + " " + hr + ":" + min + ampm + " " + date + " " + month + " " + year;
<span id="time"></span>

I have a date format function I like to include in my standard library. It takes a format string parameter that defines the desired output. The format strings are loosely based on .Net custom Date and Time format strings. For the format you specified the following format string would work: "dddd h:mmtt d MMM yyyy".

var d = new Date();
var x = document.getElementById("time");
x.innerHTML = formatDate(d, "dddd h:mmtt d MMM yyyy");

Demo: jsfiddle.net/BNkkB/1

Here is my full date formatting function:

const MMMM = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const MMM = MMMM.map(m => m.slice(0, 3));
const dddd = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
const ddd = dddd.map(d => d.slice(0, 3));

function ii(i, len = 2) {
    return (i + "").padStart(len, "0");
}

function tzHHMM(tz) {
    const sign = tz > 0 ? "-" : "+"; // +08:00 == -480, signs are reversed
    const tzv = Math.abs(tz);
    const tzHrs = Math.floor(tzv / 60);
    const tzMin = tzv % 60;
    return sign + ii(tzHrs) + ":" + ii(tzMin);
}

function formatDate(date, format, utc) {
    const y = utc ? date.getUTCFullYear() : date.getFullYear();
    const M = utc ? date.getUTCMonth() : date.getMonth();
    const d = utc ? date.getUTCDate() : date.getDate();
    const H = utc ? date.getUTCHours() : date.getHours();
    const h = H > 12 ? H - 12 : H == 0 ? 12 : H;
    const m = utc ? date.getUTCMinutes() : date.getMinutes();
    const s = utc ? date.getUTCSeconds() : date.getSeconds();
    const f = utc ? date.getUTCMilliseconds() : date.getMilliseconds();
    const TT = H < 12 ? "AM" : "PM";
    const tt = TT.toLowerCase();
    const day = utc ? date.getUTCDay() : date.getDay();
    const replacements = {
        y,
        yy: y.toString().slice(-2),
        yyy: y,
        yyyy: y,
        M,
        MM: ii(M),
        MMM: MMM[M],
        MMMM: MMMM[M],
        d,
        dd: ii(d),
        ddd: ddd[day],
        dddd: dddd[day],
        H,
        HH: ii(H),
        h,
        hh: ii(h),
        m,
        mm: ii(m),
        s,
        ss: ii(s),
        f: Math.round(f / 100),
        ff: ii(Math.round(f / 10)),
        fff: ii(f, 3),
        ffff: ii(f * 10, 4),
        T: TT[0],
        TT,
        t: tt[0],
        tt,
        K: utc ? "Z" : tzHHMM(date.getTimezoneOffset()),
        "\\": "",
    };
    return format.replace(/(?:\\(?=.)|(?<!\\)(?:([yMdf])\1{0,3}|([HhmsTt])\2?|K))/g, $0 => replacements[$0]);
}
Discussions

How get a date formatted like 2023-02-07 in JS?
You can do a quick and dirty format to yyyy-mm-dd with date.toISOString().slice(0, 10) More on reddit.com
🌐 r/webdev
26
0
March 13, 2023
Date/ Time Manipulation

With the new java.time API, I was trying to take a dateTime string and parse it, checking to make sure it fits the correct format, but for all that is holy, I could not get anything working while taking input through a Scanner object.

As is stated in many places here show your code - without code we cannot help you.

Show us: What have you tried

More on reddit.com
🌐 r/javahelp
17
3
November 27, 2012
🌐
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: const d = new Date("2015-03-25"); Try it Yourself » · The computed date will be relative to your time zone.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Intl › DateTimeFormat
Intl.DateTimeFormat - JavaScript | MDN
Returns a new object with properties reflecting the locale and formatting options computed during initialization of the object. In basic use without specifying a locale, DateTimeFormat uses the default locale and default options. ... const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0)); // toLocaleString without arguments depends on the implementation, // the default locale, and the default time zone console.log(new Intl.DateTimeFormat().format(date)); // "12/19/2012" if run with en-US locale (language) and time zone America/Los_Angeles (UTC-0800)
🌐
Bugfender
bugfender.com › blog › javascript-date-and-time
The Definitive Guide to JavaScript Date and Time | Bugfender
February 18, 2025 - Understand JavaScript Date object basic operations. Learn multiple ways to create, format, and manipulate Date objects. Discover methods for getting the current date in various formats. Explore advanced topics like calculating elapsed time and handling time zones.
🌐
Moment.js
momentjs.com
Moment.js | Home
moment().format('MMMM Do YYYY, h:mm:ss a'); moment().format('dddd'); moment().format("MMM Do YY"); moment().format('YYYY [escaped] YYYY'); moment().format();
Find elsewhere
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Date and time
The method Date.parse(str) can read a date from a string. 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.
Top answer
1 of 16
3084

If you need slightly less control over formatting than the currently accepted answer, Date#toLocaleDateString can be used to create standard locale-specific renderings. The locale and options arguments let applications specify the language whose formatting conventions should be used, and allow some customization of the rendering.

Options key examples:

  1. day:
    The representation of the day.
    Possible values are "numeric", "2-digit".
  2. weekday:
    The representation of the weekday.
    Possible values are "narrow", "short", "long".
  3. year:
    The representation of the year.
    Possible values are "numeric", "2-digit".
  4. month:
    The representation of the month.
    Possible values are "numeric", "2-digit", "narrow", "short", "long".
  5. hour:
    The representation of the hour.
    Possible values are "numeric", "2-digit".
  6. minute: The representation of the minute.
    Possible values are "numeric", "2-digit".
  7. second:
    The representation of the second.
    Possible values are "numeric", 2-digit".
  8. hour12:
    The representation of time format.
    Accepts boolean true or false

All these keys are optional. You can change the number of options values based on your requirements, and this will also reflect the presence of each date time term.

Note: If you would only like to configure the content options, but still use the current locale, passing null for the first parameter will cause an error. Use undefined instead.

For different languages:

  1. "en-US": For American English
  2. "en-GB": For British English
  3. "hi-IN": For Hindi
  4. "ja-JP": For Japanese

You can use more language options.

For example

var options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
var today  = new Date();

console.log(today.toLocaleDateString("en-US")); // 9/17/2016
console.log(today.toLocaleDateString("en-US", options)); // Saturday, September 17, 2016
console.log(today.toLocaleDateString("hi-IN", options)); // शनिवार, 17 सितंबर 2016

You can also use the toLocaleString() method for the same purpose. The only difference is this function provides the time when you don't pass any options.

// Example
9/17/2016, 1:21:34 PM

References:

  • toLocaleString()

  • toLocaleDateString()

2 of 16
1756

For custom-delimited date formats, you have to pull out the date (or time) components from a DateTimeFormat object (which is part of the ECMAScript Internationalization API), and then manually create a string with the delimiters you want.

To do this, you can use DateTimeFormat#formatToParts. You could destructure the array, but that is not ideal, as the array output depends on the locale:

{ // example 1
   let formatter = new Intl.DateTimeFormat('en');
   let example = formatter.formatToParts();
   console.log(example);
}
{ // example 2
   let formatter = new Intl.DateTimeFormat('hi');
   let example = formatter.formatToParts();
   console.log(example);
}

Better would be to map a format array to resultant strings:

function join(date, options, separator) {
   function format(option) {
      let formatter = new Intl.DateTimeFormat('en', option);
      return formatter.format(date);
   }
   return options.map(format).join(separator);
}

let options = [{day: 'numeric'}, {month: 'short'}, {year: 'numeric'}];
let joined = join(new Date, options, '-');
console.log(joined);

You can also pull out the parts of a DateTimeFormat one-by-one using DateTimeFormat#format, but note that when using this method, as of March 2020, there is a bug in the ECMAScript implementation when it comes to leading zeros on minutes and seconds (this bug is circumvented by the approach above).

let date = new Date(2010, 7, 5);
let year = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(date);
let month = new Intl.DateTimeFormat('en', { month: 'short' }).format(date);
let day = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(date);
console.log(`{month}-${year}`);

When working with dates and times, it is usually worth using a library (eg. luxon, date-fns, moment.js is not recommended for new projects) because of the many hidden complexities of the field.

Note that the ECMAScript Internationalization API, used in the solutions above is not supported in IE10 (0.03% global browser market share in Feb 2020).

🌐
Day.js
day.js.org › docs › en › display › format
Format · Day.js
'2020-04-02T08:02:17-05:00' dayjs('2019-01-25').format('[YYYYescape] YYYY-MM-DDTHH:mm:ssZ[Z]') // 'YYYYescape 2019-01-25T00:00:00-02:00Z' dayjs('2019-01-25').format('DD/MM/YYYY') // '25/01/2019'
🌐
DEV Community
dev.to › rfornal › format-date-and-time-with-vanilla-javascript-5f4m
Format Date and Time with Vanilla JavaScript - DEV Community
August 31, 2020 - I've used a variety of tools over the years to manage date and time functionality. Here I am documenting the Vanilla JavaScript for my own use. Tagged with javascript, webdev, beginners, frontend.
🌐
LogRocket
blog.logrocket.com › home › how to format dates in javascript: methods, libraries, and best practices
How to format dates in JavaScript: Methods, libraries, and best practices - LogRocket Blog
May 8, 2025 - import moment from 'moment'; import 'moment-timezone'; // Creating moments const now = moment(); // Current date/time const fromString = moment('2025-02-18T14:30:00Z'); const fromFormat = moment('18/02/2025', 'DD/MM/YYYY'); // Formatting fromString.format('YYYY-MM-DD'); // "2025-02-18" fromString.format('dddd, MMMM Do YYYY'); // "Tuesday, February 18th 2025" fromString.format('h:mm a'); // "2:30 pm" // Operations (modifies the original moment) fromString.add(7, 'days'); fromString.subtract(2, 'months'); // Timezone handling const tokyoTime = fromString.clone().tz('Asia/Tokyo').format('YYYY-MM-
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-do-you-display-javascript-datetime-in-12-hour-am-pm-format
How to Display JavaScript datetime in 12 hour AM/PM Format? - GeeksforGeeks
July 12, 2025 - The Intl.DateTimeFormat object enables language-sensitive date and time formatting. By specifying the appropriate options, we can easily convert the time to a 12-hour format with AM/PM notation. JavaScript ·
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Intl › DateTimeFormat › DateTimeFormat
Intl.DateTimeFormat() constructor - JavaScript | MDN
January 21, 2026 - The localized representation of the time zone name. Possible values are: ... Short generic non-location format (e.g.: PT, Los Angeles Zeit).
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date › toLocaleTimeString
Date.prototype.toLocaleTimeString() - JavaScript | MDN
In implementations without Intl.DateTimeFormat support, this parameter is ignored. See the Intl.DateTimeFormat() constructor for details on these parameters and how to use them. A string representing the time portion of the given date according to language-specific conventions.
🌐
Codecademy
codecademy.com › article › javascript-date-format
How to Format a Date in JavaScript | Codecademy
"Wed Feb 12 2025 11:47:21 GMT+0530 (India Standard Time)" ... The default format does the job in many cases. However, it may not work in situations where you want the date in another format. In such cases, you can format the date to get it in your preferred format. In the next section, you’ll learn some inbuilt date formatting methods in JavaScript.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-format-a-date-with-javascript-date-formatting-in-js
How to Format a Date with JavaScript – Date Formatting in JS
November 7, 2024 - Using Intl.DateTimeFormat, you can specify the desired locale and various options to format dates precisely as needed. There are more options you can use in the official documentation. When working with dates, it's essential to consider time zones, especially when dealing with global applications or time-sensitive information. JavaScript provides methods to handle time zones effectively:
🌐
W3Schools
w3schools.com › js › js_dates.asp
JavaScript Dates
February 4, 2026 - JavaScript stores dates as number of milliseconds since January 01, 1970. Zero time is January 01, 1970 00:00:00 UTC.
🌐
Moment.js
momentjs.com › docs
Moment.js | Docs
Display Format Time from now Time from X Time to now Time to X Calendar Time Difference Unix Timestamp (milliseconds) Unix Timestamp (seconds) Days in Month As Javascript Date As Array As JSON As ISO 8601 String As Object As String Inspect
🌐
D3
d3js.org › d3-time-format
d3-time-format | D3 by Observable
This module provides an approximate ... representations. To format a date, create a formatter from a specifier (a string with the desired format directives, indicated by %); then pass a date to the formatter, which returns a string....
🌐
Reddit
reddit.com › r/webdev › how get a date formatted like 2023-02-07 in js?
r/webdev on Reddit: How get a date formatted like 2023-02-07 in JS?
March 13, 2023 -

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