Essential Go Time and date  Suggest an edit

Format time like strftime

If you prefer strftime style of formatting time values (as used in Python or Ruby) or porting code that uses that style, you can use one of several libraries.

Here’s an example using package:

t := time.Date(2017, 9, 4, 3, 38, 45, 0, time.UTC)
fmt.Println(strftime.Format("%Y-%m-%d %H:%M:%S", t))
2017-09-04 03:38:45
Side note on package naming: since the package name is strftime, the import path / repository path should be It’s bad style to add go or go- to repository path.

Unfortunately, when it comes to parsing things are not as good. There are two packages:

but they both are cgo-wrappers around C libraries, which makes them more finicky to build, especially on Windows.

List of strftime directives

%aWeekday as locale’s abbreviated name.Mon
%AWeekday as locale’s full name.Monday
%bThe abbreviated month nameJan
%BThe full month nameJanuary
%dDay of the month01..31
%eDay of the month without a leading zero1..31
%jDay of the year001..366
%mMonth of the year01..12
%UWeek number of the current year, starting with the first Sunday as the first day of the first week00..53
%WWeek number of the current year, starting with the first Monday as the first day of the first week00..53
%wDay of the week (Sunday is 0)0..6
%xPreferred representation for the date alone, no time 
%yYear without a century00..99
%YYear with century 
%HHour of the day, 24-hour clock00..23
%IHour of the day, 12-hour clock01..12
%lHour of the day, 12-hour clock without a leading zero1..12
%MMinute of the hour00..59
%PMeridian indicator ("am" or "pm")am
%pMeridian indicator ("AM" or "PM")PM
%SSecond of the minute00..60
%XPreferred representation for the time alone, no date 
%ZTime zone name 
%cThe preferred local date and time representation 
%%Literal "%" character 

You can also use service to help you build strftime formatting strings.

  ↑ ↓ to navigate     ↵ to select     Esc to close