Date and Time in JavaScript

Hi guys,

In this post we will walk through the Date object that is available in JavaScript and the various capabilities of that object and various ways can use it in our application

Date in JavaScript is powerful enough to suffice for most of the requirements we have in our applications. Interesting thing to note here is that numeric value of date is the number of milliseconds that have lapsed after 01Jan1970 UTC (all the leap time is ignored).

Another interesting thing that we can do with date in JavaScript is create a future date by adding days, hours, months, etc.

The various methods available on Date are:

getDate() Returns the day of the month (from 1-31)
getDay() Returns the day of the week (from 0-6)
getFullYear() Returns the year (four digits)
getHours() Returns the hour (from 0-23)
getMilliseconds() Returns the milliseconds (from 0-999)
getMinutes() Returns the minutes (from 0-59)
getMonth() Returns the month (from 0-11)
getSeconds() Returns the seconds (from 0-59)
getTime() Returns the number of milliseconds since midnight Jan 1, 1970
getTimezoneOffset() Returns the time difference between UTC time and local time, in minutes
getUTCDate() Returns the day of the month, according to universal time (from 1-31)
getUTCDay() Returns the day of the week, according to universal time (from 0-6)
getUTCFullYear() Returns the year, according to universal time (four digits)
getUTCHours() Returns the hour, according to universal time (from 0-23)
getUTCMilliseconds() Returns the milliseconds, according to universal time (from 0-999)
getUTCMinutes() Returns the minutes, according to universal time (from 0-59)
getUTCMonth() Returns the month, according to universal time (from 0-11)
getUTCSeconds() Returns the seconds, according to universal time (from 0-59)
getYear() Deprecated. Use the getFullYear() method instead
parse() Parses a date string and returns the number of milliseconds since midnight of January 1, 1970
setDate() Sets the day of the month of a date object
setFullYear() Sets the year (four digits) of a date object
setHours() Sets the hour of a date object
setMilliseconds() Sets the milliseconds of a date object
setMinutes() Set the minutes of a date object
setMonth() Sets the month of a date object
setSeconds() Sets the seconds of a date object
setTime() Sets a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970
setUTCDate() Sets the day of the month of a date object, according to universal time
setUTCFullYear() Sets the year of a date object, according to universal time (four digits)
setUTCHours() Sets the hour of a date object, according to universal time
setUTCMilliseconds() Sets the milliseconds of a date object, according to universal time
setUTCMinutes() Set the minutes of a date object, according to universal time
setUTCMonth() Sets the month of a date object, according to universal time
setUTCSeconds() Set the seconds of a date object, according to universal time
setYear() Deprecated. Use the setFullYear() method instead
toDateString( Converts the date portion of a Date object into a readable string
toGMTString() Deprecated. Use the toUTCString() method instead
toISOString() Returns the date as a string, using the ISO standard
toJSON() Returns the date as a string, formatted as a JSON date
toLocaleDateString() Returns the date portion of a Date object as a string, using locale conventions
toLocaleTimeString() Returns the time portion of a Date object as a string, using locale conventions
toLocaleString() Converts a Date object to a string, using locale conventions
toString() Converts a Date object to a string
toTimeString() Converts the time portion of a Date object to a string
toUTCString() Converts a Date object to a string, according to universal time
UTC() Returns the number of milliseconds in a date string since midnight of January 1, 1970, according to universal time
valueOf() Returns the primitive value of a Date object

Source – http://www.w3schools.com/jsref/jsref_obj_date.asp

Accessing the current date and parts of the date

We can create a Date object in Javascript that returns the current date time in current time zone. This happens when we do not pass any parameters while passing the Date object.

We pass different types of parameters to create the date we are looking for. We can pass

Number of milliseconds that have lapsed after 01 Jan 1970 00:00:00:000 UTC –> will return the date adding the lapsed milliseconds to base date of 01 Jan 1970 00:00:00:000 UTC.

String that is parseable into date –> will return the date that is parsed from the string.

Year, Month, Day, Hours, Minutes, Seconds, Milliseconds –> A date will be created if we pass just the Year, Month and Day. The remaining parameters will be considered as 0.

We can use the methods that return the individual part of the date.

Accessing dates in UTC timezone

We also have methods which return the UTC equivalent of the date. So whatever operations that we have done till now on the local date we can perform all of them to get the UTC time and date.

Get CurrentTime

It returns the number of milliseconds that have lapsed since 01 Jan 1970 00:00:00:000 UTC

Converting Date To and From ISO 8601 format

Convert Date in ISO 8601 Format

There is a method (toISOString) available on the Date object. It returns the date in ISO format which looks something like below:

2013-07-29T18:56:20.370Z

This format separates the date and time by placing T in between them. Also if this string is in UTC timezone it is suffixed by Z and if it is not in the UTC timezone then it tells the time difference between the current timezone and UTC timezone by +hh:mm or –hh:mm.

Convert ISO 8601 date into standard format accepted by Date method

If we are using older versions of the browser and we try to use the string date returned by the toISOString as a parameter to create a date then we will get an invalid date error. So before we pass this string to create a date we need to convert it into a format acceptable by the Date object. So could use the function below that will convert the ISO 8601 string into format acceptable by the date object.

However if we are running the latest browsers we could simply pass the ISO 8601 formatted string as a parameter to the date object and we will get the localized date.

Create future date

We can do that by either passing the individual parts of the date or use the set method available to set the values of the individual parts of the date.

Find time elapsed

There are times when we need to know how long a function takes to execute. So do that all we need to do is create a Date object before we call the function and then we create another object when the control returns back from the function and then we subtract both the times.

When we apply mathematical operations on date apart from subtraction we get different results.

Date + Date = string concatenation of date

Date – Date = Difference of milliseconds equivalent of date

Date / Date = Division of milliseconds equivalent of the date

Date * Date = Multiplication of milliseconds equivalent of date

The code for this post is available here.

Any comments, feedback or questions are most welcome!!