Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The date command doesn't offer such thing, which is kind of sad since RFC-3339 is the modern, widespread, sane format used everywhere (except in email which is neither modern nor sane).

My timezone offset is currently -08:00 so the simplest form of this command should print the current time as 2013-09-05T14:58:33.102-08:00.

share|improve this question
    
Which standard (s) are you interested in? – Stéphane Chazelas Mar 19 '14 at 21:54
    
I personally am fine with GNU but in the grand scheme of things it really should be broader than that. It should also come with a reasonable default (short option, millisecond precision) and have a way to specify the subsecond precision (number of digits other than 0 or 9). – Martin Jambon Mar 19 '14 at 22:37
1  
It's not a sane format: the total absence of whitespace makes it unnecessarily hard for humans to read. You should use the slight variation 2013-09-05 14:58:33.102 -0800 except when there is some reason why spaces must not be used at all. – zwol Mar 20 '14 at 1:26
    
I was comparing it to the mail date format (RFC 822, 2822) which is hard to read for both humans and machines. – Martin Jambon Mar 20 '14 at 1:31

It seems like you can do several formats using the switch to the GNU implementation of date (version 5.90 or above), --rfc3339=.

Examples

$ date --rfc-3339=date
2014-03-19

$ date --rfc-3339=seconds
2014-03-19 18:00:05-04:00

$ date --rfc-3339=ns
2014-03-19 18:00:08.179780629-04:00

If you want the T to be added, as a hack:

$ date --rfc-3339=seconds | sed 's/ /T/'
2014-03-19T18:35:03-04:00

If you want it in milliseconds:

$ date --rfc-3339=ns | sed 's/ /T/; s/\(\....\).*-/\1-/g'
2014-03-19T18:42:52.362-04:00

References

share|improve this answer
    
These days, no hack is needed to get the 'T'. The --iso-8601' option accepts the same arguments as --rfc-3339`, and includes the 'T' in its output. – Ti Strga Mar 23 at 18:41

With GNU date (5.90 or above):

$ TZ=America/Anchorage date '+%FT%T.%N%:z'
2014-03-19T14:29:31.041119357-08:00

Replace %N with %3N for milliseconds, %6N for micro-seconds...

AFAIK, none of the POSIX, Unix or LSB specifications specify any command that can display times with sub-second granularity, but the fractional part is optional in RFC 3339.

POSIX/Unix/LSB strftime supports %z to display the TZ offset as -0800, so the most portable you're probably going to get is:

 $ TZ=America/Anchorage perl -MPOSIX -le '$t = strftime "%Y-%m-%dT%T%z",
   localtime; $t =~ s/..$/:$&/; print $t'
 2014-03-19T14:30:23-08:00
share|improve this answer

GNU date has the ISO-8601 format built in - isn't that quite close or even idential to RFC-3339?

1065 % date --iso-8601=seconds
2014-03-19T16:51:16-0600
share|improve this answer
    
ISO-8601 allows different formattings of dates and times, but I think the OP is asking specifically for W3C Date format "Complete date plus hours, minutes, seconds and a decimal fraction of a second YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)" - W3C Date and Time Formats. – hakre Oct 13 '14 at 10:58

How about good old :

$ date +%Y-%m-%dT%T%z
2015-10-29T14:47:06+0200
share|improve this answer
    
Where is the time zone information? – Raphael Ahrens Oct 29 '15 at 12:45
1  
@RaphaelAhrens fixed it! Thanks a lot – Eran Chetzroni Oct 29 '15 at 13:51
    
This works both on mac and linux 'date' – Eran Chetzroni Oct 29 '15 at 13:51
1  
How about milliseconds? – Martin Jambon Oct 29 '15 at 14:19
2  
quick note here, without opening up the RFC, the timezone segment, 0200 is missing a colon, which may break some systems which require it. – Mike Mackintosh Nov 30 '15 at 17:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.