- 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() Constructor
The JavaScript Date() constructor is either used to create an instance of Date or to get a string representing the current date and time. For example:
<!DOCTYPE html> <html> <body> <p id="xyz"></p> <p id="abc"></p> <script> const d = new Date(); document.getElementById("xyz").innerHTML = d; document.getElementById("abc").innerHTML = d.toLocaleString(); </script> </body> </html>
Please note: A constructor is used to create an object, whereas a method is used to execute a block of statements.
JavaScript Date() syntax
The syntax of the Date() constructor in JavaScript is:
new Date()
where the new keyword is used to create an instance of the object.
However, we can also define value, DateString, or DateObject while creating an instance of the Date() constructor. For example:
<!DOCTYPE html> <html> <body> <p id="res"></p> <script> const d = new Date("September 16, 2022, 08:59:00"); document.getElementById("res").innerHTML = d; </script> </body> </html>
Also we can define year, month (by index number from 0 to 11), day, hours, minutes, seconds, and milliseconds while creating an instance of the Date() constructor. Keep the following points in mind while using these parameters to in Date() constructor:
-
year: For 1900 to 1999, you have to use 0 to 99. And use the actual year for all other years. For example:
- 89 for 1989
- 2011 for 2011
- 97 for 1997
- 2000 for 2000
- monthIndex: For January, you have to use 0. Similarly, for February, you have to use 1. That is, use 0-11 for January-December.
- day: Use an integer value from 1-31 based on the day of the month you need to use.
- hours: Legal values are from 0 to 23.
- minutes: Legal values are from 0 to 0 to 59.
- seconds: Legal values are from 0 to 59.
- milliseconds: Legal values are from 0 to 999.
Please note: The last four parameters, which are hours, minutes, seconds, and milliseconds, are optional. The default value of day is 1, whereas the default value of the other three parameters is 0. For example:
<!DOCTYPE html> <html> <body> <p>With only Required Parameters: <span id="resOne"></span></p> <p>With Required and an Optional Parameter: <span id="resTwo"></span></p> <script> const dOne = new Date(2022, 8); document.getElementById("resOne").innerHTML = dOne; const dTwo = new Date(2022, 8, 16); document.getElementById("resTwo").innerHTML = dTwo; </script> </body> </html>
With only Required Parameters:
With Required and an Optional Parameter:
« Previous Tutorial Next Tutorial »