duanjiaoxi4928 2018-11-08 07:52
浏览 702
已采纳

相当于strtotime的Golang(“本周日23:59:59”)

I am writing a scraper that scrapes offers off websites and these offers have end dates. One such website has offers that expire every Sunday. I have gone through the golang time documentation but still dont get how that can be done the equivalence I found in PHP and is pretty simple.

$endDate = strtotime('this Sunday, 23:59:59');

Is there a golang way to do this?

  • 写回答

1条回答 默认 最新

  • doufu8887 2018-11-08 10:27
    关注

    Write a function in Go using the Go standard library time package. For example,

    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())
    }
    

    Playground: https://play.golang.org/p/T0oZGRO9NV8

    Output:

    2018-11-08 05:25:01.104445722 -0500 EST Thursday
    2018-11-11 23:59:59.999999999 -0500 EST Sunday
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大