- JavaScript Basics
- JavaScript Tutorial
- JavaScript: where to write
- JavaScript: how to display
- JavaScript: keywords
- JavaScript: comments
- JavaScript: variables
- JavaScript: operators
- JavaScript: data types
- JavaScript Conditional Statements
- JavaScript: if-else
- JavaScript: switch
- JavaScript: for loop
- JavaScript: while loop
- JavaScript: do-while loop
- JavaScript: break and continue
- JavaScript Popup Boxes
- JavaScript: alert box
- JavaScript: confirm box
- JavaScript: prompt box
- JavaScript Popular Topics
- JavaScript: functions
- JavaScript: innerHTML
- JavaScript: getElementById()
- JavaScript: getElementsByClassName()
- JavaScript: getElementsByName()
- JavaScript: getElementsByTagName()
- JavaScript: querySelector()
- JavaScript: querySelectorAll()
- JavaScript: document.write()
- JavaScript: console.log()
- JavaScript: boolean
- JavaScript: events
- JavaScript: Math object
- JavaScript: Math.random()
- JavaScript: Number()
- JavaScript: parseInt()
- JavaScript: parseFloat()
- JavaScript Arrays
- JavaScript: array
- JavaScript: find length of array
- JavaScript: add element at beginning
- JavaScript: add element at end
- JavaScript: remove first element
- JavaScript: remove last element
- JavaScript: get first index
- JavaScript: get last index
- JavaScript: reverse an array
- JavaScript: sort an array
- JavaScript: concatenate arrays
- JavaScript: join()
- JavaScript: toString()
- JavaScript: from()
- JavaScript: check if value exists
- JavaScript: check if array
- JavaScript: slice an array
- JavaScript: splice()
- JavaScript: find()
- JavaScript: findIndex()
- JavaScript: entries()
- JavaScript: every()
- JavaScript: fill()
- JavaScript: filter()
- JavaScript: forEach()
- JavaScript: map()
- JavaScript Strings
- JavaScript: string
- JavaScript: length of string
- JavaScript: convert to lowercase
- JavaScript: convert to uppercase
- JavaScript: string concatenation
- JavaScript: search()
- JavaScript: indexOf()
- JavaScript: search() vs. indexOf()
- JavaScript: match()
- JavaScript: match() vs. search()
- JavaScript: replace()
- JavaScript: toString()
- JavaScript: String()
- JavaScript: includes()
- JavaScript: substr()
- JavaScript: slice string
- JavaScript: charAt()
- JavaScript: repeat()
- JavaScript: split()
- JavaScript: charCodeAt()
- JavaScript: fromCharCode()
- JavaScript: startsWith()
- JavaScript: endsWith()
- JavaScript: trim()
- JavaScript: lastIndexOf()
- JavaScript Date and Time
- JavaScript: date and time
- JavaScript: Date()
- JavaScript: getFullYear()
- JavaScript: getMonth()
- JavaScript: getDate()
- JavaScript: getDay()
- JavaScript: getHours()
- JavaScript: getMinutes()
- JavaScript: getSeconds()
- JavaScript: getMilliseconds()
- JavaScript: getTime()
- JavaScript: getUTCFullYear()
- JavaScript: getUTCMonth()
- JavaScript: getUTCDate()
- JavaScript: getUTCDay()
- JavaScript: getUTCHours()
- JavaScript: getUTCMinutes()
- JavaScript: getUTCSeconds()
- JavaScript: getUTCMilliseconds()
- JavaScript: toDateString()
- JavaScript: toLocaleDateString()
- JavaScript: toLocaleTimeString()
- JavaScript: toLocaleString()
- JavaScript: toUTCString()
- JavaScript: getTimezoneOffset()
- JavaScript: toISOString()
- JavaScript Regular Expression
- JavaScript: regular expression
- JavaScript: RegEx . (dot)
- JavaScript: RegEx \w and \W
- JavaScript: RegEx \d and \D
- JavaScript: RegEx \s and \S
- JavaScript: RegEx \b and \B
- JavaScript: RegEx \0
- JavaScript: RegEx \n
- JavaScript: RegEx \xxx
- JavaScript: RegEx \xdd
- JavaScript: RegEx quantifiers
- JavaScript: RegEx test()
- JavaScript: RegEx lastIndex
- JavaScript: RegEx source
- JavaScript Programs
- JavaScript Programs
JavaScript Date and Time (with Example Programs)
This post is published to provide information regarding the work with date and time in JavaScript, like:
- Why do we need to learn date and time in JavaScript?
- JavaScript Date and Time Functions
- JavaScript Date and Time Programs and Codes
- JavaScript displays the date in the format of yyyy-mm-dd
- JavaScript displays the date in the format of dd-mm-yyyy
- JavaScript Display Time in Format hh:mm:ss
- JavaScript Display Time in Format hh:mm:ss AM/PM
- JavaScript displays date and time in the format dd-mm-yyyy hh:mm:ss AM/PM
Every time we need to implement date and/or time in our JavaScript code, we first need to create an object in the Date() constructor. For example:
<!DOCTYPE html> <html> <body> <p id="res"></p> <script> const d = new Date(); document.getElementById("res").innerHTML = d; </script> </body> </html>
The Date() constructor creates an object (a Date object) with the current date and time based on the browser's timezone.
Why do we need to learn date and time in JavaScript?
Sometimes we need to implement a date and/or time in our application because of the following reasons:
- to know when the post was published.
- to know when the post was modified.
- to know when the comment was published.
- and many more.
So to implement these important features in your applications, you must have at least basic awareness regarding the work with date and time in JavaScript. That is why I have created a brief tutorial on it.
JavaScript Date and Time Functions
Before starting anything regarding the work with date and time, let me first list all the JavaScript built-in methods that can be used to fetch and/or get date and/or time in JavaScript:
- getFullYear(): get the year (in the form YYYY).
- getMonth(): get the month (0–11) of a date, where 0 is for January and 11 is for December.
- getDate(): get the day of the month, which will be from 1 to 31.
- getDay(): get the day of the week (0–6), where 0 is for Sunday and 6 is for Saturday.
- getHours(): get the hour (0–23).
- getMinutes(): get the minutes (0–59) of the time.
- getSeconds(): get the seconds (0–59) of the time.
- getMilliseconds(): get the milliseconds (0–999).
- getTime(): get the total number of milliseconds since January 1, 1970.
- getUTCFullYear(): similar to getFullYear(), except that it will be based on the Universal Time Coordinated (UTC) date.
- getUTCMonth(): similar to getMonth(), except that it will be based on the Universal Time Coordinated (UTC) date.
- getUTCDate(): similar to getDate(), except that it will be based on the Universal Time Coordinated (UTC) date.
- getUTCDay(): similar to getDay(), except that it will be based on the Universal Time Coordinated (UTC) date.
- getUTCHours(): similar to getHours(), except that it will be based on the Universal Time Coordinated (UTC) date.
- getUTCMinutes(): similar to getMinutes(), except that it will be based on the Universal Time Coordinated (UTC) date.
- getUTCSeconds(): similar to getSeconds(), except that it will be based on the Universal Time Coordinated (UTC) date.
- getUTCMilliseconds(): similar to getMilliseconds(), except that it will be based on the Universal Time Coordinated (UTC) date.
- toDateString(): get the date as a string.
- toLocaleDateString(): get only the date portion of the toLocaleString().
- toLocaleTimeString(): get only the time portion of the toLocaleString().
- toLocaleString(): get the complete date using locale conventions.
- toUTCString(): get the complete date as a string based on the UTC date.
- getTimezoneOffset(): find the difference between UTC and local time.
- toISOString(): get the date as a string based on the ISO (International Organization for Standardization) standard.
However, there are more other methods under the category of date and time in JavaScript, but I have not listed those. I think these essential methods (listed above) are enough to do all the tasks regarding the work with date and time in your JavaScript application(s).
JavaScript Display Date in Format yyyy-mm-dd
<!DOCTYPE html> <html> <body> <p>Today's Date: <span id="date"></span></p> <script> const d = new Date(); let yyyy = d.getFullYear(); let mm = d.getMonth() + 1; let dd = d.getDate(); if(mm<10) mm = '0' + mm; if(dd<10) dd = '0' + dd; let dt = yyyy + "-" + mm + "-" + dd; document.getElementById("date").innerHTML = dt; </script> </body> </html>
Today's Date:
Since the getMonth() method returns the month's index number of the year, which is from 0 to 11, instead of 1 to 12, therefore, I've added 1 to the returned value by this method.
JavaScript Display Date in Format dd-mm-yyyy
To display the date in the format of dd-mm-yyyy, just replace the following statement from the above (previous) example or program:
let dt = yyyy + "-" + mm + "-" + dd;
with the statement given below:
let dt = dd + "-" + mm + "-" + yyyy;
JavaScript Display Time in Format hh:mm:ss
<!DOCTYPE html> <html> <body> <p>Current Time: <span id="time"></span></p> <script> const d = new Date(); let hh = d.getHours(); let mm = d.getMinutes(); let ss = d.getSeconds(); if(hh<10) hh = '0' + hh; if(mm<10) mm = '0' + mm; if(ss<10) ss = '0' + ss; let dt = hh + ":" + mm + ":" + ss; document.getElementById("time").innerHTML = dt; </script> </body> </html>
Current Time:
JavaScript Display Time in Format hh:mm:ss AM/PM
<!DOCTYPE html> <html> <body> <p>Current Time: <span id="time"></span></p> <script> const d = new Date(); let hh = d.getHours(); let mm = d.getMinutes(); let ss = d.getSeconds(); let apm = hh<12 ? 'AM' : 'PM'; hh = hh%12; if(hh<10) hh = '0' + hh; if(mm<10) mm = '0' + mm; if(ss<10) ss = '0' + ss; let dt = hh + ":" + mm + ":" + ss + ' ' + apm; document.getElementById("time").innerHTML = dt; </script> </body> </html>
Current Time:
JavaScript displays date and time in the format dd-mm-yyyy hh:mm:ss AM/PM
<!DOCTYPE html> <html> <body> <p>Current Date and Time: <span id="res"></span></p> <script> const d = new Date(); let yyyy = d.getFullYear(); let mn = d.getMonth() + 1; let dd = d.getDate(); let hh = d.getHours(); let mm = d.getMinutes(); let ss = d.getSeconds(); if(mn<10) mn = '0' + mn; if(dd<10) dd = '0' + dd; if(hh<10) hh = '0' + hh; if(mm<10) mm = '0' + mm; if(ss<10) ss = '0' + ss; let apm = hh<12 ? 'AM' : 'PM'; hh = hh%12; let dt = dd + "-" + mn + "-" + yyyy + " " + hh + ":" + mm + ":" + ss + ' ' + apm; document.getElementById("res").innerHTML = dt; </script> </body> </html>
Current Date and Time:
« Previous Tutorial Next Tutorial »