时间格式化与解析

GolangGolangBeginner
立即练习

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

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

本实验聚焦于Go语言中的时间格式化与解析。Go语言提供了基于模式的布局用于时间格式化与解析。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Golang`")) -.-> go/AdvancedTopicsGroup(["`Advanced Topics`"]) go/AdvancedTopicsGroup -.-> go/time_formatting_parsing("`Time Formatting Parsing`") subgraph Lab Skills go/time_formatting_parsing -.-> lab-15521{{"`时间格式化与解析`"}} end

时间格式化与解析

问题在于使用提供的布局在Go语言中格式化和解析时间。

  • 使用time包来格式化和解析时间。
  • 使用time.RFC3339布局来格式化和解析时间。
  • 使用Mon Jan 2 15:04:05 MST 2006这个参考时间来展示用于格式化/解析给定时间/字符串的模式。
  • 使用Parse函数来解析时间。
  • 使用Format函数来格式化时间。
  • 使用fmt.Println函数来打印格式化后的时间。
  • 使用fmt.Printf函数来打印带有提取组件的格式化时间。
$ 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
解析时间“8:41PM”为“Mon Jan _2 15:04:05 2006”时:...

以下是完整代码:

// Go语言通过基于模式的布局支持时间格式化和解析。

package main

import (
	"fmt"
	"time"
)

func main() {
	p := fmt.Println

	// 这是一个根据RFC3339格式化时间的基本示例,使用相应的布局常量。
	t := time.Now()
	p(t.Format(time.RFC3339))

	// 时间解析使用与`Format`相同的布局值。
	t1, e := time.Parse(
		time.RFC3339,
		"2012-11-01T22:08:41+00:00")
	p(t1)

	// `Format`和`Parse`使用基于示例的布局。通常,你会为这些布局使用`time`包中的常量,但你也可以提供自定义布局。布局必须使用参考时间`Mon Jan 2 15:04:05 MST 2006`来展示用于格式化/解析给定时间/字符串的模式。示例时间必须与显示的完全一致:2006年,小时为15,星期一是一周中的某天,等等。
	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)

	// 对于纯数字表示,你也可以使用标准字符串格式化以及时间值的提取组件。
	fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
		t.Year(), t.Month(), t.Day(),
		t.Hour(), t.Minute(), t.Second())

	// `Parse`在输入格式错误时将返回一个错误,解释解析问题。
	ansic := "Mon Jan _2 15:04:05 2006"
	_, e = time.Parse(ansic, "8:41PM")
	p(e)
}

总结

在本实验中,我们学习了如何使用time包在Go语言中格式化和解析时间。我们使用time.RFC3339布局来格式化和解析时间,并使用Mon Jan 2 15:04:05 MST 2006这个参考时间来展示用于格式化/解析给定时间/字符串的模式。我们还分别使用了ParseFormat函数来解析和格式化时间。

您可能感兴趣的其他 Golang 教程