HUGO

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
gohugoio Star
  • About Hugo
    • Overview
    • What is Hugo
    • Hugo features
    • Static site generators
    • Hugo's security model
    • Hugo and the GDPR
    • License
  • Installation
    • Overview
    • macOS
    • Linux
    • Windows
    • BSD
  • Getting started
    • Overview
    • Quick start
    • Basic usage
    • Directory structure
    • Configuration
    • Configure markup
    • Glossary of terms
    • External learning resources
  • Quick reference
    • Overview
    • Emojis
    • Functions
    • Methods
    • Page collections
  • Content management
    • Overview
    • Organization
    • Page bundles
    • Content formats
    • Front matter
    • Build options
    • Page resources
    • Image processing
    • Shortcodes
    • Related content
    • Sections
    • Content types
    • Archetypes
    • Taxonomies
    • Summaries
    • Links and cross references
    • URL management
    • Menus
    • Static files
    • Table of contents
    • Comments
    • Multilingual
    • Markdown attributes
    • Syntax highlighting
    • Diagrams
    • Mathematics
  • Templates
    • Overview
    • Templating
    • Template lookup order
    • Base templates and blocks
    • Single page templates
    • List templates
    • Homepage template
    • Section templates
    • Taxonomy templates
    • Pagination
    • Content view templates
    • Partial templates
    • Shortcode templates
    • Menu templates
    • Data templates
    • RSS templates
    • Sitemap templates
    • Internal templates
    • Custom output formats
    • 404 page
    • Robots.txt
  • Functions
    • Overview
    • cast
    • collections
    • compare
    • crypto
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • Methods
    • Overview
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • Render hooks
    • Overview
    • Introduction
    • Code blocks
    • Headings
    • Images
    • Links
  • Hugo Modules
    • Overview
    • Configure Hugo modules
    • Use Hugo Modules
    • Theme components
  • Hugo Pipes
    • Overview
    • Introduction
    • Transpile Sass to CSS
    • PostCSS
    • PostProcess
    • JavaScript building
    • Babel
    • Asset minification
    • Concatenating assets
    • Fingerprinting and SRI hashing
    • Resource from string
    • Resource from template
  • CLI
  • Troubleshooting
    • Overview
    • Audit
    • Logging
    • Inspection
    • Deprecation
    • Performance
    • FAQs
  • Developer tools
    • Overview
    • Editor plugins
    • Front-ends
    • Search
    • Migrations
    • Other projects
  • Hosting and deployment
    • Overview
    • Hugo Deploy
    • Deploy with Rclone
    • Deploy with Rsync
    • Host on 21YunBox
    • Host on AWS Amplify
    • Host on Azure Static Web Apps
    • Host on Cloudflare Pages
    • Host on Firebase
    • Host on GitHub Pages
    • Host on GitLab Pages
    • Host on KeyCDN
    • Host on Netlify
    • Host on Render
  • Contribute
    • Overview
    • Development
    • Documentation
    • Themes
  • Maintenance
FUNCTIONS TIME FUNCTIONS

time.Format

Returns the given date/time as a formatted and localized string.

Syntax

time.Format LAYOUT INPUT

Returns

string

Alias

dateFormat

Use the time.Format function with time.Time values:

{{ $t := time.AsTime "2023-02-27T23:44:58-08:00" }}
{{ time.Format "2 Jan 2006" $t }} → 27 Feb 2023

Or use time.Format with a parsable string representation of a date/time value:

{{ $t := "27 Feb 2023" }}
{{ time.Format "January 2, 2006" $t }} → February 27, 2023

Examples of parsable string representations:

String representation Time zone
2023-10-15T14:20:28-07:00 America/Los_Angeles
2023-10-15T13:18:50-0700 America/Los_Angeles
2023-10-15T13:18:50Z Etc/UTC
2023-10-15T13:18:50 Etc/UTC
2023-10-15 Etc/UTC
15 Oct 2023 Etc/UTC

The last four examples are not fully qualified. Without a time zone offset, the time zone is set to Etc/UTC (Coordinated Universal Time).

Layout string

Format a time.Time value based on Go’s reference time:

Mon Jan 2 15:04:05 MST 2006

Create a layout string using these components:

Description Valid components
Year "2006" "06"
Month "Jan" "January" "01" "1"
Day of the week "Mon" "Monday"
Day of the month "2" "_2" "02"
Day of the year "__2" "002"
Hour "15" "3" "03"
Minute "4" "04"
Second "5" "05"
AM/PM mark "PM"
Time zone offsets "-0700" "-07:00" "-07" "-070000" "-07:00:00"

Replace the sign in the layout string with a Z to print Z instead of an offset for the UTC zone.

Description Valid components
Time zone offsets "Z0700" "Z07:00" "Z07" "Z070000" "Z07:00:00"
{{ $t := "2023-01-27T23:44:58-08:00" }}
{{ $t = time.AsTime $t }}
{{ $t = $t.Format "Jan 02, 2006 3:04 PM Z07:00" }}

{{ $t }} → Jan 27, 2023 11:44 PM -08:00 

Strings such as PST and CET are not time zones. They are time zone abbreviations.

Strings such as -07:00 and +01:00 are not time zones. They are time zone offsets.

A time zone is a geographic area with the same local time. For example, the time zone abbreviated by PST and PDT (depending on Daylight Savings Time) is America/Los_Angeles.

Localization

Use the time.Format function to localize time.Time values for the current language and region.

Localization of dates, currencies, numbers, and percentages is performed by the gohugoio/locales package. The language tag of the current site must match one of the listed locales.

Use the layout string as described above, or one of the tokens below. For example:

{{ .Date | time.Format ":date_medium" }} → Jan 27, 2023

Localized to en-US:

Token Result
:date_full Friday, January 27, 2023
:date_long January 27, 2023
:date_medium Jan 27, 2023
:date_short 1/27/23
:time_full 11:44:58 pm Pacific Standard Time
:time_long 11:44:58 pm PST
:time_medium 11:44:58 pm
:time_short 11:44 pm

Localized to de-DE:

Token Result
:date_full Freitag, 27. Januar 2023
:date_long 27. Januar 2023
:date_medium 27.01.2023
:date_short 27.01.23
:time_full 23:44:58 Nordamerikanische Westküsten-Normalzeit
:time_long 23:44:58 PST
:time_medium 23:44:58
:time_short 23:44

See also

  • time.AsTime
  • time.Duration
  • time.Now
  • time.ParseDuration

On this page

  • Layout string
  • Localization

In this section

  • time.AsTime
  • time.Duration
  • time.Format
  • time.Now
  • time.ParseDuration
Last updated: November 4, 2023: Namespace functions and methods (723a827f)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo Sponsors

 

The Hugo logos are copyright © Steve Francia 2013–2024.

The Hugo Gopher is based on an original work by Renée French.

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
  • About Hugo
  • Installation
  • Getting started
  • Quick reference
  • Content management
  • Templates
  • Functions
  • Methods
  • Render hooks
  • Hugo Modules
  • Hugo Pipes
  • CLI
  • Troubleshooting
  • Developer tools
  • Hosting and deployment
  • Contribute
  • Maintenance