检查是否过了3天 [英] Checking if 3 days have passed

查看:58
本文介绍了检查是否过了3天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试比较两个日期,看它是否已经过了3天。我正在使用UTC时间。由于某种原因,它不起作用,我继续刮我的头为什么。



我尝试过:



 DateTime time = DateTime.Parse(6/20/2018 4:38:26 AM); 





 if(DateTime.UtcNow> time.AddDays(3))





如果有人可以用if语句帮助我,那就太棒了!

解决方案

也许试试这个:

 if(DateTime.Compare(DateTime.UtcNow,time.AddDays(3))> 0)


比较看起来很好,并且在我的机器上表现得如预期的那样。



对日期的解释可能是问题所在。它根据您计算机的日期设置解析日期。在我的电脑上(荷兰语,nl-NL)它会引发异常,除非我要么:



- 更改格式:

 DateTime time = DateTime.Parse(20-06-2018 04:38:26); 





- 设置线程文化到en-US:

 Thread.CurrentThread.CurrentCulture = new CultureInfo(en-US); 


Hey, i'm trying to compare 2 dates to see if it has been 3 days. I'm using UTC time for this. For some reason it is not working and I am continuing to scratch my head why.

What I have tried:

DateTime time = DateTime.Parse("6/20/2018 4:38:26 AM");



if (DateTime.UtcNow > time.AddDays(3))



If someone could help me out with that if statement, that would be great!

解决方案

Maybe try this:

if (DateTime.Compare(DateTime.UtcNow, time.AddDays(3)) > 0)


The comparison looks fine and behaves as expected on my machine.

The interpretation of the date could be the problem. It parses the date based on the date settings of your computer. On my computer (Dutch, nl-NL) it throws an exception unless I either:

- Change the format:

DateTime time = DateTime.Parse("20-06-2018 04:38:26");



- Set the thread culture to en-US:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");


这篇关于检查是否过了3天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