Date 使用Go语言获取日期、月份和年份
我有一个这样的物体:Date 使用Go语言获取日期、月份和年份,date,datetime,go,Date,Datetime,Go,我有一个这样的物体: type searchObj struct { symbol string dataType string fromDate time.Time toDate time.Time } search.fromDate.Date.Month search.fromDate.Date.Day search.fromDate.Date.Year 我希望能够从fromDate和toDate解析出日期、月份和年份。我该怎么做?是否有更好的类型可以使用
type searchObj struct {
symbol string
dataType string
fromDate time.Time
toDate time.Time
}
search.fromDate.Date.Month
search.fromDate.Date.Day
search.fromDate.Date.Year
我希望能够从fromDate和toDate解析出日期、月份和年份。我该怎么做?是否有更好的类型可以使用,比如(日期),因为我不需要它的时间段
因此,我希望能够通过这样一个日期2016年2月19日,并能够获得数据。日期=19,日期。月份=02,日期。年份=2016
我试着这样做:
type searchObj struct {
symbol string
dataType string
fromDate time.Time
toDate time.Time
}
search.fromDate.Date.Month
search.fromDate.Date.Day
search.fromDate.Date.Year
这是我当前用于创建searchObj的示例:
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
我是新手,谢谢你的帮助 您要查找的所有方法都存在于
time.time
类型中。你可以这样做
year := fromDate.Year()
month := fromDate.Month()
day := fromDate.Day()
编辑:
我想用时间会更简洁
year, month, day := fromDate.Date()
time
类型还有一个方法,可以在一次调用中返回时间的年、月和日。使用可接受的方法,如果您需要月号,您可以简单地将月对象强制转换为int,因为它是一个枚举:
year, month, day := time.Now().Date()
log.Printf("%v-%v-%v", year, int(month), day)
那么
time.time上的Month()
、Day()
和Year()
方法呢?@JimB:太贵了。使用@peterSO,是的,我自己没有检查文档;)这就是我要找的。谢谢,很抱歉这个错误的问题。@user3788671这是nbd。对于熟悉包/类型的人来说,这可能是显而易见的,但如果它还没有打开,那么询问它是有价值的。