You are currently viewing 3 Methods to Get Current Date yyyy-mm-dd in JavaScript

3 Methods to Get Current Date yyyy-mm-dd in JavaScript

JavaScript Get Current Date yyyy-mm-dd

In this article, we will explore how to get the current date in the yyyy-mm-dd format in JavaScript.

How to get the current date?

1. Using the Date object:

We can get the current date in several ways but the most common and simplest method is using Date() function. The Date() function creates a new Date object that represents the current date and time. Let’s see the example,

let currentDate = new Date();
let year = currentDate.getFullYear();
let month = (currentDate.getMonth() + 1).toString().padStart(2, '0');
let day = currentDate.getDate().toString().padStart(2, '0');
let dateString = `${year}-${month}-${day}`;

Sample Output:


In this code:

  • We have created a new Date object called ‘currentDate‘.
    Then we get the current year using ‘getFullYear()’ method and stored it on the variable ‘year’.
  • Get current month using ‘getMonth()’ method and stored it on the variable ‘month’. Note that the getMonth() method returns the month index, which starts from 0 for January, so we add 1 to it to get the actual month value.
  • And get current day using ‘getDate()’ method and stored it on the variable ‘day’. We converted the month and day values to strings by using the padStart() method to add a leading zero if they are less than 10.
  • Finally, we concatenate the year, month, and day variables into single variable ‘dateString’ in the yyyy-mm-dd format.
    If we run this code, it will return the current date of the user system.
READ ALSO  3 Steps to Create Table from Stored Data in JavaScript Array

2. Using the Intl.DateTimeFormat object:

const today = new Date();
const formatter = new Intl.DateTimeFormat('en', { year: 'numeric', month: '2-digit', day: '2-digit' });
const currentDate = formatter.format(today);
  • In this above example code, we have created the new Date() object called ‘today’.
  • Then we use ‘Intl.DateTimeFormat‘ object with passing the format ‘yyyy-mm-dd’ and stored the value it on variable ‘formatter‘.
  • Create a variable ‘currentDate’ and pass the variable ‘today’ as an argument to the format() method.
  • Finally, we logged the variable ‘currentDate’. It has the current date the yyyy-mm-dd format.

3. Using moment.js library:

Moment.js is a popular JavaScript library for working with dates and times. It provides an easy-to-use API for parsing, manipulating, and formatting dates and times. In this method, you need to download the Moment.js library.

const moment = require('moment');
const today = moment().format('YYYY-MM-DD');
  • In this above code, we have created a new moment object representing the current date and time.
  • Then we use the format() method and pass the format ‘YYYY-MM-DD’, which is assigned to the ‘today’ variable. So, this method returns a string representing the current date in the specified format,
  • And we logged the variable ‘today’.


Leave a Reply