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()
?
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()
?
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()
/ 1e6
part do? –
Harpsichord UnixNano()
returns Nanoseconds. To get from Nano to Milli, you divide by 1000000. –
Dermal 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.
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
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
}
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
It returns milliseconds since epoch time and so it is time.Now().UnixNano()
© 2022 - 2024 — McMap. All rights reserved.