Skip to content
On this page

Date Handling

String Parsing

Usually start with a string as a source. Convert that to a Date object in Javascript

Within the native Date library documentation, there are many warnings:

Note: Parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.

Some browsers assume UTC, others assume local time. This is where libraries can help.


Date.parse('01 Jan 1970 00:00:00 GMT');


Options exist natively in newer versions of Javascript

      const start = new Date(Date.parse(value))
      const options = { month: 'long', year: 'numeric' }
      return start.toLocaleDateString('en-US', options)

To format a date for a filename, the following pattern works:

function toJSONLocal(date) {
  var local = new Date(date);
  local.setMinutes(date.getMinutes() - date.getTimezoneOffset());
  return local.toJSON().slice(0, 10);

let date = new Date();
let name = toJSONLocal(date);

adapted via:


var endDate = somedate;
var startdate = new Date(endDate);
var durationInMinutes = 20;
startdate.setMinutes(endDate.getMinutes() - durationInMinutes);
let stale = new Date();
stale.setMinutes(stale.getMinutes() - 5);

Adding Days

var date = new Date();
date.setDate(date.getDate() + days);
return date;

Days between dates

const _MS_PER_DAY = 1000 * 60 * 60 * 24;

// a and b are javascript Date objects
function dateDiffInDays(a, b) {
  // Discard the time and time-zone information.
  const utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
  const utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());

  return Math.floor((utc2 - utc1) / _MS_PER_DAY);

// test it
const a = new Date("2017-01-01"),
    b = new Date("2017-07-25"),
    difference = dateDiffInDays(a, b);


Both Day.js and date-fns stand out as good alternatives that should minimal impact to bundle size.

Day JS

API is similar to Moment JS. Very popular on Github. Weighs only 2KB!
iamkun/dayjs: ⏰ Day.js 2KB immutable date library alternative to Moment.js with the same modern API


Compatible with treeshaking, but with Day JS only at 2kb, maybe it's not important to tree shake?
date-fns - modern JavaScript date utility library
you-dont-need/You-Dont-Need-Momentjs: List of functions which you can use to replace moment.js + ESLint Plugin

to use in vue

yarn add date-fns
import { format, parseISO } from "date-fns";

export default {
  data() {
    return {


Date calculator. ~40kb


Wraps Intl library to modernize Moment JS:

Moment JS

Moment JS is a popular solution (as of 2020) for date handling in Javascript.

However, for what it does, Moment.js is pretty big in size. A lot of that comes from locales.

It may be possible to optimize what is included with a bundler, but it is still likely to be large.
will webpack ignore parts of momentjs not used? - Google Search
jmblog/how-to-optimize-momentjs-with-webpack: Explaining how to optimize the large bundle size of moment.js with webpack
size of moment js - Google Search
Angular CLI and Moment.js: A recipe for disaster … and how to fix it.
vue date filter - Google Search
How to format date for display - General Discussion - Vue Forum
vue-moment - npm
vue-moment/vue-moment.js at master · brockpetrie/vue-moment

YAML date-time at DuckDuckGo
Ruby: How do I define a datetime field in YAML? - Stack Overflow
Timestamp Language-Independent Type for YAML™ Version 1.1