Date in javascript
1. Date
JavaScript Date
objects represent a particular moment in time. Date
contains a numeric value that represents milliseconds since 1 January 1970 UTC.
2. Creating a Date object
Date object can be created using Date()
constructor in the following ways:
2.1. Without arguments
Date()
creates a date object containing the current date and time.
var date = new Date();
console.log(date);
Output:
Tue Feb 02 2021 13:41:36 GMT+0530 (India Standard Time)
2.2. With a single integer argument
Date(a)
create a date object containing the time and date corresponding to 1 January 1970 + a
milliseconds.
var date = new Date(89829384324);
console.log(date);
Output:
Sun Nov 05 1972 22:06:24 GMT+0530 (India Standard Time)
2.3. With a string argument
Date(StringDate)
first parses the StringDate
with Date.parse()
then creates a date instance.
var date =new Date('21 Mar 2018');
console.log(date);
Output:
Wed Mar 21 2018 00:00:00 GMT+0530 (India Standard Time)
2.4. With two or more integer arguments
Date(y, m, d, h, min, s, ms)
arguments represent the year, month, day, hour, minutes, seconds, and milliseconds and creates the corresponding Date
object.
var date =new Date(2019, 11, 20, 22, 34, 45, 3945);
console.log(date);
Output:
Fri Dec 20 2019 22:34:48 GMT+0530 (India Standard Time)
Note: Month in javaScript starts with 0. So 0 represents January and 11 represents December.
3. Converting date to a string
JavaScript provides a variety of methods to convert a date
object to a string
object which you can use based on your needs.
3.1. Convert to String
toString()
converts the whole object into a string object.
var date = new Date();
console.log(date.toString());
Output:
Tue Feb 02 2021 14:36:56 GMT+0530 (India Standard Time)
3.2. Convert to Time String
toTimeString()
removes the date from date
object and returns the new date object as a string.
var date = new Date();
console.log(date.toTimeString());
Output:
14:40:04 GMT+0530 (India Standard Time)
3.3. Convert to Date String
toDateString()
returns only the date part of the date
object as a string.
var date = new Date();
console.log(date.toDateString());
Output:
Tue Feb 02 2021
3.4. Convert to UTC String
toUTCString()
converts the date to UTC date format and returns it as a string.
var date = new Date();
console.log(date.toUTCString());
Output:
Tue, 02 Feb 2021 09:15:29 GMT
3.5. Convert to ISO String
toISOString()
converts the date to ISO date format and returns it as a string.
var date = new Date();
console.log(date.toISOString());
Output:
2021-02-02T09:18:21.737Z
3.6. Convert to GMT String
toGMTString()
converts the date to GMT date format and returns it as a string.
var date = new Date();
console.log(date.toGMTString());
Output:
Tue, 02 Feb 2021 09:20:29 GMT
3.7. Convert to Locale Date String
toLocaleDateString()
converts the date object to locale date format and returns it as a string.
var date = new Date();
console.log(date.toLocaleDateString());
Output:
2/2/2021
4. Formatting a JavaScript date
toLocaleDateString()
provides facility for formatting the date object according to your needs.
Syntax: dateObj.toLocaleDateString([locales [, options]])
locales
is a string parameter with a BCP 47 language tag or an array of strings containing BCP 47 language tags. You can find the complete list here.
options
parameter can have some or all of the following properties:
- localeMatcher: possible values are "lookup" and "best fit"; the default is "best fit"
- timeZone: the only value implementations must recognize is "UTC"; the default is the runtime's default time zone.
- hour12:possible values are true and false; the default is locale-dependent
- formatMatcher: possible values are "basic" and "best fit"; the default is "best fit"
- weekday: possible values are "narrow", "short" & "long"
- era: possible values are "narrow", "short" & "long"
- year: possible values are "numeric" & "2-digit"
- month: possible values are "numeric", "2-digit", "narrow", "short" & "long"
- day: possible values are "numeric" & "2-digit"
- hour: possible values are "numeric" & "2-digit"
- minute: possible values are "numeric" & "2-digit"
- second: possible values are "numeric" & "2-digit"
- timeZoneName: possible values are "short" & "long"
Example 1:
var date = new Date().toLocaleDateString('en-GB', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
timeZoneName: 'short'
});
console.log(date)
Output:
2 Feb 2021, 15:30:22 GMT+5:30
Example 2:
var date = new Date().toLocaleDateString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
});
console.log(date)
Output:
February 2, 2021
5. Get current information from the date
You can also get only parts different current information from the date
object in javascript by adding different methods to new Date()
5.1. Current year
getFullyear()
returns the year of the date
object on which it is called.
var dateObj = new Date();
console.log("date : "+dateObj)
console.log("year : "+dateObj.getFullYear());
Output:
date : Tue Feb 02 2021 15:52:51 GMT+0530 (India Standard Time)
year : 2021
5.2. Current month
getMonth()
returns the month of the date
object on which it is called.
var dateObj = new Date();
console.log("date : "+dateObj)
console.log("month : "+dateObj.getMonth());
Output:
date : Tue Feb 02 2021 15:48:08 GMT+0530 (India Standard Time)
month : 1
5.3. Current Date
getDate()
returns the day of the date
object on which it is called.
var dateObj = new Date();
console.log("date : "+dateObj)
console.log("day : "+dateObj.getDate());
Output:
date : Tue Feb 02 2021 15:50:52 GMT+0530 (India Standard Time)
day : 2
5.4. Current hour
getHours()
returns the hour of the date
object on which it is called.
var dateObj = new Date();
console.log("date : "+dateObj)
console.log("hour : "+dateObj.getHours());
Output:
date : Tue Feb 02 2021 15:56:00 GMT+0530 (India Standard Time)
hour : 15
5.5. Current minutes
getMinutes()
returns the minutes of the date
object on which it is called.
var dateObj = new Date();
console.log("date : "+dateObj)
console.log("minutes : "+dateObj.getMinutes());
Output:
date : Tue Feb 02 2021 15:58:04 GMT+0530 (India Standard Time)
minutes : 58
5.6. Current seconds
getSeconds()
returns the seconds of the date
object on which it is called.
var dateObj = new Date();
console.log("date : "+dateObj)
console.log("seconds : "+dateObj.getSeconds());
Output:
date : Tue Feb 02 2021 16:02:42 GMT+0530 (India Standard Time)
seconds : 42
5.7. Current milliseconds
getMilliseconds()
returns the milliseconds of the date
object on which it is called.
var dateObj = new Date();
console.log("date : "+dateObj)
console.log("milliseconds : "+dateObj.getMilliseconds());
Output:
date : Tue Feb 02 2021 16:05:00 GMT+0530 (India Standard Time)
milliseconds : 775
6. Convert to JSON
toJSON()
converts the date
object into a JSON object.
var dateObj = new Date();
console.log("date : "+dateObj)
console.log("json : "+dateObj.toJSON());
Output:
date : Tue Feb 02 2021 16:08:39 GMT+0530 (India Standard Time)
json : 2021-02-02T10:38:39.874Z
7. Conclusion
Date in JavaScript is very flexible. JavaScript provides a lot of methods to work with date
objects and use them according to your needs.