JavaScript Date objects and methods

JavaScript Date Objects


JavaScript Date Object lets us work with dates:


Example:


JavaScript Date Output


By default, JavaScript uses the browser's time zone and display a date as a full text string:

Fri Oct 18 2019 11:35:53 GMT+0530 (IST)

Creating Date Objects


Date objects are created with the new Date() constructor.

There are 4 ways to create a new date object:

  • new Date()

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

  • new Date(milliseconds)

  • new Date(date string)

new Date()


new Date() creates a new date object with the current date and time.


Example:

Date objects are static. The computer time is ticking, but date objects are not.


new Date(year, month, ...)

  • new Date(year, month,...) creates a new date object with a specified date and time.

  • 7 numbers specify year, month, day, hour, minute, second, and millisecond (in that order):

  • 6 numbers specify year, month, day, hour, minute, second.

  • 5 numbers specify year, month, day, hour, and minute.

  • 4 numbers specify year, month, day, and hour.milliseconds.

  • 3 numbers specify year, month, and day.

  • 2 numbers specify year and month.

  • Month can not be omitted. If only one parameter is supplied, it will be treated as milliseconds.

Example:

Note:-

  • JavaScript counts months from 0 to 11.

  • January is 0. December is 11.


Previous Century


One and two digit years will be interpreted as 19xx:


Example:


JavaScript Stores Dates as Milliseconds

  • JavaScript stores dates as number of milliseconds since January 01, 1970, 00:00:00 UTC (Universal Time Coordinated).

  • Zero time is January 01, 1970 00:00:00 UTC.

  • Now the time is: 1571378753606 milliseconds past January 01, 1970


new Date(milliseconds)

  • new Date(milliseconds) creates a new date object as zero time plus milliseconds.

  • Using new Date(milliseconds), creates a new date object as January 1, 1970, 00:00:00 Universal Time (UTC) plus the milliseconds.

Example:

Note:- One day (24 hours) is 86 400 000 milliseconds.


Date Methods

  • When a Date object is created, a number of methods allow you to operate on it.

  • Date methods allow you to get and set the year, month, day, hour, minute, second, and millisecond of date objects, using either local time or UTC (universal, or GMT) time.


Displaying Dates

  • JavaScript will (by default) output dates in full text string format.

  • When you display a date object in HTML, it is automatically converted to a string, with the toString() method.

  • The toUTCString() method converts a date to a UTC string (a date display standard).

  • The toDateString() method converts a date to a more readable format.

Example:


JavaScript Date Formats


JavaScript Date Input


There are generally 3 types of JavaScript date input formats:


Type Example

  • ISO Date "2015-03-25" (The International Standard)

  • Short Date "03/25/2015"

  • Long Date "Mar 25 2015" or "25 Mar 2015"

The ISO format follows a strict standard in JavaScript.

The other formats are not so well defined and might be browser specific.


JavaScript Date Output

Independent of input format, JavaScript will (by default) output dates in full text string format:

Wed Mar 25 2015 05:30:00 GMT+0530 (IST)


JavaScript ISO Dates

  • ISO 8601 is the international standard for the representation of dates and times.

  • The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format.

Example (Complete date)

  • The computed date will be relative to your time zone.

  • Depending on your time zone, the result above will vary between March 24 and March 25.


Note:- UTC (Universal Time Coordinated) is the same as GMT (Greenwich Mean Time).


Time Zones

  • When setting a date, without specifying the time zone, JavaScript will use the browser's time zone.

  • When getting a date, without specifying the time zone, the result is converted to the browser's time zone.

  • In other words: If a date/time is created in GMT (Greenwich Mean Time), the date/time will be converted to IST (Indian Standard Time) if a user browses from India.


JavaScript Short Dates.


Short dates are written with an "MM/DD/YYYY" syntax like this.


Example:


JavaScript Long Dates.

  • Long dates are most often written with a "MMM DD YYYY" syntax like this.

  • Month and day can be in any order.

  • And, month can be written in full (January), or abbreviated (Jan).

  • Commas are ignored. Names are case insensitive.

Example:


Date Input - Parsing Dates

  • If you have a valid date string, you can use the Date.parse() method to convert it to milliseconds.

  • Date.parse() returns the number of milliseconds between the date and January 1, 1970.

  • You can then use the number of milliseconds to convert it to a date object.

Example:


JavaScript Get Date Methods


These methods can be used for getting information from a date object:


Method Description

  • getFullYear() Get the year as a four digit number (yyyy)

  • getMonth() Get the month as a number (0-11)

  • getDate() Get the day as a number (1-31)

  • getHours() Get the hour (0-23)

  • getMinutes() Get the minute (0-59)

  • getSeconds() Get the second (0-59)

  • getMilliseconds() Get the millisecond (0-999)

  • getTime() Get the time (milliseconds since January 1, 1970)

  • getDay() Get the weekday as a number (0-6)

  • Date.now() Get the time. ECMAScript 5.


The getTime() Method


The getTime() method returns the number of milliseconds since January 1, 1970:


Example:


The getMonth() Method

  • The getMonth() method returns the month of a date as a number (0-11).

  • In JavaScript, the first month (January) is month number 0, so December returns month number 11.

  • You can use an array of names, and getMonth() to return the month as a name.

Example:


The getDate() Method


The get Date() method returns the day of a date as a number (1-31).


Example:


UTC Date Methods


UTC date methods are used for working with UTC dates (Universal Time Zone dates):


Method Description

  • getUTCDate() Same as getDate(), but returns the UTC date

  • getUTCDay() Same as getDay(), but returns the UTC day

  • getUTCFullYear() Same as getFullYear(), but returns the UTC year

  • getUTCHours() Same as getHours(), but returns the UTC hour

  • getUTCMilliseconds() Same as getMilliseconds(), but returns the UTC milliseconds

  • getUTCMinutes() Same as getMinutes(), but returns the UTC minutes

  • getUTCMonth() Same as getMonth(), but returns the UTC month

  • getUTCSeconds() Same as getSeconds(), but returns the UTC seconds

JavaScript Set Date Methods


Set Date methods let you set date values (years, months, days, hours, minutes, seconds, milliseconds) for a Date Object.


Set Date Methods


Set Date methods are used for setting a part of a date:


Method Description

  • setDate() Set the day as a number (1-31)

  • setFullYear() Set the year (optionally month and day)

  • setHours() Set the hour (0-23)

  • setMilliseconds() Set the milliseconds (0-999)

  • setMinutes() Set the minutes (0-59)

  • setMonth() Set the month (0-11)

  • setSeconds() Set the seconds (0-59)

  • setTime() Set the time (milliseconds since January 1, 1970)


The setFullYear() Method

  • The setFullYear() method sets the year of a date object. In this example to 2020.

  • The setFullYear() method can optionally set month and day.

Example:


The setHours() Method


The setHours() method sets the hours of a date object (0-23):


Example:


Compare Dates

  • Dates can easily be compared.

  • JavaScript counts months from 0 to 11. January is 0. December is 11.

  • The following example compares today's date with January 14, 2100

Example:



Contact Us

Tel: (+91) 0120  4118730  

Time :   10 : 00  AM -  08 : 00 PM IST 

Registered address: G-69, Sector 63, 

 Noida - 201301, India

We Provide Services Across The different countries

USA    Australia   Canada   UK    UAE    Singapore   New Zealand    Malasia   India   Ireland   Germany

CodersArts is a Product by Sofstack Technology Solutions Pvt. Ltd.

  • CodersArts | Linkedin
  • Instagram