Format time like strftime

suggest change

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 github.com/jehiah/go-strftime package:

t := time.Date(2017, 9, 4, 3, 38, 45, 0, time.UTC)
fmt.Println(strftime.Format("%Y-%m-%d %H:%M:%S", t))
Side note on package naming: since the package name is strftime, the import path / repository path should be github.com/jehiah/strftime. 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

Code Meaning Example
%a Weekday as locale’s abbreviated name. Mon
%A Weekday as locale’s full name. Monday
%b The abbreviated month name Jan
%B The full month name January
%d Day of the month 01..31
%e Day of the month without a leading zero 1..31
%j Day of the year 001..366
%m Month of the year 01..12
%U Week number of the current year, starting with the first Sunday as the first day of the first week 00..53
%W Week number of the current year, starting with the first Monday as the first day of the first week 00..53
%w Day of the week (Sunday is 0) 0..6
%x Preferred representation for the date alone, no time  
%y Year without a century 00..99
%Y Year with century  
     
%H Hour of the day, 24-hour clock 00..23
%I Hour of the day, 12-hour clock 01..12
%l Hour of the day, 12-hour clock without a leading zero 1..12
%M Minute of the hour 00..59
%P Meridian indicator ("am" or "pm") am
%p Meridian indicator ("AM" or "PM") PM
%S Second of the minute 00..60
%X Preferred representation for the time alone, no date  
%Z Time zone name  
     
%c The preferred local date and time representation  
%% Literal "%" character  

You can also use http://www.strfti.me/ service to help you build strftime formatting strings.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents