# strftime

Available inall subroutines.

Formats a time to a string. This uses standard POSIX strftime formats.

**HINT:** Regular strings ("short strings") in VCL use `%xx`

escapes (percent encoding) for special characters, which would conflict with the `%`

used in the `strftime`

format. For the `strftime`

examples, we use VCL "long strings" `{"..."}`

, which do not use the `%xx`

escapes. Alternatively, you could use `%25`

for each `%`

.

Valid format specifiers include:

Format | Substitution |
---|---|

`%a` | The abbreviated weekday name according to the current locale. |

`%A` | The full weekday name according to the current locale. |

`%b` | The abbreviated month name according to the current locale. |

`%B` | The full month name according to the current locale. |

`%c` | The preferred date and time representation for the current locale. |

`%C` | The century number (year/100) as a 2-digit integer. |

`%d` | The day of the month as a decimal number (range 01 to 31). |

`%D` | Equivalent to `%m` /`%d` /`%y` . |

`%e` | Like `%d` , the day of the month as a decimal number, but a leading zero is replaced by a space. |

`%F` | Equivalent to `%Y` -`%m` -`%d` (the ISO 8601 date format). |

`%G` | The ISO 8601 week-based year with century as a decimal number. The 4-digit year corresponding to the ISO week number (see `%V` ). This has the same format and value as `%Y` , except that if the ISO week number belongs to the previous or next year, that year is used instead. |

`%g` | Like `%G` , but without century, that is; with a 2-digit year (00 to 99). |

`%h` | Equivalent to `%b` . |

`%H` | The hour as a decimal number using a 24-hour clock (range 00 to 23). |

`%I` | The hour as a decimal number using a 12-hour clock (range 01 to 12). |

`%j` | The day of the year as a decimal number (range 001 to 366). |

`%k` | The hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank. (See also `%H` .) |

`%l` | The hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank. (See also `%I` .) |

`%m` | The month as a decimal number (range 01 to 12). |

`%M` | The minute as a decimal number (range 00 to 59). |

`%n` | A newline character. |

`%p` | Either `AM` or `PM` according to the given time value, or the corresponding strings for the current locale. Noon is treated as `PM` and midnight as `AM` . |

`%P` | Like `%p` but in lowercase: `am` or `pm` or a corresponding string for the current locale. |

`%r` | The time in a.m. or p.m. notation. In the POSIX locale this is equivalent to `%I` :`%M` :`%S` `%p` . |

`%R` | The time in 24-hour notation (`%H` :`%M` ). For a version including the seconds, see `%T` . |

`%s` | The number of seconds since the epoch; that is, since 1970-01-01 00:00:00 UTC. |

`%S` | The second as a decimal number (range 00 to 60). (A range up to 60 allows for occasional leap seconds.) |

`%t` | A tab character. |

`%T` | The time in 24-hour notation (`%H` :`%M` :`%S` ). |

`%u` | The day of the week as a decimal, range 1 to 7, Monday being 1. See also `%w` . |

`%U` | The week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01. See also `%V` and `%W` . |

`%V` | The ISO 8601 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least four days in the new year. See also `%U` and `%W` . |

`%w` | The day of the week as a decimal, range 0 to 6, Sunday being 0. See also `%u` . |

`%W` | The week number of the current year as a decimal number, range 00 to 53, starting with the first Monday as the first day of week 01. |

`%x` | The preferred date representation for the current locale, without the time. |

`%X` | The preferred time representation for the current locale, without the date. |

`%y` | The year as a decimal number without a century (range 00 to 99). |

`%Y` | The year as a decimal number, including the century. |

`%z` | The time-zone as an hour offset from GMT. Required to emit RFC 822-conformant dates (using `%a` , `%d` `%b` `%Y` `%H` :`%M` :`%S` `%z` ). |

`%Z` | The timezone or name or abbreviation. |

`%%` | A literal `%` character. |

Valid modifiers include:

Format | Substitution |
---|---|

`%Ec` | Equivalent to %c. |

`%EC` | Equivalent to %C. |

`%En` | A newline character. |

`%Ep` | Equivalent to %p. |

`%EP` | Equivalent to %P. |

