{"id":1266,"date":"2020-01-31T18:24:50","date_gmt":"2020-01-31T18:24:50","guid":{"rendered":"https:\/\/golangbyexamples.com\/?p=1266"},"modified":"2020-01-31T18:26:25","modified_gmt":"2020-01-31T18:26:25","slug":"convert-time-timezones-go","status":"publish","type":"post","link":"https:\/\/golangbyexamples.com\/convert-time-timezones-go\/","title":{"rendered":"Convert time between different timezones in Go (Golang)"},"content":{"rendered":"\n
Every time.Time<\/strong> object has an associated location<\/strong> value. When you change the location of any time.Time<\/strong> object to any other location, then that instant of time is not changed. Only the location<\/strong> value associated with that time changes. The location<\/strong> associated with the time.Time object only has a presentation or display logic. <\/p>\n\n\n\n The In<\/strong> function can be used to change the location<\/strong> associated with a particular time.Time<\/strong> object. Whenever the In<\/strong> function is called on any time.Time<\/strong> object (say t) then, <\/p>\n\n\n\n Let’s see the below-working code which can be used to change the location value associated with a particular time.<\/p>\n\n\n\n Output:<\/strong><\/p>\n\n\n\n Every time.Time object has an associated location value. When you change the location of any time.Time object to any other location, then that instant of time is not changed. Only the location…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[175,178],"class_list":["post-1266","post","type-post","status-publish","format-standard","hentry","category-tech","tag-time-package","tag-time-time"],"yoast_head":"\npackage main\n\nimport (\n \"fmt\"\n \"time\"\n)\n\nfunc main() {\n now := time.Now()\n\n loc, _ := time.LoadLocation(\"UTC\")\n fmt.Printf(\"UTC Time: %s\\n\", now.In(loc))\n \n loc, _ = time.LoadLocation(\"Europe\/Berlin\")\n fmt.Printf(\"Berlin Time: %s\\n\", now.In(loc))\n\n loc, _ = time.LoadLocation(\"America\/New_York\")\n fmt.Printf(\"New York Time: %s\\n\", now.In(loc))\n\n loc, _ = time.LoadLocation(\"Asia\/Dubai\")\n fmt.Printf(\"Dubai Time: %s\\n\", now.In(loc))\n}<\/code><\/pre>\n\n\n\n
UTC Time: 2020-01-31 18:09:41.705858 +0000 UTC\nBerlin Time: 2020-01-31 19:09:41.705858 +0100 CET\nNew York Time: 2020-01-31 13:09:41.705858 -0500 EST\nDubai Time: 2020-01-31 22:09:41.705858 +0400 +04<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"