Tech Study

JavaScript Date Format

Introduction to js date format

Javascript date format is a platform-independent format representing one and a single moment in time. It’s an intregal number representing milliseconds since January 1, 1970, UTC (the epoch) midnight at the beginning.

What are constructors in js date format?

  1. Date():  Current date and time representing a string are returned when using date() constructors.
  2. New date(): A new date object is returned using this constructor.

Types of JavaScript date formats

  • ISO Dates :  “2023-01-17”(international standard)
  • Short Date: “7-15-2020”
  • Long Date:  “Jan 17 2023” or “17 Jan 2023”

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

Output of JavaScript Date format

The default output dates for js date format, independent of the platform, is in full string format.

Mon Jan 16 2023 12:47:13 GMT+0530 (India Standard Time)

ISO DATES –

The international standard for dates and times is Iso 806 whose syntax (YYYY-MM-DD) is the preferred format for javascript date. js date format yyyy-mm-dd is standard format.

Syntax for Iso Date

const date = new Date(“2023-01-17”);

const date = new Date("2023-01-17");

ISO Dates (Year and Month) :

Date in Iso can be written in year and Month without day(YYYY-MM):

const date = new Date("2023-01-17");

 ISO DATES ( only year ) –

Date in Iso can be written in year only without month and day (YYYY):

const date = new Date("2023-01-17");

 ISO DATES ( date and time )

Date in Iso can be written adding time as hours minutes and seconds(YYYY-MM):

const date = new Date("2023-01-17T12:00:00Z");
  • capital letter  T is used to separate date and time in javascript.
  • capital letter Z is used to define UTC time
  • Add +HH:MM or -HH:MM if you want to change time relative to UTC , z can be removed as well.

Example:

const date = new Date("2023-01-17");

Short Dates in javascript.

js date format mm/dd/yyyy is the format for short dates.

Example :

const date = new Date("2023-01-17");

Error may be produced in some browsers when there is no leading zeros in month and days use (2017/05/22)

Long Dates in Javascript

‘MMM DD YYYY’ is the most used format for long dates.

Example :

const date = new Date("2023-01-17");

Month and date can be in any order

How to convert date format in javascript?

Methods of Conversion of Dates

To change or modify individual components of date and time [getDay() and getHours()] alternate version for the same method to change or modify the date and time using UTC[getUTCDay() and setUTCHours()].

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more