douan7529 2016-04-24 23:49
浏览 1412
已采纳

在Go / Golang中获取当月的第一天和最后一天?

I'm trying to get the first and last day of the current month. You can add days and hours but not the month, which I was thinking of subtracting one day from the next month to get the last day of this month. Something like this:

package main

import (
    "fmt"
    "time"
)

func main() {

    date := time.Now()
    nextMonth := date.Add(time.Month)
    LastDay := nextMonth.Add(-time.Hour * 24)

    fmt.Println(LastDay)

}
  • 写回答

7条回答 默认 最新

  • duan5991518 2016-04-25 00:20
    关注

    You can use now library, it really simple :

    now.BeginningOfMonth()    // 2013-11-01 00:00:00 Fri
    now.EndOfMonth()          // 2013-11-30 23:59:59.999999999 Sat
    

    Please take a look here for detail : https://github.com/jinzhu/now

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器