在Go语言中,可以使用`time.Now()`函数来获取当前时间的等效值。

huangapple go评论119阅读模式
英文:

Date.now() equivalent in Go

问题

在JavaScript中,我可以这样赋值:

var now = Date.now();

然后将now用作数字变量进行计算。

在Go语言中,time.Time类型似乎无法满足这个需求。Go语言中与JavaScript的Date.now()等效的是什么?

英文:

In JavaScript, I can assign:

var now = Date.now();

Then use now to calculate as a number variable

time.Time type in Go doesn't seem to meet this demand. What is the Go equivalent of JavaScript's Date.now() ?

答案1

得分: 17

Date.now() 返回自纪元以来的毫秒数(以协调世界时为准)。

> now() 方法返回自 1970 年 1 月 1 日 00:00:00 UTC 至今经过的毫秒数,以数字形式表示。

要在 Go 中获取这个值,你可以使用以下代码:

time.Now().UTC().UnixNano() / 1e6

或者在 Go 版本 1.17 之后可以使用:

time.Now().UTC().UnixMilli()
英文:

Date.now() returns milliseconds since epoch UTC

> The now() method returns the milliseconds elapsed since 1 January 1970
> 00:00:00 UTC up until now as a Number.

To get that in Go, you can use:

time.Now().UTC().UnixNano() / 1e6

or since Go version 1.17

time.Now().UTC().UnixMilli()

答案2

得分: 3

你可以使用"time"包中的"Now"函数来获取当前时间,示例如下:

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println(time.Now())
    fmt.Println(time.Now().Date())
}

示例输出:

2009-11-10 23:00:00 +0000 UTC
2009 November 10

以下是文档中对该函数的解释:

func Now() Time

Now函数返回当前的本地时间。

func (t Time) Date() (year int, month Month, day int)

Date函数返回时间t所在的年份、月份和日期。

你可以在Live Demo中查看示例。

英文:

You can use Now function from "time" package as follows:

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println(time.Now())
    fmt.Println(time.Now().Date())
}

Sample output:

2009-11-10 23:00:00 +0000 UTC
2009 November 10

Here is function explanation from documentation:

func Now() Time

Now returns the current local time.

func (t Time) Date() (year int, month Month, day int)

Date returns the year, month, and day in which t occurs.

Watch it in Live Demo.

答案3

得分: 3

Date.Now() 返回当前的 UTC 日期和时间,以 epoch(unix)格式表示。在 Go 中的等效方式是:

time.Now().Unix()

time.Now() 返回当前时间。调用 Unix() 将时间转换为 epoch 或 unix 时间,即 自 1970 年 1 月 1 日 UTC 起经过的秒数

完整的 GoDocs 时间文档

英文:

Date.Now() returns the current UTC date and time in epoch(unix) format. The equivalent in go would be:

time.Now().Unix()

time.Now() returns the current time. Calling Unix() converts the time to epoch or unix time, which is the number of seconds elapsed since January 1, 1970 UTC

Full GoDocs for Time

答案4

得分: 1

在Go语言中,你可以使用方法time.Now().Date()

英文:

In go you can use method time.Now().Date()

答案5

得分: 0

在Go语言中,你可以使用以下方法:

package main

import (
	"fmt"
	"time"
)

func main() {

	currentTime := time.Now()

	fmt.Println(currentTime.Date())  //

	fmt.Println("Current Time in String: ", currentTime.String())

	fmt.Println("MM-DD-YYYY : ", currentTime.Format("01-02-2006"))

	fmt.Println("YYYY-MM-DD : ", currentTime.Format("2006-01-02"))

	fmt.Println("YYYY.MM.DD : ", currentTime.Format("2006.01.02 15:04:05"))

	fmt.Println("YYYY#MM#DD {Special Character} : ", currentTime.Format("2006#01#02"))

	fmt.Println("YYYY-MM-DD hh:mm:ss : ", currentTime.Format("2006-01-02 15:04:05"))

	fmt.Println("Time with MicroSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000"))

	fmt.Println("Time with NanoSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000000"))

	fmt.Println("ShortNum Month : ", currentTime.Format("2006-1-02"))

	fmt.Println("LongMonth : ", currentTime.Format("2006-January-02"))

	fmt.Println("ShortMonth : ", currentTime.Format("2006-Jan-02"))

	fmt.Println("ShortYear : ", currentTime.Format("06-Jan-02"))

	fmt.Println("LongWeekDay : ", currentTime.Format("2006-01-02 15:04:05 Monday"))

	fmt.Println("ShortWeek Day : ", currentTime.Format("2006-01-02 Mon"))

	fmt.Println("ShortDay : ", currentTime.Format("Mon 2006-01-2"))

	fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5"))

	fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 PM"))

	fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 pm"))

	//	2021 February 10
	//	Current Time in String:  2021-02-10 11:47:30.5807222 +0530 +0530 m=+0.001994001
	//	MM-DD-YYYY :  02-10-2021
	//	YYYY-MM-DD :  2021-02-10
	//	YYYY.MM.DD :  2021.02.10 11:47:30
	//	YYYY#MM#DD {Special Character} :  2021#02#10
	//	YYYY-MM-DD hh:mm:ss :  2021-02-10 11:47:30
	//	Time with MicroSeconds:  2021-02-10 11:47:30.580722
	//	Time with NanoSeconds:  2021-02-10 11:47:30.580722200
	//	ShortNum Month :  2021-2-10
	//	LongMonth :  2021-February-10
	//	ShortMonth :  2021-Feb-10
	//	ShortYear :  21-Feb-10
	//	LongWeekDay :  2021-02-10 11:47:30 Wednesday
	//	ShortWeek Day :  2021-02-10 Wed
	//	ShortDay :  Wed 2021-02-10
	//	Short Hour Minute Second:  2021-02-10 11:47:30
	//	Short Hour Minute Second:  2021-02-10 11:47:30 AM
	//	Short Hour Minute Second:  2021-02-10 11:47:30 am

}

