开发工具
Go时间戳助手
Golang时间包示例和实用程序
Go Timestamp Examples
Complete guide to working with timestamps in Go. Learn time package, formatting, and best practices.
Get Current Timestamp
Get current Unix timestamp in Go
package main
import (
"fmt"
"time"
)
func main() {
// Get current Unix timestamp in seconds
now := time.Now()
unixSeconds := now.Unix()
fmt.Println("Unix seconds:", unixSeconds) // 1729584000
// Get Unix timestamp in milliseconds
unixMilli := now.UnixMilli()
fmt.Println("Unix milliseconds:", unixMilli) // 1729584000000
// Get Unix timestamp in nanoseconds
unixNano := now.UnixNano()
fmt.Println("Unix nanoseconds:", unixNano)
// Get Unix timestamp in microseconds
unixMicro := now.UnixMicro()
fmt.Println("Unix microseconds:", unixMicro)
}Convert Timestamp to Time
Convert Unix timestamp to time.Time object
package main
import (
"fmt"
"time"
)
func main() {
// From Unix seconds
timestamp := int64(1729584000)
t := time.Unix(timestamp, 0)
fmt.Println(t) // 2024-10-22 01:20:00 +0000 UTC
// From Unix milliseconds
timestampMilli := int64(1729584000000)
t2 := time.UnixMilli(timestampMilli)
fmt.Println(t2)
// From Unix microseconds
timestampMicro := int64(1729584000000000)
t3 := time.UnixMicro(timestampMicro)
fmt.Println(t3)
// Format the time
formatted := t.Format("2006-01-02 15:04:05")
fmt.Println(formatted) // "2024-10-22 01:20:00"
}Parse String to Timestamp
Parse date strings and convert to timestamps
package main
import (
"fmt"
"time"
)
func main() {
// Parse RFC3339 (ISO 8601)
str := "2024-10-22T01:20:00Z"
t, err := time.Parse(time.RFC3339, str)
if err != nil {
panic(err)
}
fmt.Println(t.Unix()) // 1729584000
// Parse custom format
customStr := "2024-10-22 01:20:00"
layout := "2006-01-02 15:04:05"
t2, err := time.Parse(layout, customStr)
if err != nil {
panic(err)
}
fmt.Println(t2.Unix())
// Parse with timezone
loc, _ := time.LoadLocation("America/New_York")
t3, _ := time.ParseInLocation(layout, customStr, loc)
fmt.Println(t3.Unix())
}Quick Reference
Get Current Timestamp
time.Now().Unix()time.Now().UnixMilli()time.Now().UnixNano()Parse Timestamp
time.Unix(sec, 0)time.UnixMilli(ms)time.UnixMicro(micro)Format Time
t.Format("2006-01-02")t.Format(time.RFC3339)t.Format("01/02/2006")Parse String
time.Parse(layout, str)time.ParseInLocation(...)time.ParseDuration(str)Go时间戳助手
了解更多关于这个强大工具
Golang时间包示例和实用程序
用途
使用此工具查看 Unix 时间戳和时区的 Go 时间包示例。它可以帮助开发人员在生产前验证时间数据。输入值,选择格式或时区,然后复制日志、API、数据库、自动化或文档的准确结果。
优势
当您查看 Unix 时间戳和时区的 Go 时间包示例时,可以使用此工具更快地工作。验证可以尽早发现格式错误的值,并且复制就绪的结果可以防止转录错误。浏览器处理将数据保留在您的设备上,以便进行可重复检查。
工具特性
了解这款工具的强大功能
核心
易于使用
简单直观的界面,适合所有用户。
性能
快速处理
快速高效地处理时间戳。
质量
准确结果
精确的时间戳计算和转换。
为什么选择我们的时间戳工具?
专业级时间戳处理,企业级可靠性
100%
隐私保护
0ms
处理延迟
24/7
全天候可用
免费
永久免费
工作原理
简单步骤有效使用此时间戳工具
1
输入数据
在工具提供的输入字段中输入您的时间戳或日期值。
2
选择选项
选择您首选的输出格式和任何转换的附加选项。
3
获取结果
以高准确性和精度即时查看转换结果。
4
复制和使用
将结果复制到剪贴板,并在您的应用程序或项目中使用。