Javascript
JavaScript Fundamentals -- Strings, Numbers, & Dates

Working with Dates

PRO
Outline

First off, working with dates in JavaScript is hard. There is a Date object that provides some functionality to help, but it still is difficult. If you're going to be working with dates frequently in your app, it's probably best to reach for a 3rd party library to work with dates. Two popular options are Moment.js and date-fns.

The basics of working with dates in JavaScript though, are as follows. You can create a new date object like this:

const today = new Date();
const mar1_2020 = new Date('2020-03-01');

You can pass a string in that represents a particular day. The format can be seen above, YYYY-MM-DD.

You can also get a reference to a current point in time by calling the Date.now() method. This method returns the number of milliseconds that have elapsed since Jan 1, 1970. Many applications will save this number in a database to timestamp when something occurred. You can pass that number into the Date constructor as well to create a date reference at a particular moment.

 

I finished! On to the next chapter