这段代码展示了在Go语言中如何使用时间相关的方法。

英文:

In go you can use these methods

package main
import (
"fmt"
"time"
)
func main() {
currentTime := time.Now()
fmt.Println(currentTime.Date())  //
fmt.Println("Current Time in String: ", currentTime.String())
fmt.Println("MM-DD-YYYY : ", currentTime.Format("01-02-2006"))
fmt.Println("YYYY-MM-DD : ", currentTime.Format("2006-01-02"))
fmt.Println("YYYY.MM.DD : ", currentTime.Format("2006.01.02 15:04:05"))
fmt.Println("YYYY#MM#DD {Special Character} : ", currentTime.Format("2006#01#02"))
fmt.Println("YYYY-MM-DD hh:mm:ss : ", currentTime.Format("2006-01-02 15:04:05"))
fmt.Println("Time with MicroSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000"))
fmt.Println("Time with NanoSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000000"))
fmt.Println("ShortNum Month : ", currentTime.Format("2006-1-02"))
fmt.Println("LongMonth : ", currentTime.Format("2006-January-02"))
fmt.Println("ShortMonth : ", currentTime.Format("2006-Jan-02"))
fmt.Println("ShortYear : ", currentTime.Format("06-Jan-02"))
fmt.Println("LongWeekDay : ", currentTime.Format("2006-01-02 15:04:05 Monday"))
fmt.Println("ShortWeek Day : ", currentTime.Format("2006-01-02 Mon"))
fmt.Println("ShortDay : ", currentTime.Format("Mon 2006-01-2"))
fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5"))
fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 PM"))
fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 pm"))
//	2021 February 10
//	Current Time in String:  2021-02-10 11:47:30.5807222 +0530 +0530 m=+0.001994001
//	MM-DD-YYYY :  02-10-2021
//	YYYY-MM-DD :  2021-02-10
//	YYYY.MM.DD :  2021.02.10 11:47:30
//	YYYY#MM#DD {Special Character} :  2021#02#10
//	YYYY-MM-DD hh:mm:ss :  2021-02-10 11:47:30
//	Time with MicroSeconds:  2021-02-10 11:47:30.580722
//	Time with NanoSeconds:  2021-02-10 11:47:30.580722200
//	ShortNum Month :  2021-2-10
//LongMonth :  2021-February-10
//ShortMonth :  2021-Feb-10
//ShortYear :  21-Feb-10
//LongWeekDay :  2021-02-10 11:47:30 Wednesday
//	ShortWeek Day :  2021-02-10 Wed
//ShortDay :  Wed 2021-02-10
//	Short Hour Minute Second:  2021-02-10 11:47:30
//	Short Hour Minute Second:  2021-02-10 11:47:30 AM
//	Short Hour Minute Second:  2021-02-10 11:47:30 am
}

答案6

得分: 0

Go

time.Now().UTC().UnixMilli()

参考链接:https://pkg.go.dev/time#Time.UnixMilli

> 注意:我在发现这个问题时使用的是 go 1.20 版本。

Javascript

Date.now()

两者将得到相同的结果:1677469641253

英文:

Go

time.Now().UTC().UnixMilli()

See reference: https://pkg.go.dev/time#Time.UnixMilli

> Note: I am using go 1.20 when I discovered this.

Javascript

Date.now()

Both will have the same result: 1677469641253

huangapple
  • 本文由 发表于 2016年3月17日 11:31:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/36051177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定