Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis on the Application of Go language time package

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of Go language time package application case analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Go language time package application case analysis article. Let's take a look at it.

1. Preface

Time and date are a very important package for any programming language. The GO language provides time packages to measure and display time. You can either get the current time based on the selected time zone, or add the duration of the current time zone using the time package, and so on.

two。 Representation of date and time

The time package provides a time type, which is used to represent a specific moment in time, and has the following functions:

Now () function: returns the current time of the current time zone

Date (y, m, d, h, min, sec, nsec, loc): this function creates a time that represents a specified point in time represented by year, month, day, hour, minute, second, nanosecond, and position parameters.

Unix (sec, nsec): this function creates a time value, usually called Unix time, from the number of seconds and nanoseconds since UTC on January 1, 1970.

Current time

You can get the current time directly through the time.Now () function:

Package mainimport ("fmt"time") func main () {now: = time.Now () fmt.Println ("Hello, learn Dates And Times") fmt.Printf ("now Beijing time:% s", now)}

Run the code:

$go run main.go

Hello, learn Dates And Times together.

It is now Beijing time: 2022-04-23 23 20 CST 10.3383785 + 0800 Beijing time 0.004403601

Date function

The date function returns the time corresponding to yyyy-mm-dd hh:mm:ss + nsec nanoseconds.

Package mainimport ("fmt"time") func main () {fmt.Println ("Hello, learn Dates And Times") date: = time.Date (2022, 02, 22, 22, 22, 22, 222222222, time.UTC) fmt.Printf ("Date is% s\ n", date)}

Run the code:

$go run main.go

Hello, learn Dates And Times together.

Date is 2022-02-2222: 22 22.222222222 + 0000 UTC

How to get the timestamp of the current UNIX in Golang

You can use the now () method to get the current time, and its Unix () method helps us convert the time to a UNIX timestamp.

Package mainimport ("fmt"time") func main () {fmt.Println ("Hello, learn Dates And Times") cur_time: = time.Now () .Unix () fmt.Printf ("current Unix timestamp:% v\ n", cur_time)}

Run the code and record the only Unix timestamp on which you are running the code at this time:

Hello, learn Dates And Times together.

Current Unix timestamp: 1650728863

3. Methods of accessing time components

How to add year, month, and day to the current time

The AddDate method helps add a year, month, and day to the corresponding date, which can be any date. You can pass integer values of year, month, and day. The return type is time. You can pass any of the three parameters.

Package mainimport ("fmt"time") func main () {fmt.Println ("Hello, learn Dates And Times") date: = time.Date (2022, 02, 22, 22, 22, 22, 222222222, time.UTC) next_date: = date.AddDate (1,2,1) fmt.Printf ("Date is% s\ n", date) fmt.Printf ("Next date is% s\ n", next_date)}

Running result:

Hello, learn Dates And Times together.

Date is 2022-02-2222: 22 22.222222222 + 0000 UTC

Next date is 2023-04-23 22 22 22 22 22 222 + 0000 UTC

How to add hours, minutes, and seconds to the current time

The time package provides an add () method to get the future time based on the passing value. This method requires integer hours, minutes, and seconds.

Package mainimport ("fmt"time") func main () {fmt.Println ("Hello, learn Dates And Times") cur_time: = time.Now () next_time: = cur_time.Add (time.Hour*2 + time.Minute*1 + time.Second*22) fmt.Printf ("current time:% s\ n", cur_time) fmt.Printf ("next time period added:% s\ n", next_time)}

Running result:

Hello, learn Dates And Times together.

Current time: 2022-04-23 23 42purl 26.6081221 + 0800 CST masks 0.006779901

The next time period after the time is added: 2022-04-24 01PUBG 43PUBG 48.6081221 + 0800 CST massively 7282.006779901

In addition to the week, two types are defined to help describe the components of the time value:

How do I parse a date string in Golang?

Package mainimport ("fmt"time") func main () {fmt.Println ("Hello, study Dates And Times") date: = "2022-02-22T22:22:22.221Z" parse_time, _: = time.Parse (time.RFC3339, date) fmt.Printf ("Custom date:% s\ n", date) fmt.Printf ("parsing time:% s\ n", parse_time)}

Run the code:

Hello, learn Dates And Times together.

Custom date: 2022-02-22T22:22:22.221Z

Parsing time: 2022-02-22 22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22-22

This is the end of the article on "Go language time package Application example Analysis". Thank you for reading! I believe you all have a certain understanding of the knowledge of "Go language time package Application case Analysis". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 281

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report