fbpx
April 6, 2020
javascript date object

JAVASCRIPT DATE | Introduction To JavaScript Date Object

In this tutorial we will be discussing about JavaScript date object which entails clearly how to use the date object and constructor in JavaScript. In our previous tutorial we looked at the JavaScript array and we discussed all the properties and methods associated with arrays in JavaScript.  

Read previous tutorial on: JAVASCRIPT ARRAY | Introduction to Array Objects In JavaScript

JAVASCRIPT DATE OBJECT

When coding in JavaScript, sometimes you might come across the need to make use of dates. Maybe you want to display the date or you want an action to be carried out on a specific date. Such activity can be achieved with the help of the date object in JavaScript.

The date objects by default is created using the new date() constructor. Once the date object has been initially created, we can pass in methods that can be used to operate on it fully. These methods are used to structure the year, month, days, hours, seconds and the milliseconds fields found in the date object either by using the local time  or the universal timing.

DATE OBJECT SYNTAX

The syntax for the date object is shown below

New Date( )

New Date(year,month,date,[hour,minute,second,millisecond])

Please note that the date constructor is structured in the format Date(). It must start with a capital letter ā€œDā€.

POINTS TO NOTE ABOUT THE DATE OBJECT

  • EMPTY ARGUMENT: if the argument field is empty the date() sets the date to the current date and time.
  • Milliseconds: when a single numeric value is passed to the date object as the argument, the date object takes this value in milliseconds
  • Datestring: when a string value is passed in, it becomes a string representation for date as accepted by the Date.parse() method.

JAVASCRIPT DATE PROPERTIES

Below shows a list of some of the properties of the JavaScript date object.

S/N DATE OBJECT PROPERTY PROPERTY DESCRIPTION
1 constructor This is simply the function that is used to create the date property
2 Prototype This allows a method and property to be passed to an object

JAVASCRIPT DATE OBJECT METHODS

Below list shows some of the date methods used in javascript

METHOD DESCRIPTION
Date() This is used to specify the current date and time
getDay() This is to specify the day of the week for a specified date
getDate() This is use to specify the day of the month for a specified date
getHours() The specifies the hours in a specified date
getMilliseconds() The specifies the milliseconds in a specified date
getFullYear() The specifies the year in a specified date
getMonth() The specifies the months in a specified date
getSeconds() The specifies the seconds in a specified date
getMinutes() The specifies the minutes in a specified date
getTime() This is used to return the numeric value of the specified date
getUTCDate() This specifies the day of the month based on the specified date in universal time format
getUTCFullYear() This specifies the year in the specified date in a universal format
getUTCHours() This specifies the hours in the specified date in a universal format
getUTCMinutes() This specifies the minutes in the specified date in a universal format
getUTCMilliseconds() This specifies the milliseconds in the specified date in a universal format
getUTCSeconds() This specifies the seconds in the specified date in a universal format
getUTCMonth() This specifies the month in the specified date in a universal format
setDate() This is used to set the date of the month for a specified date based on local timing
getUTCDay() This returns the day of the week in universal timing format for a specified date
setFullYear() This is used to structure the full year for a given date based on local time
setHours() This is used to structure the hours for a given date based on local time
setMilliseconds() This is used to structure the milliseconds for a given date based on local time
setMonth() This is used to structure the month for a given date based on local time
setMinutes() This is used to structure the minutes for a given date based on local time
setSeconds() This is used to structure the seconds for a given date based on local time
setTime() This sets the time using number values
setUTCDate() This is used to structure the day of the month for a given date based on universal time
setUTCFullYear() This is used to structure the full year for a given date based on universal time
setUTCHours() This is used to structure the hours for a given date based on universal time
setUTCMilliseconds() This is used to structure the milliseonds for a given date based on universal time
setUTCMinutes() This is used to structure the minutes  for a given date based on universal time
setUTCMonths() This is used to structure the month for a given date based on universal time
setUTCSeconds() This is used to structure the seconds for a given date based on universal time
setYear() This is used to structure the year for a given date based on universal time
toLocalString() This converts the date to a string using the locale format
toString() This is used to return a string which represents the date
valueOf() This is used to specify the value of the date object
toTimestring() This returns the time using strings
toUTCString() This returns the time in strings using the universal time format
toLocaleFormat() This returns the time in strings using the locale time format
toDateString() This returns the date using strings

THE DATE STATIC METHODS

Below list are the static methods used with the date object

S/N STATIC METHOD DESCRIPTION
1 Date.parse() This accepts the string representation of the date and time and returns the date and time in milliseconds
2 Date.UTC() This is used to return the millisecond representation of the giving UTC date and time

NEXT TOPIC: JAVASCRIPT MATH OBJECT

Leave a Reply

Your email address will not be published. Required fields are marked *