DEV Community

myleftshoe
myleftshoe

Posted on

Javascript dates in milliseconds

I find the js Date object somewhat cumbersome, it's often easier just to work with milliseconds and convert to the required format when presenting to the user. Here's a small lib that does just that! It's simple, flexible, and functional.

https://github.com/myleftshoe/msdate

Examples

Simple


import { now, today, days, weeks, years, toDate } from '@mls44/msdate'

now
 1592692591560

today
 1592661600000

fromDate(new Date())
 1592692591566

toDate(today)
 2020-06-20T14:00:00.000Z

toDate(today + 1 * days)
 2020-06-21T14:00:00.000Z

toDate(now + 1 * days)
 2020-06-21T22:36:31.560Z

toDate(today + 4 * weeks)
 2020-07-18T14:00:00.000Z

toDate(today + 10 * years)
 2030-06-21T02:00:00.000Z

Enter fullscreen mode Exit fullscreen mode

Ranges


import { arrayOf, days, today, toDate } from '@mls44/msdate'

arrayOf(days, 7).from(today)
 [
  1592575200000,
  1592661600000,
  1592748000000,
  1592834400000,
  1592920800000,
  1593007200000,
  1593093600000
]

arrayOf(days, 7, toDate).from(today)
 [
  2020-06-19T14:00:00.000Z,
  2020-06-20T14:00:00.000Z,
  2020-06-21T14:00:00.000Z,
  2020-06-22T14:00:00.000Z,
  2020-06-23T14:00:00.000Z,
  2020-06-24T14:00:00.000Z,
  2020-06-25T14:00:00.000Z
]

arrayOf(days, 7, { weekday: 'long' }).from(today)
 [
  'Saturday',
  'Sunday',
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday'
]

// Or use between:

arrayOf(days, { weekday: 'long' }).between(today, today + days * 7)
 [
  'Saturday',
  'Sunday',
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday'
]

Enter fullscreen mode Exit fullscreen mode

In the above examples, you need to import days. Alternatively you can call arrayOf like this:

arrayOf('days', 7)
arrayOf({ days: 7})
Enter fullscreen mode Exit fullscreen mode

Formatting

msdate uses Intl.DateTimeFormat. Provide an options object as per the spec to format.

arrayOf also accepts the same object (or even a function like toDate) as an optional second argument (see above examples).


import { now, today, toDate, format } from '@mls44/msdate'

format(now,  toDate)
 2020-06-20T23:20:27.448Z

format(now, { weekday: 'long' })
 Sunday

Enter fullscreen mode Exit fullscreen mode

Functional

toDate, fromDate, and format can be used functionally, e.g. [...].map(toDate).


import { now, today, arrayOf, days, toDate, fromDate, format } from '@mls44/msdate'

arrayOf(days, 7).from(today).map(toDate)
 [
  2020-06-20T14:00:00.000Z,
  2020-06-21T14:00:00.000Z,
  2020-06-22T14:00:00.000Z,
  2020-06-23T14:00:00.000Z,
  2020-06-24T14:00:00.000Z,
  2020-06-25T14:00:00.000Z,
  2020-06-26T14:00:00.000Z
]

arrayOf(days, 7).from(today).map(format({ weekday: 'long' }))
 [
  'Sunday',
  'Monday',
  'Tuesday',
  'Wednesday',
  'Thursday',
  'Friday',
  'Saturday'
]

[
    new Date('2020-06-19T14:00:00.000Z'),
    new Date('2020-06-20T14:00:00.000Z'),
    new Date('2020-06-21T14:00:00.000Z'),
    new Date('2020-06-22T14:00:00.000Z'),
    new Date('2020-06-23T14:00:00.000Z'),
    new Date('2020-06-24T14:00:00.000Z'),
    new Date('2020-06-25T14:00:00.000Z')
].map(fromDate)
 [
  1592575200000,
  1592661600000,
  1592748000000,
  1592834400000,
  1592920800000,
  1593007200000,
  1593093600000
]

Enter fullscreen mode Exit fullscreen mode

It's a small lib that meets a specific requirement rather than a swiss army knife.

That's it, thanks for reading!

Top comments (0)

Some comments have been hidden by the post's author - find out more