Skip to content

Instantly share code, notes, and snippets.

@tenomoto
Last active November 30, 2020 02:08
Show Gist options
  • Save tenomoto/e994e981519d2f9d875fb0237d3fb090 to your computer and use it in GitHub Desktop.
Save tenomoto/e994e981519d2f9d875fb0237d3fb090 to your computer and use it in GitHub Desktop.
Date conversions
load "$NCARG_ROOT/lib/ncarg/nclscripts/contrib/cd_inv_string.ncl"
valid = cd_inv_string("2019101212", "%Y%N%D%H")
init = cd_inv_calendar(2019, 10, 9, 12, 0, 0.0, valid@units, 0)
fth = valid - init
yyyymmddhh = cd_calendar(time, -3)
#!/bin/sh
init=$(date -jf %Y%m%d%H%M%S 20191009120000 +%s)
valid=$(date -jf %Y%m%d%H%M%S 20191012120000 +%s)
ft=$(expr ${valid} - ${init})
fth=$(expr ${ft} \/ 3600)
date -jf %s ${init} +%Y%m%d%H
#!/bin/sh
init=$(date -d "2019-10-09 12:00:00+00" +%s)
valid=$(date -d "2019-10-12 12:00:00+00" +%s)
ft=$(expr ${valid} - ${init})
fth=$(expr ${ft} \/ 3600)
TZ=0 date -d @${init} +%Y%m%d%H
from datetime import datetime
init = datetime(2019, 10, 9, 12)
valid = datetime.strptime("2019101212", "%Y%m%d%H")
ft = valid - init # timedelt object
fth = ft.total_seconds() / 3600
yyyymmddhh = init.strftime("%Y%m%d%H")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment