golang time conversion time zone conversion

ParseInLocation is similar to Parse with two important differences. First, when the time zone information is missing, Parse interprets the time as UTC time, and ParseInLocation sets the Location of the returned value to loc; second, when the time string provides time zone offset information, Parse will try to match the local time zone, and ParseInLocation will match loc.

 

func (t Time) In(loc *Location) Time

In returns a Time with the location and time zone specified by loc, but pointing to the same point in time. If loc is nil it will panic.


package main

import (
	"fmt"
	"time"
)

func main(){
	t := "2019-10-10 10:10:10"
	t1, _ := time.Parse("2006-01-02 15:04:05", t)
	t2, _ := time.ParseInLocation("2006-01-02 15:04:05", t, time.Local)
	fmt.Println(t1)
	fmt.Println(t2)
	fmt.Println(t1.Equal(t2))
	var cstSh, _ = time.LoadLocation("Asia/Shanghai") //上海
	fmt.Println("SH : ", time.Now().In(cstSh).Format("2006-01-02 15:04:05"))

	//时区转换
	fmt.Println("***************")
	t = "2021-01-11T23:46:05Z"
	t1, _ = time.Parse("2006-01-02T15:04:05Z", t)
	fmt.Println(t)
	fmt.Println("SH : ", t1.In(cstSh).Format("2006-01-02 15:04:05"))

}

result:

2019-10-10 10:10:10 +0000 UTC
2019-10-10 10:10:10 +0800 CST
false
SH :  2021-01-18 17:01:40
***************
2021-01-11T23:46:05Z
SH :  2021-01-12 07:46:05
 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324123309&siteId=291194637