How to get ISO time in JavaScript

By Ev Bogue - August 10th 2015

[Update 2016: you can also use the command

% date -Im

To get ISO time from your terminal]

I had this strange thing I had come up, which I want to tell you about.

It's an error that appears everytime I run Metalwork, which uses Moment.js to render dates.

The problem is that if I use a date output by the UNIX command date it looks similar to this:

Mon Aug 10 16:20:30 CDT 2015

But Moment.js for what seem to be somewhat anal reasons, doesn't want to parse this date. It does parse it, and the parse works fine. However, I got this error demanding that I use a thing called ISO time instead of the standard UNIX format of time which has some other name I'm sure.

Anyway, Moment.js is sending everyone to a Github issue thread with five million comments where the lead developer is trying to defend his decision to depricate the output of a standard UNIX command. Which seems to be to be dumb, and a lot of other people too. But you can't fix stubburn.

So I wondered, how the hell do I get the current standard ISO time that Moment.js demands with oh so unsolid reasons?

The answer took awhile to find, but here it is. Let's call this file isotime.js and write a brief JavaScript program.

var date = new Date().toISOString()


This gets the current time in the regular format and converts it into an ISO string:

% node isotime.js

Then you can put it in your Metalwork YAML frontmatter:

title: How to get ISO time in JavaScript
date: 2015-08-10T21:19:18.549Z
template: post.jade
collection: blog 

And you won't get the stubborn 'your date isn't ISO enough' error from Moment.js that leads to the Moment.js issue thread that will never end.

Another idea I have is to sub a different module for Moment.js that will parse UNIX standard dates. But I haven't got around to doing that yet. One module I have my eye on is fecha.

Take The Node.js Test →

← What will it take to bring minimalism back?