JavaScript – Date


What is the Date ?

Date is the notation of day/month/year specified in number.

What is the Date in Javascript ?

Date object is a javascript built-in datatype.

Date is a Javascript object that can be used to get year, month and day.

Why is Date Object used in javascript ?

Date Object is mostly used on webpages to display the timer.

We can get and set the (year/month/day/hour/minute/second/millisecond) of date objects by using Date methods.

How to construct a Date Object in javascript ?

By using Date constructors we can create date objects.

Date methods facilitates to get and set (day/month/year/ hours /minute/seconds).

There are four ways to construct Date object,

1. Date()  

2. Date(milliseconds)  

3. Date(dateString)  


4. Date(year, month, day, hours, minutes, seconds, milliseconds)

Free online javascript course !!!

Click to Learn More about – Javascript online learning

1. Date()

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var a = new Date();
			document.write(a+"<br/>");
		</script>
	</head>
	<body></body>
</html>

Output :

Tue Apr 30 2019 12:43:43 GMT+0530 (India Standard Time)

2. Date(milliseconds)

We can provide milliseconds to find out the Date using the Date() object.

By providing both positive and negative milliseconds values.

Javascript will provide date values before and after period values based upon the milliseconds.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var millisec = 1556608000131;
			var Oldmillisec = -1556608000131;

			var a = new Date(millisec);
			var b = new Date(Oldmillisec);
			document.write("Date value for this milliseconds = "+millisec+"<br/>");
			document.write(a+"<br/>");
			document.write("Date value for Negative milliseconds = "+Oldmillisec+"<br/>");
			document.write(b+"<br/>");
		</script>
	</head>
	<body></body>
</html>

Output :

Date value for this milliseconds = 1556608000131

Tue Apr 30 2019 12:36:40 GMT+0530 (India Standard Time)

Date value for Negative milliseconds = -1556608000131

Fri Sep 03 1920 22:23:19 GMT+0530 (India Standard Time)

3. Date(dateString)  

We can create a Date object with a specified date and time passing as String.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var value = "November 11, 2018 12:30:23";
			var a = new Date(value);
			document.write("Date value by passing String value = "+value+"<br/>");
			document.write(a+"<br/>");
		</script>
	</head>
	<body></body>
</html>

Output :

Date value by passing String value = November 11, 2018 12:30:23

Sun Nov 11 2018 12:30:23 GMT+0530 (India Standard Time)

4. Date(year, month, day, hours, minutes, seconds, milliseconds)

We can create Date() object with specified params like year,month,day etc.

Example :

<!DOCTYPE html>
<html>
	<head>
		<title>Our title</title>
		<script type="text/javascript">
			var year = 2015;
			var month = 18;
			var day = 22;
			var hour = 13;
			var minute = 15;
			var second = 27;
			var millisecond = 14;
			var a = new Date(year,month,day,hour,minute,second,millisecond);
			document.write("year = "+year+"<br/>");
			document.write("month = "+month+"<br/>");
			document.write("day = "+day+"<br/>");
			document.write("hour = "+hour+"<br/>");
			document.write("minute = "+minute+"<br/>");
			document.write("second = "+second+"<br/>");
			document.write("millisecond = "+millisecond+"<br/>");
			document.write("DateObject = "+a+"<br/>");
		</script>
	</head>
	<body></body>
</html>

Output :


year = 2015

month = 18

day = 22

hour = 13

minute = 15

second = 27

millisecond = 14

DateObject = Fri Jul 22 2016 13:15:27 GMT+0530 (India Standard Time)

Javascript has a lot of Date methods, some of them are listed below.

SnoMethod / FunctionDescription
1getDate()getDate() method returns the integer value between 1 and 31 that represents the day for the specified date on the basis of local time.
2getDay()getDay() method  returns the integer value between 0 and 6 that represents the day of the week on the basis of local time.
3getFullYears()getFullYears() method returns the integer value that represents the year on the basis of local time.
4getHours()getHours() method returns the integer value between 0 and 23 that represents the hours on the basis of local time.
5getMilliseconds()getMilliseconds() method returns the integer value between 0 and 999 that represents the milliseconds on the basis of local time.
6getMinutes()getMinutes() method returns the integer value between 0 and 59 that represents the minutes on the basis of local time.
7getMonth()getMonth() method returns the integer value between 0 and 11 that represents the month on the basis of local time.
8getSeconds()getSeconds() method returns the integer value between 0 and 60 that represents the seconds on the basis of local time.
9getUTCDate()getUTCDate() method returns the integer value between 1 and 31 that represents the day for the specified date on the basis of universal time.
10getUTCDay()getUTCDay() method returns the integer value between 0 and 6 that represents the day of the week on the basis of universal time.
11getUTCFullYears()getUTCFullYears() method returns the integer value that represents the year on the basis of universal time.
12getUTCHours()getUTCHours() method returns the integer value between 0 and 23 that represents the hours on the basis of universal time.
13getUTCMinutes()getUTCMinutes() method returns the integer value between 0 and 59 that represents the minutes on the basis of universal time.
14getUTCMonth()getUTCMonth() method returns the integer value between 0 and 11 that represents the month on the basis of universal time.
15getUTCSeconds()getUTCSeconds() method returns the integer value between 0 and 60 that represents the seconds on the basis of universal time.
16setDate()setDate() method sets the day value for the specified date on the basis of local time.
17setDay()setDay() method sets the particular day of the week on the basis of local time.
18setFullYears()setFullYears() method sets the year value for the specified date on the basis of local time.
19setHours()setHours() method sets the hour value for the specified date on the basis of local time.
20setMilliseconds()setMilliseconds() method sets the millisecond value for the specified date on the basis of local time.
21setMinutes()setMinutes() method sets the minute value for the specified date on the basis of local time.
22setMonth()setMonth() method sets the month value for the specified date on the basis of local time.
23setSeconds()setSeconds() method sets the second value for the specified date on the basis of local time.
24setUTCDate()setUTCDate() method sets the day value for the specified date on the basis of universal time.
25setUTCDay()setUTCDay() method sets the particular day of the week on the basis of universal time.
26setUTCFullYears()setUTCFullYears() method sets the year value for the specified date on the basis of universal time.
27setUTCHours()setUTCHours() method sets the hour value for the specified date on the basis of universal time.
28setUTCMilliseconds()setUTCMilliseconds() method sets the millisecond value for the specified date on the basis of universal time.
29setUTCMinutes()setUTCMinutes() method sets the minute value for the specified date on the basis of universal time.
30setUTCMonth()setUTCMonth() method sets the month value for the specified date on the basis of universal time.
31setUTCSeconds()setUTCSeconds() method sets the second value for the specified date on the basis of universal time.
32toDateString()toDateString() method returns the date portion of a Date object.
33toISOString()toISOString() method returns the date in the form ISO format string.
34toJSON()toJSON() method returns a string representing the Date object. It also serializes the Date object during JSON serialization.
35toString()toString() method returns the date in the form of string.
36toTimeString()toTimeString() method returns the time portion of a Date object.
37toUTCString()toUTCString() method converts the specified date in the form of string using UTC time zone.
38valueOf()valueOf() method returns the primitive value of a Date object.