Date.now() equivalent in Go
Asked Answered
I

7

11

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() ?

Innocence answered 17/3, 2016 at 3:31 Comment(1)
D
19

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()
Dermal answered 17/3, 2016 at 7:22 Comment(2)
What does the / 1e6 part do?Harpsichord
UnixNano() returns Nanoseconds. To get from Nano to Milli, you divide by 1000000.Dermal
E
3

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.

Embroil answered 17/3, 2016 at 3:38 Comment(0)
E
3

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

Eweneck answered 17/3, 2016 at 15:7 Comment(0)
A
1

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

Apophyllite answered 9/2, 2021 at 9:46 Comment(0)
E
0

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

}
Eyebolt answered 10/2, 2021 at 6:19 Comment(0)
C
0

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

Copycat answered 27/2, 2023 at 3:56 Comment(0)
J
0

It returns milliseconds since epoch time and so it is time.Now().UnixNano()

Jerkin answered 17/7 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.