I have below code. Having date value as string type from request and trying to convert to time type. But, I have some issue with format.
type LeanData struct {
Start_Date time.Time
}
const dateLayout = "01-02-2006"
startdate := request.FormValue("startdate")
if len(strings.TrimSpace(startdate)) > 0 {
sdate, err := time.Parse(dateLayout, startdate)
}
fmt.Println("startdate", startdate)
fmt.Println("sdate", sdate)
I have below output.
startdate 02-03-2016
sdate 2016-02-03 00:00:00 +0000 UTC
Here, I am doing conversion because start date is of type time.Time. I want to convert it as 2016-02-03 but not with 2016-02-03 00:00:00 +0000 UTC. Also, How to assign empty value to start date if value from request is nil/empty.
Can someone let me know how to achieve this?
time.Parse
returns an object of type time.Time
which contains date and time information. There is no type in Go only containing date information. You can however ignore the time part of the date when formatting it, eg:
fmt.Println("sdate", sdate.Format("2006-01-02"))
which will print:
sdate 2016-02-03
To initialize an empty time, just declare it as:
var sdate time.Time