JavaScript Date Functions

Introduction to JavaScript Date Functions

In the previous tutorial, you learned about Math object in JavaScript so you don’t need to define each and every mathematical logic to get your desired result. Today in this tutorial you will learn about JavaScript Date Functions. After this tutorial, you will be able to use date functions in JavaScript and their working. In JavaScript, we have a Date object (a date class object) which helps to find some properties i.e. years, months, days, hours, minutes, seconds, and milliseconds using different functions.

Date Formats in JavaScript

There are two types of the format used to display the date using Data object.

  • Date as a string.
  • Date as a number.

Date as a string in JavaScript

Sun Jun 26 2016 08:17:38 GMT-0700 (Pacific Daylight Time)

Date as a number in JavaScript

1466954258279

When the date was written in number, then it will in milliseconds from 1st January 1970 and time 00:00:00.

Time Zones

Before we start and learn more about data, we must have knowledge of time zone. While using Data, you must have to define the time zone if you do not define the time zone the script use automatically the browser time zone and due to this, it may cause some errors in results.

In other words: If a date/time is created in EDT (Eastern Daylight Time), the date/time will be converted to CDT (Central US Daylight Time) if a user browses from the central US.

PDT    (US) Pacific Daylight Time

EST    (US) Eastern Standard Time

PST    (US) Pacific Standard Time

MDT    (US) Mountain Daylight Time

MST    (US) Mountain Standard Time

CST    (US) Central Standard Time

Display Date as a String in JavaScript

 

JavaScript Date Object Creation

To use date JavaScript have Data () class. And we have to create its object.

A date may consist of day, month, year, hour, second, etc.

As in JavaScript we use the new keyword to define or create an object of a class so in this we use new Date (); to create a date object.

There are four types of Date constructor:

  • new Date()
  • new Date(milliseconds)
  • new Date(date String)
  • new Date(year, month, day, hours, minutes, seconds, milliseconds)

 

Date Formats in JavaScript

In JavaScript, there are four types of date formats, but all these formats show full date string, but the only provided part is active like year or month.

  • Short Dates
  • ISO Dates
  • Full Format
  • Long Dates

Get Date in different Formats

Above we know different date formats but they does not allow you to skip the remaining part of the date string so to get a month or year or day or vice versa separately we use different function some of them are following:

getDate()

This method returns complete system date, along with the time and the time zone.

getMonth()

This method returns the current month according to system date.

getYear() deprecated because of year 2k problem

getFullYear()

This method returns the current complete year i.e. 2016 according to the current system’s date.

getHours()

This method returns the current hours according to system’s time.

getTime()

This function returns the number of milliseconds since 1970/01/01 as a string in JavaScript.
You can also get milliseconds since 1970/01/01 till the specified date.

getSeconds()

This function returns seconds part of current system’s time.

 Live Demo of JavaScript Date Functions