Monday, April 6, 2020

The ultimate guide to JavaScript Date object



Hello guys! morning to you all. I welcome you to this new tutorial post on JavaScript. In my previous tutorial post we discussed about how to implement the various JavaScript array methods available. So without wasting much time, am going to introduce a new topic to you guys. In this tutorial, we are going to be discussing about JavaScript Date Object. So i will suggest you read through very carefully and also ask questions in areas you find confusing.

The Date object is a datatype built into the JavaScript language. Date objects are created with the new  Date() syntax as shown below.

Once a Date object is created, a number of methods allow you to work with it. Most methods simply allow you to get and set the the year, month, day, hour, minutes, seconds and millisecond fields of the object, using either  local time or UTC (universal or GMT) time.

The ECMAScript standard requires the Date object to be able to represent any date and time, to millisecond  precision, within a range of 100 million days before or after 1/1/1970. This is a range of plus or minus 273,785 years, so JavaScript can represent date and time till the year 275755.

You can also read one of our tutorial post on: How to enable JavaScript

Syntax

You can make use of any of the syntaxes to create a Date object using Date() constructor.

new Date()
new Date(milliseconds)
new Date(datestring)
new Date(year, month, date [, hour, minute, second, millisecond   ]  )         

Note - Parameters in the brackets are always optional. 

Below is the description of all the parameters listed above -

  • No Argument - With no arguments, JavaScript Date() constructor creates a Date object set to the current date and time.
  • milliseconds - When one numeric argument is passed, it is taken as the internal numeric representation of the date  in milliseconds, as returned by the getTime() method. For example, passing the argument 5000 creates a date that represents five seconds past midnight on 1/1/70.
  • datestring - When one string argument is passed, it is a string representation of a date, in the format that is accepted by the Date.parse() method.
  • 7 arguments - In order to use the last form of the constructor shown above. Here is the description of each argument -
    • year - Integer value representing the year. For compatibility issues  ( in order to avoid the Y2K problem ), you should always specify the year in full; use 1993, rather than 93.
    • month - Integer value representing the month, beginning with number 0 for January to 11 for December.
    • date - Integer value representing the day of the month.
    • hour - Integer value representing the hour of the day (24-hour scale).
    • second - Integer value representing the second segment of a time reading.
    • millisecond - Integer value representing the millisecond segment of a time reading.

You can also read our tutorial post on: Learn how to implement JavaScript Literals with examples


Date Properties

Below is the list of the properties of the Date object along with their various description.

Sr.No.Property & Description
1constructor
Specifies the function that creates an object's prototype.
2prototype
The prototype property allows you to add properties and methods to an object

In the up coming tutorial,  we will be using few examples to illustrate the proper usage of Date properties.


Date Methods

Below is the list of the methods used with JavaScript Date object and their description.

Sr.No.Method & Description
1Date()
Returns today's date and time
2getDate()
Returns the day of the month for the specified date according to local time.
3getDay()
Returns the day of the week for the specified date according to local time.
4getFullYear()
Returns the year of the specified date according to local time.
5getHours()
Returns the hour in the specified date according to local time.
6getMilliseconds()
Returns the milliseconds in the specified date according to local time.
7getMinutes()
Returns the minutes in the specified date according to local time.
8getMonth()
Returns the month in the specified date according to local time.
9getSeconds()
Returns the seconds in the specified date according to local time.
10getTime()
Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC.
11getTimezoneOffset()
Returns the time-zone offset in minutes for the current locale.
12getUTCDate()
Returns the day (date) of the month in the specified date according to universal time.
13getUTCDay()
Returns the day of the week in the specified date according to universal time.
14getUTCFullYear()
Returns the year in the specified date according to universal time.
15getUTCHours()
Returns the hours in the specified date according to universal time.
16getUTCMilliseconds()
Returns the milliseconds in the specified date according to universal time.
17getUTCMinutes()
Returns the minutes in the specified date according to universal time.
18getUTCMonth()
Returns the month in the specified date according to universal time.
19getUTCSeconds()
Returns the seconds in the specified date according to universal time.
20getYear()
Deprecated - Returns the year in the specified date according to local time. Use getFullYear instead.
21setDate()
Sets the day of the month for a specified date according to local time.
22setFullYear()
Sets the full year for a specified date according to local time.
23setHours()
Sets the hours for a specified date according to local time.
24setMilliseconds()
Sets the milliseconds for a specified date according to local time.
25setMinutes()
Sets the minutes for a specified date according to local time.
26setMonth()
Sets the month for a specified date according to local time.
27setSeconds()
Sets the seconds for a specified date according to local time.
28setTime()
Sets the Date object to the time represented by a number of milliseconds since January 1, 1970, 00:00:00 UTC.
29setUTCDate()
Sets the day of the month for a specified date according to universal time.
30setUTCFullYear()
Sets the full year for a specified date according to universal time.
31setUTCHours()
Sets the hour for a specified date according to universal time.
32setUTCMilliseconds()
Sets the milliseconds for a specified date according to universal time.
33setUTCMinutes()
Sets the minutes for a specified date according to universal time.
34setUTCMonth()
Sets the month for a specified date according to universal time.
35setUTCSeconds()
Sets the seconds for a specified date according to universal time.
36setYear()
Deprecated - Sets the year for a specified date according to local time. Use setFullYear instead.
37toDateString()
Returns the "date" portion of the Date as a human-readable string.
38toGMTString()
Deprecated - Converts a date to a string, using the Internet GMT conventions. Use toUTCString instead.
39toLocaleDateString()
Returns the "date" portion of the Date as a string, using the current locale's conventions.
40toLocaleFormat()
Converts a date to a string, using a format string.
41toLocaleString()
Converts a date to a string, using the current locale's conventions.
42toLocaleTimeString()
Returns the "time" portion of the Date as a string, using the current locale's conventions.
43toSource()
Returns a string representing the source for an equivalent Date object; you can use this value to create a new object.
44toString()
Returns a string representing the specified Date object.
45toTimeString()
Returns the "time" portion of the Date as a human-readable string.
46toUTCString()
Converts a date to a string, using the universal time convention.
47valueOf()
Returns the primitive value of a Date object.

In the subsequent tutorials, i will be making use of series of examples to demonstrate the usage of all these Date methods listed above.

You can also check out our tutorial post on: Steps on how to implement JavaScript Nested functions with examples


Date Static Methods

In addition to the many instance methods listed above, the Date object also defines two static methods. These Date static methods are invoked through the Date() constructor itself.

Sr.No.Method & Description
1Date.parse( )
Parses a string representation of a date and time and returns the internal millisecond representation of that date.
2Date.UTC( )
Returns the millisecond representation of the specified UTC date and time.

In the subsequent tutorials, we will have few examples to demonstrate the usages of Date Static methods.

Alright guys! we have come to the end of this tutorial on JavaScript Date object. In my next tutorial post, i will be using few examples to demonstrate how to implement the various Date properties available. Feel free to drop your questions via the comment box below.

Like our facebook page and also follow us on the rest of our social media platforms. You can also subscribe to our newsletter to get our tutorial posts delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment