Skip to content

Instantly share code, notes, and snippets.

@anyu
Last active July 24, 2023 18:04
Show Gist options
  • Save anyu/d648d2adb45dd799a54d5588b36f9de5 to your computer and use it in GitHub Desktop.
Save anyu/d648d2adb45dd799a54d5588b36f9de5 to your computer and use it in GitHub Desktop.
convert-date-time-to-unix-with-timezone
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