`%Er` | Equivalent to %r. |

`%ER` | Equivalent to %R. |

`%Es` | Equivalent to %s. |

`%Et` | A tab character. |

`%ET` | Equivalent to %T. |

`%Eu` | Equivalent to %u. |

`%Ex` | Equivalent to %x. |

`%EX` | Equivalent to %X. |

`%Ey` | Equivalent to %y. |

`%EY` | Equivalent to %Y. |

`%Ez` | Equivalent to %z. |

`%EZ` | Equivalent to %Z. |

`%OC` | Equivalent to %C. |

`%Od` | Equivalent to %d. |

`%Oe` | Equivalent to %e. |

`%Og` | Equivalent to %g. |

`%OG` | Equivalent to %G. |

`%OH` | Equivalent to %H. |

`%OI` | Equivalent to %I. |

`%Oj` | Equivalent to %j. |

`%Ok` | Equivalent to %k. |

`%Ol` | Equivalent to %l. |

`%Om` | Equivalent to %m. |

`%OM` | Equivalent to %M. |

`%On` | A newline character. |

`%Op` | Equivalent to %p. |

`%OP` | Equivalent to %P. |

`%Or` | Equivalent to %r. |

`%OR` | Equivalent to %R. |

`%Os` | Equivalent to %s. |

`%OS` | Equivalent to %S. |

`%Ot` | A tab character. |

`%OT` | Equivalent to %T. |

`%Ou` | Equivalent to %u. |

`%OU` | Equivalent to %U. |

`%OV` | Equivalent to %V. |

`%Ow` | Equivalent to %w. |

`%OW` | Equivalent to %W. |

`%Oy` | Equivalent to %y. |

`%Oz` | Equivalent to %z. |

`%OZ` | Equivalent to %Z. |

Valid extensions include:

Format | Substitution |
---|---|

`%-a` | Equivalent to %a. |

`%-A` | Equivalent to %A. |

`%-b` | Equivalent to %b. |

`%-B` | Equivalent to %B. |

`%-c` | Equivalent to %c. |

`%-C` | Equivalent to %C. |

`%-d` | Equivalent to %d. |

`%-D` | Equivalent to %D. |

`%-e` | Equivalent to %e. |

`%-F` | Equivalent to %F. |

`%-g` | Equivalent to %g. |

`%-G` | Equivalent to %G. |

`%-h` | Equivalent to %h. |

`%-H` | Equivalent to %H. |

`%-I` | Like %I, but without any padding. |

`%-j` | Equivalent to %j. |

`%-k` | Equivalent to %k. |

`%-l` | Like %l, but without any padding. |

`%-m` | like %m, but without any padding. |

`%-M` | Equivalent to %M. |

`%-n` | A newline character. |

`%-p` | Equivalent to %p. |

`%-P` | Equivalent to %P. |

`%-r` | Equivalent to %r. |

`%-R` | Equivalent to %R. |

`%-s` | Equivalent to %s. |

`%-S` | Equivalent to %S. |

`%-t` | A tab character. |

`%-T` | Equivalent to %T. |

`%-u` | Equivalent to %u. |

`%-U` | Equivalent to %U. |

`%-V` | Equivalent to %V. |

`%-w` | Equivalent to %w. |

`%-W` | Equivalent to %W. |

`%-x` | Equivalent to %x. |

`%-X` | Equivalent to %X. |

`%-y` | Equivalent to %y. |

`%-Y` | Equivalent to %Y. |

`%-z` | Like %z, but without any padding. |

`%-Z` | Equivalent to %Z. |

`%_a` | Equivalent to %a. |

`%_A` | Equivalent to %A. |

`%_b` | Equivalent to %b. |

`%_B` | Equivalent to %B. |

`%_c` | Equivalent to %c. |

`%_C` | Equivalent to %C. |

`%_d` | Equivalent to %d. |

`%_D` | Equivalent to %D. |

`%_e` | Equivalent to %e. |

`%_F` | Equivalent to %F. |

`%_g` | Equivalent to %g. |

`%_G` | Equivalent to %G. |

`%_h` | Equivalent to %h. |

`%_H` | Equivalent to %H. |

