go language time / timestamp operation

Keywords: Go Unix

basic operation

01: get current time

dateTime := time.Now()
fmt.Println(dateTime)

02: get month day hour minute second nanosecond

year := time.Now().Year() //year

fmt.Println(year)

month := time.Now().Month() //month
fmt.Println(month)

day := time.Now().Day() //day
fmt.Println(day)

hour := time.Now().Hour() //hour
fmt.Println(hour)

minute := time.Now().Minute() //Minute
fmt.Println(minute)

second := time.Now().Second() //second
fmt.Println(second)

nanosecond := time.Now().Nanosecond() //nanosecond
fmt.Println(nanosecond)

03: get current timestamp

timeUnix := time.Now().Unix()         //Unit second
timeUnixNano := time.Now().UnixNano() //Unit NS
    
fmt.Println(timeUnix)
fmt.Println(timeUnixNano)

04: format timestamp

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

05: time stamp converted to go format

var timeUnix int64 = 1562555859
fmt.Println(time.Unix(timeUnix,0))

// Then you can use Format, for example
fmt.Println(time.Unix(timeUnix, 0).Format("2006-01-02 15:04:05"))

06: str format time to timestamp

t := time.Date(2014, 1, 7, 5, 50, 4, 0, time.Local).Unix()

fmt.Println(t)

Calculation of time

01: get today's timestamp at 0:00:00

currentTime := time.Now()
startTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, currentTime.Location())
fmt.Println(startTime)
fmt.Println(startTime.Format("2006/01/02 15:04:05"))

02: get the timestamp of today's 23:59:59 seconds

currentTime := time.Now()
endTime := time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 23, 59, 59, 0, currentTime.Location())
fmt.Println(endTime)
fmt.Println(endTime.Format("2006/01/02 15:04:05"))

03: get the time before 1 minute

m, _ := time.ParseDuration("-1m")
result := currentTime.Add(m)

fmt.Println(result)
fmt.Println(result.Format("2006/01/02 15:04:05"))

04: get the time before 1 hour

m, _ := time.ParseDuration("-1h")
result := currentTime.Add(m)
fmt.Println(result)
fmt.Println(result.Format("2006/01/02 15:04:05"))

05: get time after 1 minute

m, _ := time.ParseDuration("1m")
result := currentTime.Add(m)

fmt.Println(result)
fmt.Println(result.Format("2006/01/02 15:04:05"))

06: get time after 1 hour

m, _ := time.ParseDuration("1h")
result := currentTime.Add(m)
fmt.Println(result)
fmt.Println(result.Format("2006/01/02 15:04:05"))

07: calculate two timestamps

afterTime, _ := time.ParseDuration("1h")
result := currentTime.Add(afterTime)

beforeTime, _ := time.ParseDuration("-1h")
result2 := currentTime.Add(beforeTime)

m := result.Sub(result2)
fmt.Printf("%v Minute \n", m.Minutes())

h := result.Sub(result2)
fmt.Printf("%v hour \n", h.Hours())

d := result.Sub(result2)
fmt.Printf("%v day\n", d.Hours()/24)

08: judge whether a time is after a time

stringTime, _ := time.Parse("2006-01-02 15:04:05", "2019-12-12 12:00:00")
beforeOrAfter := stringTime.After(time.Now())

if true == beforeOrAfter {
    fmt.Println("2019-12-12 12:00:00 After current time!")
} else {
    fmt.Println("2019-12-12 12:00:00 Before current time!")
}

09: judge how long one time has passed compared with another

startTime := time.Now()
time.Sleep(time.Second * 5)

fmt.Println("It's past now:", time.Since(startTime))

Posted by djpeterlewis on Thu, 31 Oct 2019 03:52:56 -0700