1

printf $(date +%F) prints 2012-07-14, according to YYYY-MM-DD format date in shell script. But, how do I print 2012-07-14T16:39:57-08:00, according to HTML5 datetime?

Community
  • 1
  • 1
ma11hew28
  • 121,420
  • 116
  • 450
  • 651

2 Answers2

2

HTML5 requires RFC3339 compiliance, therefore if you're using the gnu version of date then the --rfc-3339 extension will print what you want

antonio@localhost:~$ date --rfc-3339=seconds
2012-07-14 12:57:14+08:00

if you're on mac or don't have the gnu version then:

[13:00] antonio@chimera:~>date +%FT%T%z       
2012-07-14T13:01:35+0800

is close, but no cigar, because of a missing ':' in the timezone part, so if you don't want to pipe it through sed, cut, etcetera you can trick it by asking date to output UTC time and just adding the timezone portion manually (obviously UTC time offset is 00:00 so this will always work)

[13:01] antonio@chimera:~>date -u '+%FT%T+00:00'
2012-07-14T05:01:59+00:00
AntonioD
  • 547
  • 2
  • 7
1
$ date +%FT%T%z
2012-07-14T00:42:52-0400

I'm not sure how to get the colon in the time zone offset without using string manipulation.

$ datetime=`date +%FT%T%z`
$ printf ${datetime:0:${#datetime}-2}:${datetime:${#datetime}-2}
2012-07-14T00:42:52-04:00
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • 1
    But HTML5 datetime would have `04:00` at the end, no? ISO8601 has that last colon optional. – Ray Toal Jul 14 '12 at 04:47
  • I think you're right. Only way I know how to add the colon is by string manipulation. Do you know of a better way? – ma11hew28 Jul 14 '12 at 04:56