GO语言系列(四)

今天来看看一些关于时间操作的基本示例代码,一个个来吧。

1. 如何获取当前时区和其他时区的当前时间?

package main

import (

"fmt"

"time"

)

func main() {

t := time.Now()

fmt.Println("Location : ", t.Location(), " Time : ", t) // local time

location, err := time.LoadLocation("America/New_York")

if err != nil {

fmt.Println(err)

}

fmt.Println("Location : ", location, " Time : ", t.In(location)) // America/New_York

loc, _ := time.LoadLocation("Asia/Shanghai")

now := time.Now().In(loc)

fmt.Println("Location : ", loc, " Time : ", now) // Asia/Shanghai

}

运行结果:

C:\golang\time>go run t1.go

Location : Local Time : 2017-08-26 21:04:56.1874497 +0530 IST

Location : America/New_York Time : 2017-08-26 11:34:56.1874497 -0400 EDT

Location : Asia/Shanghai Time : 2017-08-26 23:34:56.1884498 +0800 CST

C:\golang\time>

2. 使用Weekday和YearDay方法的示例:

package main

import (

"fmt"

"time"

)

func main() {

t,_ := time.Parse("2006 01 02 15 04", "2015 11 11 16 50")

fmt.Println(t.YearDay()) // 315

fmt.Println(t.Weekday()) // Wednesday

t,_ = time.Parse("2006 01 02 15 04", "2011 01 01 0 00")

fmt.Println(t.YearDay())

fmt.Println(t.Weekday())

}

输出结果:

C:\golang\time>go run t7.go

315

Wednesday

1

Saturday

3. 获取当前时间对应的年、月、日的代码:

package main

import (

"fmt"

"time"

)

func main() {

currentTime := time.Now()

fmt.Println("\n######################################\n")

fmt.Println(currentTime.Format("2006-01-02 15:04:05"))

fmt.Println("\n######################################\n")

timeStampString := currentTime.Format("2006-01-02 15:04:05")

layOut := "2006-01-02 15:04:05"

timeStamp, err := time.Parse(layOut, timeStampString)

if err != nil {

fmt.Println(err)

}

hr, min, sec := timeStamp.Clock()

fmt.Println("Year :", currentTime.Year())

fmt.Println("Month :", currentTime.Month())

fmt.Println("Day :", currentTime.Day())

fmt.Println("Hour :", hr)

fmt.Println("Min :", min)

fmt.Println("Sec :", sec)

fmt.Println("\n######################################\n")

year, month, day := time.Now().Date()

fmt.Println("Year :", year)

fmt.Println("Month :", month)

fmt.Println("Day :", day)

fmt.Println("\n######################################\n")

t := time.Now()

y := t.Year()

mon := t.Month()

d := t.Day()

h := t.Hour()

m := t.Minute()

s := t.Second()

n := t.Nanosecond()

fmt.Println("Year :",y)

fmt.Println("Month :",mon)

fmt.Println("Day :",d)

fmt.Println("Hour :",h)

fmt.Println("Minute :",m)

fmt.Println("Second :",s)

fmt.Println("Nanosec:",n)

}

输出结果:

C:\golang\time>go run t7.go######################################2017-08-27 18:11:54######################################Year : 2017Month : AugustDay : 27Hour : 18Min : 11Sec : 54######################################Year : 2017Month : AugustDay : 27######################################Year : 2017Month : AugustDay : 27Hour : 18Minute : 11Second : 54Nanosec: 319513100

4. UTC时间转变为其他时区时间:

package main

import (

"fmt"

"time"

)

func main() {

t, err := time.Parse("2006 01 02 15 04", "2015 11 11 16 50")

if err != nil {

fmt.Println(err)

}

fmt.Println(t)

loc, err := time.LoadLocation("America/Los_Angeles")

if err != nil {

fmt.Println(err)

}

fmt.Println(loc)

t = t.In(loc)

fmt.Println(t.Format(time.RFC822))

loc, err = time.LoadLocation("Singapore")

if err != nil {

fmt.Println(err)

}

fmt.Println(loc)

t = t.In(loc)

fmt.Println(t.Format(time.RFC822))

loc, err = time.LoadLocation("US/Hawaii")

if err != nil {

fmt.Println(err)

}

fmt.Println(loc)

t = t.In(loc)

fmt.Println(t.Format(time.RFC822))

loc, err = time.LoadLocation("EST")

if err != nil {

fmt.Println(err)

}

fmt.Println(loc)

t = t.In(loc)

fmt.Println(t.Format(time.RFC822))

loc, err = time.LoadLocation("MST")

if err != nil {

fmt.Println(err)

}

fmt.Println(loc)

t = t.In(loc)

fmt.Println(t.Format(time.RFC822))

}

输出结果:

C:\golang\time>go run t7.go2015-11-11 16:50:00 +0000 UTCAmerica/Los_Angeles11 Nov 15 08:50 PSTSingapore12 Nov 15 00:50 SGTUS/Hawaii11 Nov 15 06:50 HSTEST11 Nov 15 11:50 ESTMST11 Nov 15 09:50 MST

发个小广告!!!走过路过,不要错过!新书来啦!!!

注:本公众号与当当店铺并无从属关系,仅为大家提供一个便捷购物地址。若有所冲突,纯属巧合,立删。

麦克叔叔每晚十点说

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180416B1O90L00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。

扫码关注云+社区

领取腾讯云代金券