Last active
July 24, 2023 18:04
-
-
Save anyu/d648d2adb45dd799a54d5588b36f9de5 to your computer and use it in GitHub Desktop.
convert-date-time-to-unix-with-timezone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
inputDate := "2023-03-10" | |
inputTime := "00:00:00" | |
locationName := "America/Los_Angeles" | |
dateTimeStr := inputDate + " " + inputTime | |
location, err := time.LoadLocation(locationName) | |
if err != nil { | |
fmt.Println("Error loading location:", err) | |
return | |
} | |
parsedTime, err := time.ParseInLocation("2006-01-02 15:04:05", dateTimeStr, location) | |
if err != nil { | |
fmt.Println("Error parsing date and time:", err) | |
return | |
} | |
unixTimestamp := parsedTime.Unix() | |
fmt.Println("Date and Time:", parsedTime) | |
fmt.Println("Unix Timestamp:", unixTimestamp) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment