golang time-timestamp acquisition-conversion-calculation

1. Acquisition time

1.1 Get the current time

Code

package main

import (
        "fmt"
        "time"
)

func main() {
   currentTime := time.Now()                  //当前时间
   currentYear := time.Now().Year()        //当前年
   currentMonth := time.Now().Month()       //当前月
   currentDay := time.Now().Day()         //当前日
   currentHour := time.Now().Hour()        //当前小时小时
   currentMinute := time.Now().Minute()      //当前分钟
   currentSecond := time.Now().Second()      //当前秒
   currentNSecond := time.Now().Nanosecond()  //当前纳秒
   
   //打印结果
   fmt.Println("当前时间是:", currentTime)
   fmt.Println("当前年:", currentYear)
   fmt.Println("当前月:", currentMonth)
   fmt.Println("当前日:", currentDay)
   fmt.Println("当前小时:", currentHour)
   fmt.Println("当前分钟:", currentMinute)
   fmt.Println("当前秒:", currentSecond)
   fmt.Println("当前纳秒:", currentNSecond)
}

result output

当前时间是: 2022-04-22 16:42:44.1160954 +0800 CST m=+0.004795301
当前年: 2022
当前月: April
当前日: 22
当前小时: 16
当前分钟: 42
当前秒: 44
当前纳秒: 150022700

1.2 Get before/after time

  • Get timestamp 1 minute ago
package main

import (
	"fmt"
	"time"
)

func main() {
	currentTime := time.Now()
	m, _ := time.ParseDuration("-1m")
	result := currentTime.Add(m)
	fmt.Println(result)
}

illustrate

  • time.ParseDuration converts the incoming "-1m" to a "Duration" type (time.Duration), the output is -0h1m0s
  • Then Add() can calculate it and time

result output

2022-04-22 16:43:20.9844622 +0800 CST m=-59.994691699
  • Get the time one hour ago

The method is the same as above, and the duration can be converted as follows: time.ParseDuration("-1h")

package main

import (
	"fmt"
	"time"
)

func main() {
	currentTime := time.Now()
	m, _ := time.ParseDuration("-1h")
	result := currentTime.Add(m)
	fmt.Println(result)
}

result output

2022-04-22 15:47:01.9977133 +0800 CST m=-3599.992986699
  • Get the time after 1 hour

The method is the same as above, and the duration can be converted as follows: ime.ParseDuration("1h")

package main

import (
	"fmt"
	"time"
)

func main() {
	currentTime := time.Now()
	m, _ := time.ParseDuration("1h")
	result := currentTime.Add(m)
	fmt.Println(result)
}

result output

2022-04-22 17:50:58.3395738 +0800 CST m=+3600.004424801

2. Get timestamp

2.1 Get the current timestamp

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Printf("时间戳(秒):%v;\n", time.Now().Unix())
	fmt.Printf("时间戳(纳秒):%v;\n",time.Now().UnixNano())
	fmt.Printf("时间戳(毫秒):%v;\n",time.Now().UnixNano() / 1e6)
	fmt.Printf("时间戳(纳秒转换为秒):%v;\n",time.Now().UnixNano() / 1e9)
}

output result

时间戳(秒):1650617834;
时间戳(纳秒):1650617834110539400;
时间戳(毫秒):1650617834110;
时间戳(纳秒转换为秒):1650617834;

2.2 Time to timestamp

  • Code
package main

import (
	"fmt"
	"time"
)

func main() {
	timeStamp := time.Date(2022, 5, 20, 13, 14, 0, 0, time.Local).Unix()
	fmt.Println("时间转时间戳:",timeStamp)
}

result output

时间转时间戳: 1653023640
  • Example: Get the timestamp of the current day at 01:00:00
package main

import (
	"fmt"
	"time"
)


func main() {
	currentYear := time.Now().Year()
	currentMonth := time.Now().Month()
	currentDay := time.Now().Day()
	timeStamp := time.Date(currentYear, currentMonth, currentDay, 1, 0, 0, 0, time.Local).Unix()
	fmt.Println("当天某个时间点的时间戳:",timeStamp)
}

result output

当天某个时间点的时间戳: 1650560400

2.2 Timestamp to time

  • basic use
package main

import (
	"fmt"
	"time"
)

func main() {
	timeStr := time.Unix(1650617834, 0)
	fmt.Println(timeStr)
}

result output

2022-04-22 16:57:14 +0800 CST
  • Format output by template

Note: The time in the template format cannot be changed at will

package main

import (
	"fmt"
	"time"
)

func main() {
	timeLayout := "2006-01-02 15:04:05"
	timeStr := time.Unix(1653023640, 0).Format(timeLayout)
	fmt.Println(timeStr)
}

result output

2022-05-20 13:14:00
  • Template output current time

Same as the above example, just convert the current timestamp into a time string output

package main

import (
	"fmt"
	"time"
)

func main() {
	timeStamp := time.Now().Unix()
	timeLayout := "2006-01-02 15:04:05"
	timeStr := time.Unix(timeStamp, 0).Format(timeLayout)
	fmt.Println(timeStr)

}

result output

2022-04-22 17:21:31

3. Time calculation

3.1 Time plus time period

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

Example

package main

import (
	"fmt"
	"time"
)

func main() {
	currentTime := time.Now()
	m, _ := time.ParseDuration("-1m")
	result := currentTime.Add(m)
	fmt.Println(result)
}

result output

2022-04-22 17:23:04.2727584 +0800 CST m=-59.995884099

3.2 Calculate the difference between two times

  • grammar

The methods of timeOne - timeTwo are as follows:

timeOne.Sub(timeTwo)
  • Example
package main

import (
	"fmt"
	"time"
)

func main() {
	currentTime := time.Now()
	//创建时间1
	timeDuOne, _ := time.ParseDuration("-1h")
	timeOne := currentTime.Add(timeDuOne)
	fmt.Println("时间1:",timeOne)
	//创建时间2
	timeDuTwo, _ := time.ParseDuration("1h")
	timeTwo := currentTime.Add(timeDuTwo)
	fmt.Println("时间2:",timeTwo)
	//计算两时间
	dTime := timeOne.Sub(timeTwo)
	fmt.Println("两时间的差是", dTime)

	m := timeOne.Sub(timeTwo)
	fmt.Println("差值按分钟计:", m.Minutes())

	h := timeOne.Sub(timeTwo)
	fmt.Println("差值按小时计:", h.Hours())

	d := timeOne.Sub(timeTwo)
	fmt.Println("差值按天计算:", d.Hours()/24)
}
  • result output
时间1: 2022-04-22 16:28:04.525285 +0800 CST m=-3599.994894399
时间2: 2022-04-22 18:28:04.525285 +0800 CST m=+3600.005105601
两时间的差是 -2h0m0s
差值按分钟计: -120
差值按小时计: -2
差值按天计算: -0.08333333333333333

Guess you like

Origin blog.csdn.net/cljdsc/article/details/124351164