You can use terraform formatdate() function to format date and time in terraform. formatdate() converts a timestamp into a different time format.
Syntax: formatdate(specification, timestamp)
The format specification is a string that includes formatting sequences from the following table.
specification | outcome |
---|---|
YYYY | Four (or more) digit year, like “2006”. |
YY | The year modulo 100, zero padded to at least two digits, like “06”. |
MMMM | English month name unabbreviated, like “January”. |
MMM | English month name abbreviated to three letters, like “Jan”. |
MM | Month number zero-padded to two digits, like “01” for January. |
M | Month number with no padding, like “1” for January. |
DD | Day of month number zero-padded to two digits, like “02”. |
D | Day of month number with no padding, like “2”. |
EEEE | English day of week name unabbreviated, like “Monday”. |
EEE | English day of week name abbreviated to three letters, like “Mon”. |
hh | 24-hour number zero-padded to two digits, like “02”. |
h | 24-hour number unpadded, like “2”. |
HH | 12-hour number zero-padded to two digits, like “02”. |
H | 12-hour number unpadded, like “2”. |
AA | Hour AM/PM marker in uppercase, like “AM”. |
aa | Hour AM/PM marker in lowercase, like “am”. |
mm | Minute within hour zero-padded to two digits, like “05”. |
m | Minute within hour unpadded, like “5”. |
ss | Second within minute zero-padded to two digits, like “09”. |
s | Second within minute, like “9”. |
ZZZZZ | Timezone offset with colon separating hours and minutes, like “-08:00”. |
ZZZZ | Timezone offset with just sign and digit, like “-0800”. |
ZZZ | Like ZZZZ but with a special case “UTC” for UTC. |
Z | Like ZZZZZ but with a special case “Z” for UTC. |
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## Open terraform console terraform console formatdate("DD MMM YYYY hh:mm ZZZ", "2022-05-13T13:10:10Z") ## returns "13 May 2022 13:10 UTC" formatdate("EEEE, DD-MMM-YY hh:mm:ss ZZZ", "2022-05-13T13:10:10Z") ## returns "Friday, 13-May-22 13:10:10 UTC" formatdate("EEE, DD MMM YYYY hh:mm:ss ZZZ", "2022-05-13T13:10:10Z") ## returns "Fri, 13 May 2022 13:10:10 UTC" formatdate("MMM DD, YYYY", "2022-05-13T13:10:10Z") ## returns "May 13, 2022" formatdate("HH:mmaa", "2022-05-13T13:10:10Z") ## returns "01:10pm" |