dragon2025 2016-02-19 22:17
浏览 290
已采纳

使用Go语言随时获取日,月和年

I have an object like this:

type searchObj struct {
    symbol string
    dataType string
    fromDate time.Time
    toDate time.Time
}

I want to be able to parse out the day, month, and year from the fromDate and the toDate. How can I do this? Is there a better type to use like (Date) because I do not need the time piece of it?

so I want to be able to pass a date like this 02/19/2016 and be able to get data.Day = 19, date.Month = 02, date.Year = 2016.

I was trying something like this:

search.fromDate.Date.Month
search.fromDate.Date.Day
search.fromDate.Date.Year

This is an example of what I am currently using to create the searchObj:

time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)

I am new to Go and thank you for the help!

  • 写回答

2条回答 默认 最新

  • dongzen7263 2016-02-19 22:29
    关注

    All the methods you're looking for exist on the time.Time type. You can just do;

    year := fromtDate.Year()
    month := fromDate.Month()
    day := fromDate.Day()
    

    EDIT: I suppose it would be more concise to use time.Date like so;

    year, month, day := fromDate.Date()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!