A Platform To Learn To Code

JAVASCRIPT DATE



Date is build-in object in javascript. It stores current date, time and multiple methods to manage date and time.

Javascript Date object represent single moment in time.

year: ?
month: ?
day: ?
hour: ?
minute: ?
second: ?

Lets see an example how to create Date object.

Example

Run the code

Output:

By default javascript will use the browser's time Zone

How to Create Date object

The Date in javascript is created by constructor using new keyword.

There are 4 different ways to create Date in javascript.

  1. new Date()
  2. new Date(milliseconds)
  3. new Date(dateString)
  4. new Date(year, month, day, hours, minutes, seconds, milliseconds)

1. new Date()

The new Date() return current date and time.

Example

Run the code

2. new Date(millisecons)

JavascriptDate specifies number of milliseconds elapsed since midnight 1 january, 1970 UTC.

Second method of creating date is new Date(milliseconds). Passing millisecond in constructor parameter return date and time at passed milliseconds from midnight 1 january, 1970 UTC.

Example

Run the code

3. new Date(dateString)

The new Date(dateString) accepts a String parameter named dateString. The String represents a date and new Date(dateString) return a standard date using the String.

Example

Run the code

Output:


4. new Date(year, month, day, hours, minutes, seconds, milliseconds)

The new Date(year, month, day, hours, minutes, seconds, milliseconds) accepts 7 parameters. Lets look at all the 7 parameters:

  • year: It is interger value that represent year. Value passed for year should be full. example 2012 not 12. 2 digit year will be represented as 19xx.
  • month: It is integer value from 0 to 11 representing january(0) to december(11).
  • day: It is integer value representing date.
  • hours: It is integer which represent hour on 24-hour scale.
  • minutes: It is integer value that represent the minute.
  • second: It is integer value that represent second.
  • milliseconds: It is integer value that represent milliseconds.

Example

Run the code

Output:


Javascript date methods

Javascript Date method is used to get information from Date object.

Sr no. Method Description
1 getFullYear() Return current year as 4 digit number (yyyy)
2 getMonth() Return current month in number from 0 to 11
3 getDate() Return current date in number (1-31)
4 getHours() Return current hour in 24hour clock (0-23)
5 getMinutes() Return current minute in number (0-59)
6 getSeconds() Return current second in number (0-59)
7 getMilliseconds() Return current millisecond in number (0-999)

Lets see example for each method.

1. getFullYear() Method

getFullYear() returns full year of current date.

Use getFullYear() not getYear() which return current year in 2 digit and is deprecated.

Example: getFullYear() Method

Run the code

2. getMonth() Method

getMonth() method returns current month of the date in number from 0 to 11. 0 represent 'January' and 11 represent 'December'.

Example: getMonth()

Run the code

3. getDate() Method

getDate() method returns current date of the day in number from 1 to 11.

Example: getDate()

Run the code

4. getHours() Method

getHours() method returns current hour in form of 24 hour clock from 0-23.

Example: getHours()

Run the code

5. getMinutes() Method

getMinutes() method returns current second in number from 0-59.

Example: getMinutes()

Run the code

6. getSeconds() Method

getSeconds() method returns current second in number from 0-59.

Example: getSeconds()

Run the code

7. getMilliseconds() Method

getMilliseconds() method returns current millisecond in number from 0-999.

Example: getMilliseconds()

Run the code