Dates are useful in every type of application for logging an event.
You can get the current date in JavaScript using new Date()
statement.
Basic Example
var today_date = new Date();
console.log(today_date);
You’ll see the date in default format including the timezone information as below.
Output
Sat Jan 01 2022 20:28:58 GMT+0530 (India Standard Time)
In this tutorial, you’ll learn how to get the current date in JavaScript.
Date()
constructor and a few other external libraries are available to provide the current date in the JavaScript application.
Using Date()
Date object represents a current moment in time in a platform-independent format.
- Create a new
Date()
object and assign it to a variable. - The
date
object returns the full date, time including the time zone.
var today_date = new Date();
today_date;
Output
You’ll see the date in default format including the timezone information as below.
Sat Jan 01 2022 20:28:58 GMT+0530 (India Standard Time)
This is how to get today’s date in JavaScript.
Get Current Date in JavasScript Using Moment
Moment JS is a library that supports getting date or time information from the system.
Moment JS is in maintenance mode and it is not under development anymore.
Adding Moment JS Reference
Add the following reference to the moment js
script.
Otherwise, you’ll get Uncaught ReferenceError: $ is not defined
error when trying to use the moment js methods.
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
Code
var today_date_moment = new moment();
today_date_moment.format();
Get Current Date in JavaScript Without Time
You can get the current date in JavaScript without time by converting the date string to an ISOString using the toISOString() method.
- This method converts the date into a simplified string format which is always 24 or 27 characters long.
- It contains the date and time separated by T.
You can split to ISOString using the split method and it’ll return the date in the first index position.
Code
var today_date = new Date();
today_date.toISOString().split('T')[0];
Get Only Date
You can get the date from Date
object using the getDate() method.
Code
var today_date = new Date();
today_date.getDate();
Get Only Month
You can get the month from Date
object using the getMonth() method.
The month index starts from 0
. Hence always add +
to get the right month number.
Code
var today_date = new Date();
today_date.getMonth() + 1;
Get Only Year
You can get year from Date
object using the getFullYear() method.
Code
var today_date = new Date();
today_date.getFullYear();
Get Date in Current Locale
You can get the Date
in the current Locale,
- Converting the date object using the toLocaleDateString() method
- Pass the current locale
- To get the list of all locales, refer to this StackOverflow answer
Code
var today_date = new Date();
today_date.toLocaleDateString('en-GB')
This is how you can get today’s date in the current locale.
Get Current Date In yyyy-mm-dd Format
Date in ISOString format is already available in the yyyy-mm-dd
.
To get the current date,
- Convert the current date into ISOString
- Split it to get only the date value.
Code
var today_date = new Date();
console.log(today_date.toISOString().split('T')[0]);
Get Current Date In dd/mm/yyyy Format
You can get the current date and obtain the date, month and year and append it in the format dd/mm/yyyy
.
var today = new Date();
var dd = String(today.getDate());
var mm = String(today.getMonth() + 1);
var yyyy = today.getFullYear();
console.log(dd + '/'+ mm + '/' + yyyy);
Get Current Date in dd mmm yyyy Format
To get the month name in short form, you can use the below code snippet.
Code
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var date = new Date();
var str = date.getDate()
+ '-' + months[date.getMonth()]
+ '-' + date.getFullYear();
console.log(str);
Get Current Date in String Format
var date_str = new Date();
console.log(date_str);
JSfiddle
This tutorial is available in this JSFiddle.