📜  Golang 中的解析时间

📅  最后修改于: 2021-10-25 02:52:19             🧑  作者: Mango

解析时间就是把我们的时间转换成Golang的时间对象,方便我们从中提取日期、月份等信息。我们可以通过使用time.Parse函数,它接受我们的时间字符串和格式,使我们的字符串写成输入解析任何时间,如果在我们的格式没有错误,它会返回一个Golang时间对象。

例子:

Input : "2018-04-24"
Output : 2018-04-24 00:00:00 +0000 UTC

Input : "04/08/2017"
Output : 2017-04-08 00:00:00 +0000 UTC

代码:

// Golang program to show the parsing of time
package main
  
import (
    "fmt"
    "time"
)
  
func main() {
  
    // The date we're trying to 
    // parse, work with and format
    myDateString := "2018-01-20 04:35"
    fmt.Println("My Starting Date:\t", myDateString)
  
    // Parse the date string into Go's time object
    // The 1st param specifies the format,
    // 2nd is our date string
    myDate, err := time.Parse("2006-01-02 15:04", myDateString)
    if err != nil {
        panic(err)
    }
  
    // Format uses the same formatting style
    // as parse, or we can use a pre-made constant
    fmt.Println("My Date Reformatted:\t", myDate.Format(time.RFC822))
  
    // In YY-MM-DD
    fmt.Println("Just The Date:\t\t", myDate.Format("2006-01-02"))
}

输出:

My Starting Date:     2018-01-20 04:35
My Date Reformatted:     20 Jan 18 04:35 UTC
Just The Date:         2018-01-20