Create Date Object In JavaScript
In JavaScript, the date object is based on a timestamp of milliseconds elapsed since the epoch.
The date output will use the time zone of the local device which is normally the web browser.
A date object is created via the date constructor. An optional date string can be passed to the constructor before or after the date object is created. Alternatively, up to 7 numbers can be used to specify year, month, day, hour, minute, second and millisecond.
Common Syntax Of Date Object In JavaScript
Glossary:
Epoch
Time representation of zero.
Unix Epoch
Number of seconds since 1970-01-01 00:00:00 UTC.
Common Date Object
Name | Description | Example |
---|---|---|
Date() | Returns current date and time. | new Date(); |
Date(date_string) | Returns specified date and time. | new Date(“2025-01-04”); |
Date(year,month …) | Returns specified date and time. | new Date(2025, 0, 1, 11, 01, 59, 0); |
Name | Description | Example |
JavaScript Date Object Snippet
// OjamboShop.com Learning JavaScript Date Object Tutorial let td = new Date(); console.log(td); // Date String console.log(new Date("2025-01-04")); // Specified Date And Time console.log(new Date(2025, 0)); // Year, Month console.log(new Date(2025, 0, 4)); // Year, Month, Day console.log(new Date(2025, 0, 4, 11)); // Year, Month, Day, Hours console.log(new Date(2025, 0, 4, 11, 1)); // Year, Month, Day, Hours, Minutes console.log(new Date(2025, 0, 4, 11, 1, 59)); // Year, Month, Day, Hours, Minutes, Seconds console.log(new Date(2025, 0, 4, 11, 1, 59, 300)); // Year, Month, Day, Hours, Minutes, Seconds, Milliseconds
Usage
You can use any IDE or text editor and the web browser to compile and execute JavaScript code. For this tutorial, the OjamboShop.com Learning JavaScript Course Web IDE can used to input and compile JavaScript code for the date object.
Open Source
JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. This allows commercial use, modification, distribution, and allows making derivatives proprietary.
Learn Programming Courses:
Get the Learning JavaScript Course for your web browser on any device.
Learn Programming Books:
Learning Javascript Book is available as Learning JavaScript Paperback or Learning JavaScript Ebook.
Conclusion:
JavaScript makes it easy to use the date object. Use the date object to display the current date or a specified date and time.
If you enjoy this article, consider supporting me by purchasing one of my OjamboShop.com Online Programming Courses or publications at Edward Ojambo Programming Books
References:
- Learning JavaScript Course on OjamboShop.com
- Learning Python Course on OjamboShop.com
- Learning PHP Course on OjamboShop.com
- Learning JavaScript Paperback on Amazon
- Learning JavaScript Ebook on Amazon
- Learning Python Ebook on Amazon
- Learning PHP Ebook on Amazon
- OjamboServices.com For Custom Websites, Applications & Tutorials