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.
Table of Contents
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}`;
console.log(dateString);
Sample Output:
2023-04-30
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.
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);
console.log(currentDate);
- 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');
console.log(today);
- 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’.
Conclusion
Obtaining the current date in JavaScript is a simple task, and the Date() function provides a convenient way to do it. By using various methods of the Date object, we can extract different components of the date and format them in various ways. The code presented in this article can be used to obtain the current date in the yyyy-mm-dd format, which is a common format used in many applications.