Skip to content

Instantly share code, notes, and snippets.

@simaodeveloper
Last active July 16, 2019 13:17
Show Gist options
  • Save simaodeveloper/7a92d935d10db9c934b1fc098ad47dc1 to your computer and use it in GitHub Desktop.
Save simaodeveloper/7a92d935d10db9c934b1fc098ad47dc1 to your computer and use it in GitHub Desktop.

Javascript - Module Date

Research based on this article: https://css-tricks.com/everything-you-need-to-know-about-date-in-javascript/

format

Estructure: 'YYYY-MM-DDTHH:mm:ss:sssZ'

format: ISO 8601 Extended Format

Example: 2019-01-14T05:30:00:123

Here's the values mean:

  • YYYY -> 4 digit year
  • MM -> 2 digit month (where january is 01 and December is 12)
  • DD -> 2 digit date (0 to 31)
  • - -> Date delimiters
  • T -> Indicates the start of time
  • HH -> 24 digit hour (0 to 23)
  • mm -> Minutes (0 to 59)
  • ss -> Seconds (0 to 59)
  • sss -> Milliseconds (0 to 999)
  • : -> Time delimiters
  • Z -> If 'Z' is present, date will be set to UTC. If 'Z' is no present. it'll be Local Time. ( This only applies if time is provided.)

######## WARNING #########

If you create a Date with a DateString without set a time, the result will be different if you live in areas behind GMT, to avoid this problem set the time 'HH' and 'mm' at a minimun.

Example: new Date('2019-06-11T00:00');

######## WARNING #########

Creating dates with arguments

You can pass in up to seven arguments to create a data/time.

  • Year: 4 digit year;
  • Month: Month of the year (0-11). Month is zero-indexed. Defaults to 0 omitted;
  • Day: Day of the month (1-31); Defaults to 1 if omitted;
  • Hour: Hour of the day (0-23). Defaults to 0 if omitted;
  • Minutes: Minutes (0-59). Defaults to 0 if omitted;
  • Seconds: Seconds (0-59). Defaults to - if omitted;
  • Milliseconds: Milliseconds (0-999). Defaults to 0 omitted;

Examples:

// year, month, day, hours, minutes, seconds, and milliseconds.
new Date(2019, 5, 11, 5, 23, 59);

// 21st March 1988, 12am, Local Time.
new Date(1988, 2, 21);

// 25th December 2019, 8am, Local time.
new Date(2019, 11, 25, 8);

// 6th November 2023, 2:20am, Local Time.
new Date(2023, 10, 6, 2, 20);

// 11th June 2019, 5:23:59am, Local Time.
new Date(2019, 5, 11, 5, 23, 59);

Dates created with arguments are all in local time, but if you want to create a date in UTC way, you just need to use a UTC method:

Example:

new Date(Date.UTC(2019, 5, 11));

Creating dates with timestamps

A timestamp is the amount of milliseconds elapsed since 1 january 1970 (Unix epoch time).

  • Tip: You only use timestamps to compare between different dates.
// 11th June 2019, 8am
new Date(1560211200000);

With no arguments

If you create a date without any arguments, you get a date set to the current time (in Local Time).

new Date();
  • You can create date with new Date();
  • There are four possible syntaxes:
    • With a date string;
    • With arguments;
    • With timestamp;
    • With no arguments;
  • Never create a date with the date string method.
  • It's best to create date eith the arguments method.
  • Remember (add accept) that month is zero-indexed in JavaScript.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment