Working with dates in javascript

Dates in JavaScript

In this post we will go over through the process of manipulating date objects in javascript. Based on this javascript methods and objects we can effectively format the  dates in many ways. First of all lets have a look at the pic of code to get the current date in object. Later on in this article we will go over through the different javascript methods to manipulate it.

var dt = new Date();

Now simply write the above output in browser window with below code.

document.write(“Date : ” + dt );

and the output will be something like below.

Mon Sep 15 2016 23:20:38 GMT+0530 (India Standard Time)

Now this is not something like we wanted in our application. But java script have lots of inbuilt methods to manipulate dates to get desire output. Lets go through java script  methods to work with date.

Methods to get date
Method Description Example
getMilliseconds() Used to retrieve the milliseconds component of the date as a number(0 to 999) D = dt.getMilliseconds()
getSeconds() Used to retrieve the seconds component of the date as a number(0 to 59) D = dt.getSeconds()
getMinutes() Used to retrieve the minutes component of the date as a number(0 to 59) D = dt.getMinutes()
getHours() Used to retrieve the hours component of the date as a number(0 to 23) D = dt.getHours()
getDay() Used to retrieve the day of the week component of the date as a number(0 to 6 where (0=Sunday, 1=Monday, etc)) D = dt.getDay()
getDate() Used to retrieve the day-of-month component of the date as a number(1 to 31) D = dt.getDate()
getMonth() Used to retrieve the month component of the date as a number(0 to 11 where(0=January, 1=February, etc)) D = dt.getMonth()
getFullYear () Used to retrieve the year component of the date as a 4-digit number. D = dt.getFullYear ()
getUTCMilliseconds() Used to retrieve the milliseconds component of the date as a number(0 to 999) D = dt.getUTCMilliseconds()
getUTCSeconds() Used to retrieve the seconds component of the date as a number(0 to 59) D = dt.getUTCSeconds()
getUTCMinutes() Used to retrieve the minutes component of the date as a number(0 to 59) D = dt.getUTCMinutes()
getUTCHours() Used to retrieve the hours component of the date as a number(0 to 23) D = dt.getUTCHours()
getUTCDay() Used to retrieve the day of the week component of the date as a number(0 to 6 where (0=Sunday, 1=Monday, etc)) D = dt.getUTCDay()
getUTCDate() Used to retrieve the day-of-month component of the date as a number(1 to 31) D = dt.getUTCDate()
getUTCMonth() Used to retrieve the month component of the date as a number(0 to 11 where (0=January, 1=February, etc)) D = dt.getUTCMonth()
getUTCFullYear () Used to retrieve the year component of the date as a 4-digit number. D = dt.getUTCFullYear ()
getTimezoneOffset() Used to retrieve the time difference, in minutes, between the computer’s local time and GMT D = dt.getTimezoneOffset()

Now lets go over through the java script methods to set dates.

Methods to set date values
Method Description Example
setMilliseconds() Used to set the milliseconds component of the date using a number(0 to 999) D.setMilliseconds(500)
setSeconds() Ussed to set the seconds component of the date using a number(0 to 59) D.setSeconds(0)
setMinutes() Used to set the minutes component of the date using a number(0 to 59) D.setMinutes(45)
setHours() Used to set the hours component of the date using a number(0 to 23) D.setHours(20)
setDate() Used to set the day-of-month component of the date using a number(1 to 31) D.setDate(17)
setMonth() Used to set the month component of the date using a number(0 to 11 where (0=January, 1=February, etc)) D.setMonth(11)
setFullYear() Used to set the year component of the date using a 4-digit number. D.setFullYear(2002)
setUTCMilliseconds() Used to set the milliseconds component of the date using a number(0 to 999) D.setUTCMilliseconds(500)
setUTCSeconds() Used to set the seconds component of the date using a number(0 to 59) D.setUTCSeconds(0)
setUTCMinutes() Used to set the minutes component of the date using a number(0 to 59) D.setUTCMinutes(45)
setUTCHours() Used to set the hours component of the date using a number(0 to 23) D.setUTCHours(20)
setUTCDate() Used to set the day-of-month component of the date using a number(1 to 31) D.setUTCDate(17)
setUTCMonth() Used to set the month component of the date using a number(0 to 11 where (0=January, 1=February, etc)) D.setUTCMonth(11)
setUTCFullYear() Used to set the year component of the date using a 4-digit number. D.setUTCFullYear(2002)

Now lets go through some other useful date methods which are not covered in above sections.

Other useful Date methods
Method Description Example
parse() Used to convert a date text string to the “milliseconds since midnight Jan 1, 1970” format. We call it using Date.parse() rather than object.parse(). D = Date.parse
(“January 6, 1972”)
UTC(y, m, d, h, m, s) Used to convert the specified date to the “milliseconds since midnight Jan 1, 1970” format. We call it using Date.UTC() rather than object.UTC(). D = Date.UTC(1972, 0, 6)x = Date.UTC
(1972, 0, 6, 16, 5, 0)
toString() Used to get the value of the Date object as a date text string. D = dt.toString()
toUTCString() Used to get the value of the Date object as a date text string, using UTC. D = dt.toUTCString()
toLocaleString() Used to get the the value of the Date object as a date text string, using the computer’s locale conventions (e.g. D/M/Y for UK computers). D = dt.toLocaleString()
toDateString() Used to get the “date” portion of the Date object as a text string. D = dt.toDateString()
toTimeString() Used to get the “time” portion of the Date object as a text string. D = dt.toTimeString()

Leave a Reply