Categories
date date-formatting javascript

How do I get the current date in JavaScript?

2835

How do I get the current date in JavaScript?

11

  • 447

    var currentTime = new Date();

    – Hendrik

    Oct 7, 2009 at 11:39

  • 14

    See the documentation for the Date object. It has examples.

    – Quentin

    Oct 7, 2009 at 11:40

  • 5

    this would help you tizag.com/javascriptT/javascriptdate.php

    – user1017788

    Dec 1, 2011 at 4:34

  • 31

    new Date() returns the current time, not the current date. The distinction matters if you’re trying to compare it against another date which doesn’t have a time component (ie, is at midnight).

    Apr 12, 2015 at 1:01

  • 15

    use momentJs, this lib is gold for developers.

    – RBoschini

    Dec 22, 2015 at 17:30

3279

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 + "https://stackoverflow.com/" + dd + "https://stackoverflow.com/" + yyyy;
document.write(today);

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

Simply change today = mm +"https://stackoverflow.com/"+ dd +"https://stackoverflow.com/"+ yyyy; to whatever format you wish.

13

  • 7

    thanks for the code.. but what I still don’t get it, is the line if(dd<10){dd=’0’+dd} … why < 10? from what I understand from the code is if day’s character is less than 2, just add a preceding 0 in front of the day.. but why 10?

    – imin

    Jul 15, 2013 at 15:15

  • 23

    @imin: because less than 2 characters means 1 character… and everything under 10 (1 to 9) is 1 character, so we’ll have 01, 02, …, 09

    – zfm

    Jul 19, 2013 at 6:20

  • 11

    @MounaCheikhna – How could we be in the year 999?

    – nnnnnn

    Apr 23, 2014 at 22:36

  • 34

    Swap around the month and date if you’re not in north America.

    Jun 11, 2014 at 4:11

  • 13

    The new Date.prototype.toLocaleDateString() method is a more flexible solution. It’s a part of JavaScript since ECMAScript 5.1 and is well-supported by evergreen browsers. MDN: toLocaleDateString()

    Feb 16, 2016 at 16:53

514

var utc = new Date().toJSON().slice(0,10).replace(/-/g,"https://stackoverflow.com/");
document.write(utc);

Use the replace option if you’re going to reuse the utc variable, such as new Date(utc), as Firefox and Safari don’t recognize a date with dashes.

13

  • 6

    I dont think so 🙂 Seems pretty straightforward!

    Feb 12, 2014 at 10:40

  • 7

    toJSON() returns as utc datetime

    – Andy N

    Feb 16, 2014 at 7:10

  • 1

    It returns a JSON datetime. toUTCString() returns as utc datetime.

    Feb 18, 2014 at 9:08


  • 27

    It doesn’t consider TimezoneOffset. At my time of testing, I was seeking “now” and I got “yesterday”. stackoverflow.com/questions/13646446/…

    Jun 26, 2014 at 15:13


  • 8

    Perfect. This is the cleanest way to do this I’m seeing here. Works well in MomentJS for “Today, Not Now” moment( new Date().toJSON().slice(0, 10) )

    Aug 9, 2014 at 22:19

395

The shortest possible.

To get format like “2018-08-03”:

let today = new Date().toISOString().slice(0, 10)

console.log(today)

To get format like “8/3/2018”:

let today = new Date().toLocaleDateString()

console.log(today)

Also, you can pass locale as argument, for example toLocaleDateString("sr"), etc.

3

  • 10

    This still fails due to time zone shift.

    – LStarky

    Nov 7, 2018 at 2:29

  • Your second answer is the best.

    – GC_

    Sep 15, 2020 at 13:53

  • Note that this returns the day at UTC+0, but not the local day.

    – xji

    Mar 25, 2021 at 23:20