Skip to content

Instantly share code, notes, and snippets.

@ItalyPaleAle
Created October 7, 2020 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ItalyPaleAle/8af067d1bc1eec4fd0f9218acffe0fae to your computer and use it in GitHub Desktop.
Save ItalyPaleAle/8af067d1bc1eec4fd0f9218acffe0fae to your computer and use it in GitHub Desktop.
// Copyright (C) 2020 Alessandro Segala (ItalyPaleAle)
// License: MIT
// MyGoFunc returns a Go time.Time to JavaScript
func MyGoFunc() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
// Get the current time as a Go time.Time object
now := time.Now()
// Get the Date object constructor from JavaScript
dateConstructor := js.Global().Get("Date")
// Return a new JS "Date" object with the time from the Go "now" variable
// We're passing the UNIX timestamp to the "Date" constructor
// Because JS uses milliseconds for UNIX timestamp, we need to multiply the timestamp by 1000
return dateConstructor.New(now.Unix() * 1000)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment