How to get the Current date in Javascript?
Contents
Date Object in Javascript
Javascript provides the Date() object to get the Current date & time. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC. The Date methods can be used to get the required Date or Time information from that object.The Date object is created by using new keyword, i.e. new Date().
Syntax to create a Date object
1 |
var <variable_name> = new Date(); |
The Date() constructor creates a date object which sets the current time and date details based on the browser’s time zone.
Example
1 2 3 4 5 6 7 |
<script> // Get the Current date & Time var today = new Date(); //print the date and time information console.log(today); </script> |
Output
1 |
Fri May 01 2020 13:03:29 GMT+0530 (India Standard Time) |
Get the Current date from Date object in Javascript
The below date methods are used to get the date,month and year from the date object.
- getFullYear() – Get a year of four digit number (yyyy)
- getMonth() – Get a month as a number (0-11). 0 represents January & 1 represents February and so on.
- getDate() – Get a day as a number (1 – 31)
Example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<script> // Get the Current date & Time var today = new Date(); //Extract the date from Date object var year = today.getFullYear(); var month = today.getMonth() + 1; // 0 represents January. So adding 1 here. var day = today.getDate(); //Current Date var currentDate = year +'/'+ month + '/'+ day; //print the Today's Date document.write(" Current Date is : " + currentDate); </script> |
Output
As we run this code on May 1st,2020, We got the year as 2020, month as 5 and day as 1.
1 |
Current Date is : 2020/5/1 |
Get Month and Date in two digit format using Javascript
The getmonth() and getdate() function returns the single digit for the month/day if the number is lesser than 10. Lets see how to get the month and date in two digit format (mm/dd). Assume that we run this code on 1st,May,2020. So the date object is returning the below values for the date methods.
Method | Return value (as per the below code) |
today.getFullYear() | 2020 |
today.getMonth() | 4 |
today.getDate() | 1 |
Both getMonth() and getDate() functions returns a single digit. So we are validating the month/day number using if condition and concatenating the digit 0 before the month/day number
1 2 3 4 5 |
var month = ((today.getMonth() + 1) < 10 ? '0' : '') + (today.getMonth() + 1) ; var day = ((today.getDate() + 1) < 10 ? '0' : '') + (today.getDate()) ; |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<html> <body> <h2>Current/Today's date in Javascript</h2> <ul> <li id="cdate" style="color:darkgreen;"></li><br> <li id="fdate" style="color: darkviolet;"></li> </ul> <script> // Get the Current date & Time var today = new Date(); //Extract the date and Format it to YYYY/MM/DD var year = today.getFullYear(); // if the month is lesser than 10, adding 0 as a first digit // and then concatenating month number var month = ((today.getMonth() + 1) < 10 ? '0' : '') + (today.getMonth() + 1) ; // if the day is lesser than 10, adding 0 as a first digit // and then concatenating month number var day = ((today.getDate() + 1) < 10 ? '0' : '') + (today.getDate()) ; var currentDate = year +'/'+ month + '/'+ day; document.getElementById("cdate").innerHTML = "Current Date & Time => " + today; document.getElementById("fdate").innerHTML = "Current Date (YYYY/MM/DD) => " + currentDate; </script> </body> </html> |
Output