JavaScript - Date and Math ObjectDate Object :The Date object works with dates and times. Date objects are created with new Date().Eg : < !DOCTYPE html> < html> < body> < h1>JavaScript Dates< /h1> < h2>Using new Date()< /h2> < p id="demo">< /p> < p id="demo1">< /p> < script> const d = new Date(); //gives curent date and time document.getElementById("demo").innerHTML = d; const d1 = new Date("2024-10-09"); //gives date value document.getElementById("demo1").innerHTML = d1; < /script> < /body> < /html>Output : JavaScript DatesUsing new Date()Wed Oct 09 2024 17:59:12 GMT+0530 (India Standard Time) Wed Oct 09 2024 05:30:00 GMT+0530 (India Standard Time) Creating Date Objects : Date objects are created with the new Date() constructor.There are 9 ways to create a new date object: new Date() new Date(date string) new Date(year,month) new Date(year,month,day) new Date(year,month,day,hours) new Date(year,month,day,hours,minutes) new Date(year,month,day,hours,minutes,seconds) new Date(year,month,day,hours,minutes,seconds,ms) new Date(milliseconds)new Date(year, month, ...) new Date(year, month, ...) creates a date object with a specified date and time. 7 numbers specify year, month, day, hour, minute, second, and millisecond (in that order): < !DOCTYPE html> < html> < body> < h2>JavaScript new Date()< /h2> < p>Using new Date(7 numbers), creates a new date object with the specified date and time:< /p> < p id="demo">< /p> < script> const d = new Date(2024, 10, 09, 06, 02, 30, 0); document.getElementById("demo").innerHTML = d; < /script> < /body> < /html>Output : new Date(milliseconds) : new Date(milliseconds) creates a new date object as milliseconds plus zero time: « Previous Next Topic » (JS - Comments, Variables) |