Date.now() equivalent in Go

Viewed 4938

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

5 Answers

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

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

}
Related