Ankur Gupta
Ankur Gupta

Reputation: 153

how to print time format in yyyy-MM-ddTHH:mm:ssZ using date command in linux

I need time in this format 'yyyy-MM-ddTHH:mm:ssZ'(e.g 1985-04-12T23:20:50.52Z) using date command in Linux. Can you help me on this?

Upvotes: 0

Views: 4603

Answers (1)

David C. Rankin
David C. Rankin

Reputation: 84559

The exact reproduction of Universal Coordinated Time (UTC) (a/k/a Zulu time) with Hundredths of a second would require fashioning a custom date string and calling date -u.

Note: to address the correct assertion in the comment by Toby Speight that there is a potential for a 1-second difference between the date representation held by d and that by n in the original answer due to using separate date calls, you can alleviate the concern by using a single call to date (and thank you again Toby for reminding me we can use printf field-width modifiers to truncate the nanosecond field), e.g.

date -u +'%FT%T.%2NZ'

Example Result

$ date -u +'%FT%T.%2NZ'
2018-04-14T17:45:28.20Z

This will avoid the 1-second potential difference due to two date calls.

Upvotes: 7

Related Questions