Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Date Formats

JS Date Formats

JavaScript Date format is specified in the ECMAScript specification.

JavaScript Date Input

There are generally 3 types of JavaScript date input formats:

ISO Date: "2023-07-01" (The International Standard)

Short Date: "06/01/2023"

Long Date: "Jul 01 2023" or "01 Jul 2023"

JavaScript Date Output

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

Sat Jul 01 2023 15:43:07 GMT+0530 (India Standard Time)

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.

The ISO format follows a strict standard in JavaScript. The other formats are not so well defined and might be browser specific.

ISO Complete Dates

ISO dates is specified by (YYYY-MM-DD) format.

The computed date will be relative to your time zone.

					 
        
          const d = new Date("2023-06-01");
        
      
ISO Year, Month Dates

ISO dates can be written without specifying the day (YYYY-MM).

					 
        
          const d = new Date("2023-06");
        
      
ISO Year Dates

ISO dates can be written without month and day (YYYY).

					 
        
          const d = new Date("2023");
        
      
ISO Date & Time

ISO dates can be written with added hours, minutes, and seconds (YYYY-MM-DDTHH:MM:SSZ).

Date and time is separated with a capital T.

UTC time is defined with a capital letter Z.

If you want to modify the time relative to UTC, remove the Z and add +HH:MM or -HH:MM instead. For India use +05:30 (GMT)

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

Omitting T or Z in a date-time string can give different results in different browsers.

					 
        
          const d1 = new Date("2023-06-01T12:00:00Z");
          const d2 = new Date("2023-06-01T12:00:00+05:30");
        
      
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 CDT (Central US Daylight Time) if a user browses from central US.

Short Dates

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

In some browsers, months or days with no leading zeroes may produce an error.

The behavior of "YYYY/MM/DD" and "DD-MM-YYYY" is undefined. Some browsers will try to guess the format. Some will return NaN.

					 
        
          const d1 = new Date("06/01/2023");
          const d2 = new Date("2023-6-1");
          const d3 = new Date("2023/06/01");
          const d4 = new Date("01-06-2023");
        
      
Long Dates

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

Month and day can be in any order "MMM DD YYYY" or "DD MMM YYYY".

Month can be written in full (July), or abbreviated (Jul):

Commas are ignored. Names are case insensitive.

					 
        
          const d1 = new Date("Jul 01 2023");
          const d2 = new Date("01 Jul 2023");
          const d3 = new Date("01 July 2023");
          const d4 = new Date("01, JUlY, 2023");
        
      
Parsing Dates Input

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.

					 
        
          let msec = Date.parse("Jul 01, 2023");
          const d = new Date(msec);
        
      
Displaying Dates

JavaScript will (by default) output dates using the toString() method. This is a string representation of the date, including the time zone. The format is specified in the ECMAScript specification.

toString() return string format of the date.

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

toUTCString() method converts a date to a string using the UTC standard.

toISOString() method converts a date to a string using the ISO standard.

					 
        
          const d = new Date("Jul 01, 2023");
          d.toString();     // Sat Jul 01 2023 00:00:00 GMT+0530 (India Standard Time)
          d.toDateString(); // Sat Jul 01 2023
          d.toUTCString();  // Fri, 30 Jun 2023 18:30:00 GMT
          d.toISOString(); // 2023-06-30T18:30:00.000Z
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved