JavaScript Date and Time - Tutorial Republic
JavaScript Date and Time - Tutorial Republic
HOME HTML5 CSS3 JAVASCRIPT JQUERY BOOTSTRAP5 v4.6 PHP7 SQL REFERENCES EXAMPLES FAQ SNIPPETS
ADVERTISEMENTS
JAVASCRIPT BASIC
JS Introduction
JS Getting Started
JS Syntax
JS Variables
JS Generating Output JavaScript Date and Time
JS Data Types
JS Operators In this tutorial you will learn how to work with date and time in JavaScript.
JS Events
JS Strings
JS Numbers Using the Date Object
JS If…Else
The Date object is a built-in JavaScript object. It allows you to get the user's local time by accessing
JS Switch…Case
the computer system clock through the browser. The Date object also provides several methods for
JS Arrays
managing, manipulating, and formatting dates and times.
JS Sorting Arrays
JS Loops
JS Functions Creating a Date Object
JS Objects
Before we start working with the date and time, we need to create a Date object. Unlike other built-in
JAVASCRIPT & DOM
objects, such as arrays or functions, dates don't have a corresponding literal form: all date objects
JS DOM Nodes need to be created using the Date constructor function which is Date() .
JS DOM Selectors
There are four different ways to create a Date object in JavaScript.
JS DOM Styling
JS DOM Get Set Attributes
JS DOM Manipulation The new Date() Syntax
JS DOM Navigation
You can simply declare a new Date object without initializing its value. In this case, the date and time
JAVASCRIPT & BOM value will be set to the current date and time on the user's device on which the script is run.
JS Window
JS Screen Example Try this code »
JS Hoisting Behavior
This date is actually represent 31 January 2018 at 14:35:20 and 50 milliseconds. You can ignore the
JS Closures
time part and specify just the date part if you wish.
JS Strict Mode
JS JSON Parsing
JS Error Handling The new Date(dateString) Syntax
JS Regular Expressions
JavaScript also allows you to create a Date object by passing the string representing a date, or a date
JS Form Validation
and time, as shown in the following example:
JS Cookies
JS AJAX Requests
Example Try this code »
JS ES6 Features
https://www.tutorialrepublic.com/javascript-tutorial/javascript-date-and-time.php 1/6
6/23/23, 3:15 PM JavaScript Date and Time - Tutorial Republic
Once you have created an instance of the Date object, you can use its methods to perform various
tasks, such as getting different component of a date, setting or modifying individual date and time
value, etc. These methods are described in detail in the following sections.
Note: JavaScript provides shortcuts called "literals" for creating most of the native object
without having to use the new operator, like new Object(), new Array(), etc.
The output of this example will look something like this (depending on time zone offset):
Similarly, you can use the toLocaleTimeString() , toTimeString() methods of the Date object to
generate time strings, as shown in the following example:
https://www.tutorialrepublic.com/javascript-tutorial/javascript-date-and-time.php 2/6
6/23/23, 3:15 PM JavaScript Date and Time - Tutorial Republic
Once you have a proper date object, a number of methods are available to you to extract details from
it, such as the month, date, hours or minutes value etc. The following section describes the various
methods of extracting individual pieces of information from a Date object.
The getDay() method returns a number representing the day of the week (from 0 to 6) instead of
returning a name such as Sunday or Monday in such a way that, if it is Sunday, the method returns 0;
and if it is Monday , the method returns 1, and so on.
Likewise, the getMonth() method returns the number of months (from 0 to 11) instead of name of
the month. Here 0 represents the first month of the year. Therefore, if it is January the method returns
0 not 1; and if it is August, the method returns 7.
The getHours() method returns the number of hours into the day (from 0 to 23) according to the
24-hour clock. So, when it is midnight, the method returns 0; and when it is 3:00 P.M., it returns 15.
Note: The Date objects also have methods to obtain the UTC components. Just place UTC
after get, such as getUTCDate(), getUTCHour(), getUTCMinutes(), and so on.
https://www.tutorialrepublic.com/javascript-tutorial/javascript-date-and-time.php 3/6
6/23/23, 3:15 PM JavaScript Date and Time - Tutorial Republic
In addition to retrieving date and time values, you can also set or modify these values using the
JavaScript. This is most often used in program where you have to change the value of a date object
from one particular date or time to another. Let's see how it works.
For instance, in the following example we have used setFullYear() method to change the current
date stored in a variable ahead of two year in the future.
The output of the above example will look something like this:
Likewise, you can use the setMonth() method to set or modify the month part of a Date object.
The setMonth() method require an integer value from 0 to 11, if you set the value of the month
greater than 11, the year value of the date object will increment.
In other words, a value of 12 results in the year value increasing by 1, and the month value set to 0, as
demonstrated in the following example:
Similarly, you can modify the date part of the date object, like this:
The setDate() method require an integer value from 1 to 31. Also, if you pass the values greater
than the number of days in the month, the month will increment. For example:
https://www.tutorialrepublic.com/javascript-tutorial/javascript-date-and-time.php 4/6
6/23/23, 3:15 PM JavaScript Date and Time - Tutorial Republic
Methods for setting the time values are also pretty straight forward. The setHours() , setMinutes() ,
setSeconds() , setMilliseconds() can be used to set the hour, minutes, seconds, and milliseconds
part of the Date object respectively.
Each method takes integer values as parameters. Hours range from 0 to 23. Minutes and seconds
range from 0 to 59. And milliseconds range from 0 to 999. Here is an example:
The output of the above example will look something like the following:
ADVERTISEMENTS
Is this website helpful to you? Please give us a like, or share your feedback to help us improve. Connect with us on Facebook and Twitter for th
Terms of Use Report Error Title & Meta Length Calculator HTML Color Picker
https://www.tutorialrepublic.com/javascript-tutorial/javascript-date-and-time.php 5/6
6/23/23, 3:15 PM JavaScript Date and Time - Tutorial Republic
Sh
https://www.tutorialrepublic.com/javascript-tutorial/javascript-date-and-time.php 6/6