计算两个日期之间的天数。

huangapple go评论94阅读模式
英文:

Calculate number of days between two dates?

问题

如何计算两个日期之间的天数?在下面的代码中,我应该得到小时数,这意味着我只需要除以24。然而,我得到的结果是类似于**-44929.000000**的值。我只是想要回溯一两天,所以我期望得到24或48小时。

package main

import (
	"fmt"
	"time"
)

func main() {
	timeFormat := "2006-01-02"

	t, _ := time.Parse(timeFormat, "2014-12-28")
	fmt.Println(t)
	// duration := time.Since(t)
	duration := time.Now().Sub(t)
	fmt.Printf("%f", duration.Hours())
}

这是可执行的Go代码:http://play.golang.org/p/1MV6wnLVKh

英文:

How can I calculate the number of days between two dates? In the code below I should get the number of hours, which means that I should only need to divide by 24. However, the result I get is something like -44929.000000. I'm only looking a day or two back so I would expect 24 or 48 hours.

package main

import (
	"fmt"
	"time"
)

func main() {
	timeFormat := "2006-01-02"

	t, _ := time.Parse(timeFormat, "2014-12-28")
	fmt.Println(t)
	//	duration := time.Since(t)
	duration := time.Now().Sub(t)
	fmt.Printf("%f", duration.Hours())
}

Here's the executable Go code: http://play.golang.org/p/1MV6wnLVKh

答案1

得分: 20

你的程序似乎按预期工作。我得到了45.55小时。你尝试在本地运行过吗?

Playground的时间是固定的,time.Now()将始终给出2009-11-10 23:00:00 +0000 UTC。

英文:

Your program seems to work as intended. I'm getting 45.55 hours. Have you tried to run it locally?

Playground time is fixed, time.Now() will give you 2009-11-10 23:00:00 +0000 UTC always.

答案2

得分: 13

package main

import (
	"fmt"
	"time"
)

func main() {

    date := time.Now()
	fmt.Println(date)

    format := "2006-01-02 15:04:05"
	then,_ := time.Parse(format, "2007-09-18 11:58:06")
	fmt.Println(then)

    diff := date.Sub(then)

    //func Since(t Time) Duration
    //Since returns the time elapsed since t. 
    //It is shorthand for time.Now().Sub(t).
	
	fmt.Println(diff.Hours())// 小时数
	fmt.Println(diff.Nanoseconds())// 纳秒数
	fmt.Println(diff.Minutes())// 分钟数
	fmt.Println(diff.Seconds())// 秒数
	
	fmt.Println(int(diff.Hours()/24))// 天数    

}

这是一个Go语言的代码示例,它演示了如何计算两个时间之间的差异。代码中使用了time包来处理时间相关的操作。首先,获取当前时间并打印输出。然后,定义一个时间格式,并使用time.Parse函数将一个指定格式的时间字符串解析为时间对象。接下来,计算当前时间与指定时间之间的差异,并打印输出差异的小时数、纳秒数、分钟数、秒数以及天数。

你可以在这里运行这段代码:https://play.golang.org/p/Vbhh1cBKnh

英文:
package main

import (
	"fmt"
	"time"
)

func main() {

    date := time.Now()
	fmt.Println(date)

    format := "2006-01-02 15:04:05"
	then,_ := time.Parse(format, "2007-09-18 11:58:06")
	fmt.Println(then)

    diff := date.Sub(then)

    //func Since(t Time) Duration
    //Since returns the time elapsed since t. 
    //It is shorthand for time.Now().Sub(t).
	
	fmt.Println(diff.Hours())// number of Hours
	fmt.Println(diff.Nanoseconds())// number of Nanoseconds
	fmt.Println(diff.Minutes())// number of Minutes
	fmt.Println(diff.Seconds())// number of Seconds
	
	fmt.Println(int(diff.Hours()/24))// number of days    

}

Here is the running code https://play.golang.org/p/Vbhh1cBKnh

答案3

得分: 2

下面的代码列出了从起始日期到结束日期之间的所有日期,并显示了日期之间的天数:

to := time.Now()
from := to.AddDate(0, -1, 0)
fmt.Println("toDate", to)
fmt.Println("fromDate", from)
days := to.Sub(from) / (24 * time.Hour)
fmt.Println("days", int(days))
noofdays := int(days)

for i := 0; i <= noofdays; i++ {
    fmt.Println(from.AddDate(0, 0, i))
}

你可以点击以下链接查看代码在Go PlayGround上的演示:https://play.golang.org/p/MBThBpTqjdz

英文:

> the below code gives the list of all the days along with the number of days between the from date and to date:
you can click on the link for the code in
Go PlayGround:https://play.golang.org/p/MBThBpTqjdz

to := time.Now()
from := to.AddDate(0, -1, 0)
fmt.Println(&quot;toDate&quot;, to)
fmt.Println(&quot;fromDate&quot;, from)
days := to.Sub(from) / (24 * time.Hour)
fmt.Println(&quot;days&quot;, int(days))
noofdays := int(days)

for i := 0; i &lt;= noofdays; i++ {
	fmt.Println(from.AddDate(0, 0, i))
}

答案4

得分: 2

使用timeOne.Sub(timeTwo).Hours() / 24这种技术时需要注意一个问题,即夏令时可能导致一天只有23个小时,从而稍微影响到这个计算结果。

英文:

One caveat to be mindful of when using this technique of timeOne.Sub(timeTwo).Hours() / 24 is that daylights savings can cause a day to contain only 23 hours, throwing this calculation off slightly.

答案5

得分: 0

快乐的程序员节

package main

import (
    "fmt"
    "time"
)    

func main() {
    loc, _ := time.LoadLocation("UTC")
    now  := time.Now().In(loc)
    firstDate := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, loc)
    diff := now.Sub(firstDate)
    
    fmt.Printf("今天%s与%s之间的差距是%d天\n", now.String(), firstDate.String(),  int(diff.Hours()/24)+1)


    // Just a joke
    if ( int(diff.Hours()/24)+1 == 256 ) {
        fmt.Printf("¡快乐的程序员节!") 
    } else {
        fmt.Printf("在我的电脑上它可以运行...!?") 
    }
}
英文:

Happy programmer's day

package main

import (
    &quot;fmt&quot;
    &quot;time&quot;
)    

func main() {
    loc, _ := time.LoadLocation(&quot;UTC&quot;)
    now  := time.Now().In(loc)
    firstDate := time.Date(now.Year(), 1, 1, 0, 0, 0, 0, loc)
    diff := now.Sub(firstDate)
    
    fmt.Printf(&quot;The difference between %s and today %s es %d days\n&quot;, now.String(), firstDate.String(),  int(diff.Hours()/24)+1)


    // Just a joke
    if ( int(diff.Hours()/24)+1 == 256 ) {
        fmt.Printf(&quot;&#161;Happy programmer&#39;s day!&quot;) 
    } else {
        fmt.Printf(&quot;On my computer it works...!?&quot;) 
    }
}

huangapple
  • 本文由 发表于 2014年12月30日 05:30:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/27696309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定