I have a requirement to append timezone value in the below format at the end of milliseconds, an example follows:


I need the Unix command to get the required format.

closed as unclear what you're asking by Rui F Ribeiro, Kusalananda, Romeo Ninov, Vlastimil, αғsнιη Jul 1 '18 at 13:02

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    Where does the data come from, a file? What is the format of that file? Are you just wanting to append the string -0700 to the end of the timestamp? Where is the output going? – Kusalananda Jul 1 '18 at 8:43
  • What does it look like now? Where is if from? (it looks fine to me, you will have to explain more). – ctrl-alt-delor Jul 1 '18 at 11:07

It is posible to get the ISO8601 time format with GNU date:

$ date -Iseconds

However, to get the time with milliseconds you neef to specify the detailed string. Try:

$ date +'%Y-%m-%dT%H:%M:%S.%3N%z'
  • Since date is based on strftime() and that uses a struct tm as input, the granularity is seconds only. %N is a nonstandard extension that is implemented inside your date clone. – schily Jul 6 '18 at 14:09

With GNU or ast-open date:

$ date +%FT%T.%3N%z

(GNU date (not GNU strftime) also supports %:z for +01:00, %::z for +01:00:00¹ and %:::z for +01/+01:30/+01:30:01 (as many extra :xx as necessary)).

In ksh93:

$ printf "%(%FT%T.%3N%z)T\n"

In zsh:

$ zmodload zsh/datetime
$ echo $EPOCHREALTIME | IFS=. read s ns
$ strftime %FT%T.$ns[1,3]%z $s

Portably (the hard part is the milliseconds here, not the timezone offset which you can get with %z in anything that wraps the standard² strftime() function like date), you could try perl:

perl -MTime::HiRes=gettimeofday -MPOSIX -le '
  ($s, $us) = gettimeofday;
  $ms = sprintf("%03d", $us/1000);
  print strftime "%FT%T.$ms%z", localtime $s'

¹ Yes, it's possible to have TZ offsets with non-zero seconds, like in TZ='OFF00:00:01' date '+%Z %::z'. For real-life timezones, that only applies for dates in distant (pre-Unix) times. See for instance TZ=Europe/Amsterdam date -d '90 years ago' '+%F %::z'

² Note however that while %F and %z are specified in POSIX strftime() (at least since SUSv3), they are not in the POSIX specification of the date utility (I don't know of any current system where date doesn't support them though)


Add %z to your time format. Example:

$ date
Sun Jul  1 04:55:02 EDT 2018
$ date '+%z'

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