我正在写一个从网站上刮除报价的刮板,这些报价的结束日期。一个这样的网站提供的优惠在每个星期日到期。我已经遍历了golang时间文档,但仍然不知道如何实现与PHP中的等效功能,而且非常简单。
$endDate = strtotime('this Sunday, 23:59:59');
有golang方法可以做到这一点吗?
答案 0 :(得分:1)
使用Go standard library time package在Go中编写函数。例如,
package main
import (
"fmt"
"time"
)
func endDate(t time.Time, wd time.Weekday) time.Time {
next := int((wd - t.Weekday() + 7) % 7)
y, m, d := t.Date()
return time.Date(y, m, d+next+1, 0, 0, 0, -1, t.Location())
}
func main() {
now := time.Now().Round(0)
fmt.Println(now, now.Weekday())
end := endDate(now, time.Sunday)
fmt.Println(end, end.Weekday())
}
游乐场:https://play.golang.org/p/T0oZGRO9NV8
输出:
2018-11-08 05:25:01.104445722 -0500 EST Thursday
2018-11-11 23:59:59.999999999 -0500 EST Sunday