Golang : time.Time.Equal() function example
package time
Golang : time.Time.Equal() function usage example.
NOTE : Comparing time that are same but from different location will be true.
package main
import (
"fmt"
"time"
)
func main() {
timeString := "Aug 6, 2015 at 6:00am (UTC)"
layOut := "Jan 2, 2006 at 3:04pm (MST)"
timePresentMYT, err := time.Parse(layOut, timeString)
if err != nil {
fmt.Println(err)
}
// see also :
// https://www.socketloop.com/tutorials/golang-get-local-time-and-equivalent-time-in-different-time-zone
est, _ := time.LoadLocation("EST")
timePresentEST := time.Date(2015, 8, 6, 1, 0, 0, 0, est)
future := time.Now().Add(48 * time.Hour)
fmt.Println("Present MYT: ", timePresentMYT)
fmt.Println("Present EST : ", timePresentEST)
fmt.Println("Both time is equal even in different location.")
fmt.Println("Present MYT is equal to Present EST ? : ", timePresentMYT.Equal(timePresentEST))
fmt.Println("Future : ", future)
fmt.Println("Present MYT is equal to future ? : ", timePresentMYT.Equal(future))
}
Output :
Present MYT: 2015-08-06 06:00:00 +0000 UTC
Present EST : 2015-08-06 01:00:00 -0500 EST
Both time is equal even in different location.
Present MYT is equal to Present EST ? : true
Future : 2015-08-08 11:20:25.300126445 +0800 MYT
Present MYT is equal to future ? : false
Reference :
Advertisement
Something interesting
Tutorials
+17.4k Golang : Set, Get and List environment variables
+6.5k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+11.1k Golang : Forwarding a local port to a remote server example
+39.2k Golang : How to count duplicate items in slice/array?
+10.7k Golang : md5 hash of a string
+9.8k Golang : Removes punctuation or defined delimiter from the user's input
+20.8k Golang : Securing password with salt
+20.2k SSL : How to check if current certificate is sha1 or sha2
+6.8k Swift : Convert (cast) String to Double
+8.8k Golang : Translate language with language package example