我想合并两个字符串的日期:,当日期相同时返回true。我想忽略小时。"2021-05-30T15:00:00""2021-05-30"
"2021-05-30T15:00:00" => 2021-05-30
"2021-05-30" => 2021-05-30
2021-05-30 == 2021-05-30 // true
为了实现这一点,我制作了两个格式化日期的函数。 表示带有小时的日期。 表示不带小时的字符串日期。timeParsersimplifyDate
const dateFormat = "2006-01-02T15:04:05"
const simpleDateFormat = "2006-01-02"
const departureTime = "2021-05-30T15:00:00"
const startDate = "2021-05-30"
func timeParser(format, value string) time.Time {
parsed, err := time.Parse(format, value)
if err != nil {
fmt.Println("timeParser Error: ", err)
}
return parsed
}
func simplifyDate(value string) time.Time {
parsed, err := time.Parse(dateFormat, value)
parsed.Format(simpleDateFormat)
if err != nil {
fmt.Println("timeParser Error: ", err)
}
return parsed
}
isStartDate := simplifyDate(departureTime) == timeParser(simpleDateFormat, startDate) // return false
fmt.Println("simplifyDate(departureTime)", simplifyDate(departureTime)) //2021-05-30 15:00:00 +0000 UTC
fmt.Println("timeParser(simpleDateFormat, startDate)", timeParser(simpleDateFormat, startDate)) // 2021-05-30 00:00:00 +0000 UTC
在,我尝试按原样将日期格式化为DD-MM-YYYY。但是我得到的日期在末尾有小时和零,如, 。你能指出我做错了什么吗?timeParsersimplifyDatesimpleDateFormat = "2006-01-02"2021-05-30 00:00:00 +0000 UTC2021-05-30 15:00:00 +0000 UTC
一只名叫tom的猫
largeQ
相关分类