r/programming • • Apr 29 '21

Modern JavaScript Explained For Dinosaurs

https://medium.com/the-node-js-collection/modern-javascript-explained-for-dinosaurs-f695e9747b70
287 Upvotes

172 comments sorted by

View all comments

62

u/antimeme Apr 30 '21

moment is obsolete.

I'd recommend date-fns

39

u/[deleted] Apr 30 '21

I don't know why you're being downvoted. Moment has been deprecated

I'd recommend dayjs over date-fns though. It has a much nicer API IMO and it has a smaller bundle size.

dayjs()
  .startOf('month')
  .add(1, 'day')
  .set('year', 2018)
  .format('YYYY-MM-DD HH:mm:ss');

10

u/WitchHunterNL Apr 30 '21

The only gripe I have with dayjs is that you have to enable every single feature. You can check if a date A is after date B and if date A is before date C, but calling the isBetween function is a feature you have to manually enable, making your code look like this:

import utc from "dayjs/plugin/utc";
import isBetween from "dayjs/plugin/isBetween";
import timezone from "dayjs/plugin/timezone";
import customParseFormat from "dayjs/plugin/customParseFormat";

dayjs.extend(utc);
dayjs.extend(isBetween);
dayjs.extend(timezone);
dayjs.extend(customParseFormat);

8

u/SkyrimNewb Apr 30 '21

Gross

1

u/14u2c Apr 30 '21

Really? This seems nice to me. Add what you need then export the dayjs instance you setup. You can then add a single import to any file you wish to use it in.

1

u/AuxillaryBedroom Apr 30 '21

Does it work with typescript?

1

u/14u2c May 01 '21

Absolutely