`%_I` | Like %I, but spaces are used for padding. |

`%_j` | Equivalent to %j. |

`%_k` | Equivalent to %k. |

`%_l` | Like %l, but spaces are used for padding. |

`%_m` | Like %m, but spaces are used for padding. |

`%_M` | Equivalent to %M. |

`%_n` | A newline character. |

`%_p` | Equivalent to %p. |

`%_P` | Equivalent to %P. |

`%_r` | Equivalent to %r. |

`%_R` | Equivalent to %R. |

`%_s` | Equivalent to %s. |

`%_S` | Equivalent to %S. |

`%_t` | A tab character. |

`%_T` | Equivalent to %T. |

`%_u` | Equivalent to %u. |

`%_U` | Equivalent to %U. |

`%_V` | Equivalent to %V. |

`%_w` | Equivalent to %w. |

`%_W` | Equivalent to %W. |

`%_x` | Equivalent to %x. |

`%_X` | Equivalent to %X. |

`%_y` | Equivalent to %y. |

`%_Y` | Equivalent to %Y. |

`%_z` | Like %z, but spaces are used for padding. |

`%_Z` | Equivalent to %Z. |

`%0a` | Equivalent to %a. |

`%0A` | Equivalent to %A. |

`%0b` | Equivalent to %b. |

`%0B` | Equivalent to %B. |

`%0c` | Equivalent to %c. |

`%0C` | Equivalent to %C. |

`%0d` | Equivalent to %d. |

`%0D` | Equivalent to %D. |

`%0e` | Equivalent to %e. |

`%0F` | Equivalent to %F. |

`%0g` | Equivalent to %g. |

`%0G` | Equivalent to %G. |

`%0h` | Equivalent to %h. |

`%0H` | Equivalent to %H. |

`%0I` | Like %I, but zeros are used for padding. |

`%0j` | Equivalent to %j. |

`%0k` | Equivalent to %k. |

`%0l` | Like %l, but zeros are used for padding. |

`%0m` | Like %m, but zeros are used for padding. |

`%0M` | Equivalent to %M. |

`%0n` | A newline character. |

`%0p` | Equivalent to %p. |

`%0P` | Equivalent to %P. |

`%0r` | Equivalent to %r. |

`%0R` | Equivalent to %R. |

`%0s` | Equivalent to %s. |

`%0S` | Equivalent to %S. |

`%0t` | A tab character. |

`%0T` | Equivalent to %T. |

`%0u` | Equivalent to %u. |

`%0U` | Equivalent to %U. |

`%0V` | Equivalent to %V. |

`%0w` | Equivalent to %w. |

`%0W` | Equivalent to %W. |

`%0x` | Equivalent to %x. |

`%0X` | Equivalent to %X. |

`%0y` | Equivalent to %y. |

`%0Y` | Equivalent to %Y. |

`%0z` | Like %z, but zeros are used for padding. |

`%0Z` | Equivalent to %Z. |

This function is not prefixed with the `std.`

namespace.

## Examples

```
# Concise formatset resp.http.now = strftime({"%Y-%m-%d %H:%M"}, now);# resp.http.now is now e.g. 2006-01-02 22:04
# RFC 5322 formatset resp.http.start = strftime({"%a, %d %b %Y %T %z"}, time.start);# resp.http.start is now e.g. Mon, 02 Jan 2006 22:04:05 +0000
# ISO 8601 formatset resp.http.end = strftime({"%Y-%m-%dT%H:%M:%SZ"}, time.end);# resp.http.end is now e.g. 2006-01-02T22:04:05Z
```

## Try it out

`strftime`

is used in the following code examples. Examples apply VCL to real-world use cases and can be deployed as they are, or adapted for your own service. See the full list of code examples for more inspiration.

Click RUN on a sample below to provision a Fastly service, execute the code on Fastly, and see how the function behaves.

## AWS S3 bucket origin (private)

Use AWS authenticated requests (signature version 4) to protect communication between your Fastly service and AWS.

## Comprehensive logging

Fastly offers a myriad of different variables that you can log. See and test a large collection here.

## Format time expressions

Format dates and times in a variety of ways.