计算年,天,小时,分钟等两个日期之间的差异
I'm working on a Golang example that requires some date calculations. I was rather hoping that Go would provide some nice date libraries similar to the excellent Python datetime
module, but that doesn't appear to be the case.
How can I represent this python example in Go ?
from datetime import date
d0 = date(2013, 8, 18)
d1 = date(2018, 9, 26)
delta = d0 - d1
print delta.days
>>-1865
I've spent a fair bit of time looking around on how to do this I can't seem to find a definitive answer that is clear and concise and without caveats such as not properly calculating leap years etc.
This seems to be a fairly big limitation to what is becoming an excellent little language for building cross platform prototypes and eventually production applications.
I don't know how you've spent your fair bit of time looking and not finding anything, but the time
package of the standard library has everything you want.
Here is your example coded in Go:
d0 := time.Date(2013, 8, 18, 0, 0, 0, 0, time.UTC)
d1 := time.Date(2018, 9, 26, 0, 0, 0, 0, time.UTC)
delta := d0.Sub(d1)
fmt.Println(delta.Hours() / 24)
Output (as expected):
-1865
Try it on the Go Playground.