Skip to content

Instantly share code, notes, and snippets.

@smullins7
Last active December 30, 2015 22:59
Show Gist options
  • Select an option

  • Save smullins7/7897520 to your computer and use it in GitHub Desktop.

Select an option

Save smullins7/7897520 to your computer and use it in GitHub Desktop.
Bash functions to convert to and from milliseconds since epoch UTC. Requires moment (http://momentjs.com/docs/) to be installed.
function millis {
nvm use v0.10.12 > /dev/null && node -e "var m = require('moment'); console.log(+m.utc('${1}', ['MM/DD/YY', 'YYYYMMDDTHH:mm:ss']))"
}
function fmillis {
nvm use v0.10.12 > /dev/null && node -e "var m = require('moment'); console.log(m(${1}).utc().format())"
}
@tamale

tamale commented Dec 10, 2013

Copy link
Copy Markdown

Same functions in a source file. This won't work on OSX though, only linux. BSD bash has the somewhat-similar-but-different -f and -r flags though.

#!/bin/bash

function millis(){
  date --date="${1}" +%s -u
}

function fmillis(){
  date -d @${1} +"%Y%m%dT%H:%M:%S" -u
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment