github.com/rstandt/terraform@v0.12.32-0.20230710220336-b1063613405c/website/docs/configuration/functions/formatdate.html.md (about)

     1  ---
     2  layout: "functions"
     3  page_title: "formatdate - Functions - Configuration Language"
     4  sidebar_current: "docs-funcs-datetime-formatdate"
     5  description: |-
     6    The formatdate function converts a timestamp into a different time format.
     7  ---
     8  
     9  # `formatdate` Function
    10  
    11  -> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
    12  earlier, see
    13  [0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html).
    14  
    15  `formatdate` converts a timestamp into a different time format.
    16  
    17  ```hcl
    18  formatdate(spec, timestamp)
    19  ```
    20  
    21  In the Terraform language, timestamps are conventionally represented as
    22  strings using [RFC 3339](https://tools.ietf.org/html/rfc3339)
    23  "Date and Time format" syntax. `formatdate` requires the `timestamp` argument
    24  to be a string conforming to this syntax.
    25  
    26  ## Examples
    27  
    28  ```
    29  > formatdate("DD MMM YYYY hh:mm ZZZ", "2018-01-02T23:12:01Z")
    30  02 Jan 2018 23:12 UTC
    31  > formatdate("EEEE, DD-MMM-YY hh:mm:ss ZZZ", "2018-01-02T23:12:01Z")
    32  Tuesday, 02-Jan-18 23:12:01 UTC
    33  > formatdate("EEE, DD MMM YYYY hh:mm:ss ZZZ", "2018-01-02T23:12:01-08:00")
    34  Tue, 02 Jan 2018 23:12:01 -0800
    35  > formatdate("MMM DD, YYYY", "2018-01-02T23:12:01Z")
    36  Jan 02, 2018
    37  > formatdate("HH:mmaa", "2018-01-02T23:12:01Z")
    38  11:12pm
    39  ```
    40  
    41  ## Specification Syntax
    42  
    43  The format specification is a string that includes formatting sequences from
    44  the following table. This function is intended for producing common
    45  _machine-oriented_ timestamp formats such as those defined in RFC822, RFC850,
    46  and RFC1123. It is not suitable for truly human-oriented date formatting
    47  because it is not locale-aware. In particular, it can produce month and day
    48  names only in English.
    49  
    50  The specification may contain the following sequences:
    51  
    52  | Sequence  | Result                                                                   |
    53  | --------- | ------------------------------------------------------------------------ |
    54  | `YYYY`    | Four (or more) digit year, like "2006".                                  |
    55  | `YY`      | The year modulo 100, zero padded to at least two digits, like "06".      |
    56  | `MMMM`    | English month name unabbreviated, like "January".                        |
    57  | `MMM`     | English month name abbreviated to three letters, like "Jan".             |
    58  | `MM`      | Month number zero-padded to two digits, like "01" for January.           |
    59  | `M`       | Month number with no padding, like "1" for January.                      |
    60  | `DD`      | Day of month number zero-padded to two digits, like "02".                |
    61  | `D`       | Day of month number with no padding, like "2".                           |
    62  | `EEEE`    | English day of week name unabbreviated, like "Monday".                   |
    63  | `EEE`     | English day of week name abbreviated to three letters, like "Mon".       |
    64  | `hh`      | 24-hour number zero-padded to two digits, like "02".                     |
    65  | `h`       | 24-hour number unpadded, like "2".                                       |
    66  | `HH`      | 12-hour number zero-padded to two digits, like "02".                     |
    67  | `H`       | 12-hour number unpadded, like "2".                                       |
    68  | `AA`      | Hour AM/PM marker in uppercase, like "AM".                               |
    69  | `aa`      | Hour AM/PM marker in lowercase, like "am".                               |
    70  | `mm`      | Minute within hour zero-padded to two digits, like "05".                 |
    71  | `m`       | Minute within hour unpadded, like "5".                                   |
    72  | `ss`      | Second within minute zero-padded to two digits, like "09".               |
    73  | `s`       | Second within minute, like "9".                                          |
    74  | `ZZZZZ`   | Timezone offset with colon separating hours and minutes, like "-08:00".  |
    75  | `ZZZZ`    | Timezone offset with just sign and digit, like "-0800".                  |
    76  | `ZZZ`     | Like `ZZZZ` but with a special case "UTC" for UTC.                       |
    77  | `Z`       | Like `ZZZZZ` but with a special case "Z" for UTC.                        |
    78  
    79  Any non-letter characters, such as punctuation, are reproduced verbatim in the
    80  output. To include literal letters in the format string, enclose them in single
    81  quotes `'`. To include a literal quote, escape it by doubling the quotes.
    82  
    83  ```
    84  > formatdate("h'h'mm", "2018-01-02T23:12:01-08:00")
    85  23h12
    86  > formatdate("H 'o''clock'", "2018-01-02T23:12:01-08:00")
    87  11 o'clock
    88  ```
    89  
    90  This format specification syntax is intended to make it easy for a reader
    91  to guess which format will result even if they are not experts on the syntax.
    92  Therefore there are no predefined shorthands for common formats, but format
    93  strings for various RFC-specified formats are given below to be copied into your
    94  configuration as needed:
    95  
    96  - [RFC 822](https://tools.ietf.org/html/rfc822#section-5) and
    97    [RFC RFC 2822](https://tools.ietf.org/html/rfc2822#section-3.3):
    98    `"DD MMM YYYY hh:mm ZZZ"`
    99  - [RFC 850](https://tools.ietf.org/html/rfc850#section-2.1.4):
   100    `"EEEE, DD-MMM-YY hh:mm:ss ZZZ"`
   101  - [RFC 1123](https://tools.ietf.org/html/rfc1123#section-5.2.14):
   102    `"EEE, DD MMM YYYY hh:mm:ss ZZZ"`
   103  - [RFC 3339](https://tools.ietf.org/html/rfc3339):
   104    `"YYYY-MM-DD'T'hh:mm:ssZ"` (but this is also the input format, so such a
   105    conversion is redundant.)
   106  
   107  ## Related Functions
   108  
   109  * [`format`](./format.html) is a more general formatting function for arbitrary
   110    data.
   111  * [`timestamp`](./timestamp.html) returns the current date and time in a format
   112    suitable for input to `formatdate`.