tutorialstonight logo
A Platform To Learn To Code

JAVASCRIPT DATE



Javascript date is fundamentally the number of milliseconds that have elapsed since midnight on January 1, 1970, UTC.

Javascript provides Date object to fetch date and time. The Date object stores the current date, time and multiple methods to manage date and time.

Javascript Date object represents a single moment in time. Time value in the core of Date object is UTC, the basic method to fetch date and time all works on the local time zone.

By default, javascript will use the browser's time Zone
year: ?
month: ?
day: ?
hour: ?
minute: ?
second: ?

How to Create Date object in Javascript

The Date object in javascript is created using Date() constructor. Look at the example below.

Example

Run the code

There are 4 different ways to create Date object 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 javascript

The new Date() return current date and time.

Example

Run the code

2. new Date milliseconds javascript

The Second method of creating a date object is new Date(milliseconds). This method return date and time passed after given milliseconds from midnight 1 January 1970 UTC.

Example

Run the code

3. new Date(dateString) Javascript

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. This return standard date based on passed arguments. 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 an integer value representing a date.
  • hours: It is an integer which represents hour on a 24-hour scale.
  • minutes: It is an integer value that represents the minute.
  • second: It is an integer value that represents second.
  • milliseconds: It is an integer value that represents milliseconds.

Among these 7 parameters, last 5 are optional but it is necessary to provide year and month.

Example

Run the code

Javascript date methods

The Date object in javascript provides various methods to get specific information from the 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)

Let's see an 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