检查时间戳是否在两个日期之间。

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

check if timestamp between 2 dates

问题

我在Mongo中存储的日期是时间戳,例如1564444800000000000。我想要检查一个时间戳是否在两个日期之间 - 2021年7月1日和2021年7月31日。我使用https://www.epochconverter.com/将这两个日期转换为时间戳。我首先获取了所有的记录,并在循环遍历记录时进行了如下检查:

cDate := 1564444800000000000
if cDate > 1625167488000 && cDate < 1627759488000 {
    log.Fatal("找到一个!")
}

但是这似乎不起作用,因为我没有找到匹配项。有没有更好的方法来做这个?

英文:

I have dates stored as timestamps in mongo, for example, 1564444800000000000. I want to check if a timestamp is between 2 dates - July 1, 2021 and July, 2021. I converted these 2 dates to timestamps using https://www.epochconverter.com/. I first fetched all records and while looping through the records, I did a check like so

cDate := 1564444800000000000
if  cDate &gt; 1625167488000 &amp;&amp;  cDate &lt;  1627759488000 {
		log.Fatal(&quot;found one!&quot;)
}

But this not seem to work as I get no matches. Is there a better way of doing this?

答案1

得分: 4

我是你的中文翻译助手,以下是翻译好的内容:

我的意思是仅仅看数字:

cDate := 1564444800000000000
               1625167488000
               1627759488000

我不明白为什么一个比它所在范围的数字多六位数的数字会在它们之间。即使我们将 CDate 除以 1000000,结果仍然是:

cDate := 1564444800000
         1625167488000
         1627759488000

它仍然小于这两个数字,所以它仍然不会在它们之间。但是话虽如此,你很可能只需要将 cDate 除以 1000000,而你的例子只是不太好(因为1564444800000是2019年7月29日)。

英文:

I mean just looking at the numbers:

cDate := 1564444800000000000
               1625167488000
               1627759488000

I don't see how one with six more digits than the one's it's supposed to be between would ever be between them. And even if we divided CDate by 1000000 it would be

cDate := 1564444800000
         1625167488000
         1627759488000

and it's still less than both of those, so it still wouldn't be between them. But that being said, you most likely do just need to divide cDate by 1000000 and your example is just not a good one (since 1564444800000 is July 29 2019)

答案2

得分: 1

我建议将其转换为日期,然后使用之前和之后的API进行检查,例如:

package main

import (
	"fmt"
	"time"
)

func main() {
	cDate := int64(1564444800000000000)
	firstJuly := time.Date(2019, 7, 1, 0, 0, 0, 0, time.UTC)
	thrirtyOneJuly := time.Date(2019, 7, 31, 0, 0, 0, 0, time.UTC)

	date := time.Unix(0, cDate)
	// fmt.Println(date) //将打印出 2019-07-30 00:00:00 +0000 UTC
	fmt.Println("是七月吗?", date.After(firstJuly) && date.Before(thrirtyOneJuly))
}

将打印出:

是七月吗? true

在 playground 上查看

英文:

I suggest to transform in a date then check with the before and after API, as example:

package main

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

func main() {
	cDate := int64(1564444800000000000)
	firstJuly := time.Date(2019, 7, 1, 0, 0, 0, 0, time.UTC)
	thrirtyOneJuly := time.Date(2019, 7, 31, 0, 0, 0, 0, time.UTC)

	date := time.Unix(0, cDate)
	// fmt.Println(date) //will print 2019-07-30 00:00:00 +0000 UTC
	fmt.Println(&quot;Is July?&quot;, date.After(firstJuly) &amp;&amp; date.Before(thrirtyOneJuly))
}

will print

Is July? true

See on playground

huangapple
  • 本文由 发表于 2021年12月22日 04:00:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/70441001.html
匿名

发表评论

匿名网友

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

确定