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
METHODS TIME METHODS

Format

Returns a textual representation of the time.Time value formatted according to the layout string.

Syntax

TIME.Format LAYOUT

Returns

string
{{ $t := "2023-01-27T23:44:58-08:00" }}
{{ $t = time.AsTime $t }}
{{ $format := "2 Jan 2006" }}

{{ $t.Format $format }} → 27 Jan 2023

To localize the return value, use the time.Format function instead.

Use the Format method with any time.Time value, including the four predefined front matter dates:

{{ $format := "2 Jan 2006" }}

{{ .Date.Format $format }}
{{ .PublishDate.Format $format }}
{{ .ExpiryDate.Format $format }}
{{ .Lastmod.Format $format }}

Use the time.Format function to format string representations of dates, and to format raw TOML dates that exclude time and time zone offset.

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.

Examples

Given this front matter:

     
---
date: 2023-01-27T23:44:58-08:00
title: About time
---
+++
date = 2023-01-27T23:44:58-08:00
title = 'About time'
+++
{
   "date": "2023-01-27T23:44:58-08:00",
   "title": "About time"
}

The examples below were rendered in the America/Los_Angeles time zone:

Format string Result
Monday, January 2, 2006 Friday, January 27, 2023
Mon Jan 2 2006 Fri Jan 27 2023
January 2006 January 2023
2006-01-02 2023-01-27
Monday Friday
02 Jan 06 15:04 MST 27 Jan 23 23:44 PST
Mon, 02 Jan 2006 15:04:05 MST Fri, 27 Jan 2023 23:44:58 PST
Mon, 02 Jan 2006 15:04:05 -0700 Fri, 27 Jan 2023 23:44:58 -0800

UTC and local time

Convert and format any time.Time value to either Coordinated Universal Time (UTC) or local time.

{{ $t := "2023-01-27T23:44:58-08:00" }}
{{ $t = time.AsTime $t }}
{{ $format := "2 Jan 2006 3:04:05 PM MST" }}

{{ $t.UTC.Format $format }} → 28 Jan 2023 7:44:58 AM UTC
{{ $t.Local.Format $format }} → 27 Jan 2023 11:44:58 PM PST

Ordinal representation

Use the humanize function to render the day of the month as an ordinal number:

{{ $t := "2023-01-27T23:44:58-08:00" }}
{{ $t = time.AsTime $t }}

{{ humanize $t.Day }} of {{ $t.Format "January 2006" }} → 27th of January 2023

See also

  • time.AsTime
  • UTC
  • Local

On this page

  • Layout string
  • Examples
  • UTC and local time
  • Ordinal representation

In this section

  • Add
  • AddDate
  • After
  • Before
  • Day
  • Equal
  • Format
  • Hour
  • IsDST
  • IsZero
  • Local
  • Minute
  • Month
  • Nanosecond
  • Second
  • Sub
  • Unix
  • UnixMicro
  • UnixMilli
  • UnixNano
  • UTC
  • Weekday
  • Year
  • YearDay
Last updated: February 3, 2024: Replace links to variable pages with links to method pages (126c323d)
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