Home > Backend Development > Golang > Convert Unix timestamp to time format using time.Unix function

Convert Unix timestamp to time format using time.Unix function

WBOY
Release: 2023-07-25 08:49:09
Original
2327 people have browsed it

Use time.Unix function to convert Unix timestamp to time format

Unix timestamp is a way of recording time in computer systems. It means since 00:00 on January 1, 1970 :00 UTC to the current number of seconds. When we need to convert Unix timestamps into time in a readable format during development, we can use the Unix functions provided by the time package of the Go language. This article explains how to use the time.Unix function to perform conversions and provides code examples.

First, we need to import the time package:

import (
    "fmt"
    "time"
)
Copy after login

Then, we can use the time.Unix function to convert the Unix timestamp into a time object of type time.Time. This function accepts two parameters: the first parameter is the seconds part of the timestamp, and the second parameter is the nanosecond part of the timestamp. We can use the time.Now function to get the current Unix timestamp. Here is a sample code:

timestamp := time.Now().Unix()
fmt.Printf("Unix时间戳:%d
", timestamp)

datetime := time.Unix(timestamp, 0)
fmt.Printf("转换后的时间:%s
", datetime)
Copy after login

Run the above code, the output will be similar to the following:

Unix时间戳:1613559506
转换后的时间:2021-02-17 10:05:06 +0800 CST
Copy after login

In the above code, we first use the time.Now().Unix() function Get the current Unix timestamp. We then use the time.Unix function to convert the timestamp into a time object of type time.Time. The first parameter of this function is the number of seconds of the timestamp, and the second parameter is the number of nanoseconds. Since Unix timestamps are only accurate to seconds, we can set the nanosecond part to 0. We can use the fmt.Printf function to format and output the converted time.

If we want to output time in a specified format, we can use the time.Time.Format function. This function accepts a string as parameter, representing the date and time formatting template. Here is a sample code:

timestamp := time.Now().Unix()
datetime := time.Unix(timestamp, 0)
formattedTime := datetime.Format("2006-01-02 15:04:05")
fmt.Printf("格式化后的时间:%s
", formattedTime)
Copy after login

Run the above code, the output will be similar to the following:

格式化后的时间:2021-02-17 10:05:06
Copy after login

In the above example, we used "2006-01-02 15:04: 05" as the formatting template for time. In Go language, this template is fixed, it is the birth time of Go language, so we can use this template to format date and time.

Through the introduction of this article, we have learned how to use the time.Unix function in the time package of the Go language to convert Unix timestamps into time format. We can format and output the converted time as needed. Using the time.Unix function can easily convert between timestamps and time formats during development, thereby better handling time-related operations.

The above is the detailed content of Convert Unix timestamp to time format using time.Unix function. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template