Time Formatting and Parsing

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab focuses on time formatting and parsing in Golang. Golang provides pattern-based layouts for time formatting and parsing.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/AdvancedTopicsGroup(["`Advanced Topics`"]) go/AdvancedTopicsGroup -.-> go/time_formatting_parsing("`Time Formatting Parsing`") subgraph Lab Skills go/time_formatting_parsing -.-> lab-15521{{"`Time Formatting and Parsing`"}} end

Time Formatting and Parsing

The problem is to format and parse time in Golang using the provided layouts.

  • Use the time package to format and parse time.
  • Use the time.RFC3339 layout to format and parse time.
  • Use the Mon Jan 2 15:04:05 MST 2006 reference time to show the pattern with which to format/parse a given time/string.
  • Use the Parse function to parse time.
  • Use the Format function to format time.
  • Use the fmt.Println function to print the formatted time.
  • Use the fmt.Printf function to print the formatted time with extracted components.
$ go run time-formatting-parsing.go
2014-04-15T18:00:15-07:00
2012-11-01 22:08:41 +0000 +0000
6:00PM
Tue Apr 15 18:00:15 2014
2014-04-15T18:00:15.161182-07:00
0000-01-01 20:41:00 +0000 UTC
2014-04-15T18:00:15-00:00
parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": ...

There is the full code below:

// Go supports time formatting and parsing via
// pattern-based layouts.

package main

import (
	"fmt"
	"time"
)

func main() {
	p := fmt.Println

	// Here's a basic example of formatting a time
	// according to RFC3339, using the corresponding layout
	// constant.
	t := time.Now()
	p(t.Format(time.RFC3339))

	// Time parsing uses the same layout values as `Format`.
	t1, e := time.Parse(
		time.RFC3339,
		"2012-11-01T22:08:41+00:00")
	p(t1)

	// `Format` and `Parse` use example-based layouts. Usually
	// you'll use a constant from `time` for these layouts, but
	// you can also supply custom layouts. Layouts must use the
	// reference time `Mon Jan 2 15:04:05 MST 2006` to show the
	// pattern with which to format/parse a given time/string.
	// The example time must be exactly as shown: the year 2006,
	// 15 for the hour, Monday for the day of the week, etc.
	p(t.Format("3:04PM"))
	p(t.Format("Mon Jan _2 15:04:05 2006"))
	p(t.Format("2006-01-02T15:04:05.999999-07:00"))
	form := "3 04 PM"
	t2, e := time.Parse(form, "8 41 PM")
	p(t2)

	// For purely numeric representations you can also
	// use standard string formatting with the extracted
	// components of the time value.
	fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
		t.Year(), t.Month(), t.Day(),
		t.Hour(), t.Minute(), t.Second())

	// `Parse` will return an error on malformed input
	// explaining the parsing problem.
	ansic := "Mon Jan _2 15:04:05 2006"
	_, e = time.Parse(ansic, "8:41PM")
	p(e)
}

Summary

In this lab, we learned how to format and parse time in Golang using the time package. We used the time.RFC3339 layout to format and parse time and the Mon Jan 2 15:04:05 MST 2006 reference time to show the pattern with which to format/parse a given time/string. We also used the Parse and Format functions to parse and format time, respectively.

Other Go Tutorials you may like