Search code examples
gotimedata-conversion

How to format `2022-11-20 21:00:00+0900` to IST


I have a timestamp 2022-11-20 21:00:00+0900 now I need to convert this to IST. So I tried this

    loc, _ := time.LoadLocation("Asia/Calcutta")
    format := "Jan _2 2006 3:04:05 PM"
    timestamp := "2022-11-20 21:00:00+0900"
    ISTformat, err := time.ParseInLocation(format, timestamp,  loc)
    fmt.Println(ISTformat, err)

but it was not worked and giving error cannot parse

what type of golang time format i need to use to do this?


Solution

  • try the following

        loc, _ := time.LoadLocation("Asia/Calcutta")
        format := "2006-01-02 15:04:05-0700"
        timestamp := "2022-11-20 21:00:00+0900"
        // ISTformat, _ := time.ParseInLocation(format, timestamp, loc)
        // fmt.Println(ISTformat)
        parsed_time, _ := time.Parse(format, timestamp)
        IST_time := parsed_time.In(loc)
        fmt.Println("Time in IST", IST_time)
    

    note that your format and timestamp should be in same time format