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

you can do this in GNU bash directly, too

:~$ date --date="19-FEB-12" +%s
1329631200

:~$ date --date="2013-11-12" +%s
1384236000

:~$ date -d @1361234760 +"%Y-%m-%d"
2013-02-18

:~$ date -d @1361234760
Mon Feb 18 18:46:00 CST 2013

Add a -u for UTC:
:~$ date -d @1361234760 +"%Y-%m-%d %H:%M" -u
2013-02-19 00:46

@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