Search code examples
gostring-to-datetime

Cannot parse time as T when parsing RFC3339 string to time


I have a string from db , say

dbString := "2020-03-16 14:46:13 +0530 IST"

My requirement is to insert this string as Timestamptz into another table

I am trying to convert dbString into Time

timeToBeInserted := time.Parse(time.RFC3339,t.VO.DateLastModified)

I see the below error

+0000 UTC parsing time "2020-03-16 14:46:13 +0530 IST" as "2006-01-02T15:04:05Z07:00": cannot parse " 14:46:13 +0530 IST" as "T"

Solution

  • Your date string does not match RFC3339 format:

    RFC3339     = "2006-01-02T15:04:05Z07:00"
    

    You should use a custom format. The following one works with your string:

    dbString := "2020-03-16 14:46:13 +0530 IST"
    fmt.Println(time.Parse("2006-01-02 15:04:05 -0700 MST", dbString))
    
    
    // Output:
    // 2020-03-16 14:46:13 +0530 IST <nil>