日期比较 - 如何检查假如为20分钟过去了? [英] Date comparison - How to check if 20 minutes have passed?

查看:122
本文介绍了日期比较 - 如何检查假如为20分钟过去了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查是否20分钟从当前日期已过?

例如:

  VAR开始= DateTime.Now;
VAR oldDate =08/10/2011 23点五十分31秒;

    如果(启动?){
     // 20分钟内从开始传递
    }
 

什么是做到这一点的最好方法是什么? 感谢:)

解决方案
  1. 您应该转换的开始时间为UTC时间,说'开始'。
  2. 您可以使用您的开始时间,现在比较当前UTC时间:

    start.AddMinutes(20)> DateTime.UtcNow

这种方法意味着你将得到解决夏令时更改正确的答案。

按加入时间的开始时间,而不是减法和比较的总时间在一个时间跨度,你有一个更可读的语法,你可以处理更多的时间差的情况下,例如: 1个月开始,2周从一开始,...

How to check if 20 minutes have passed from current date?

For example:

var start = DateTime.Now;
var oldDate = "08/10/2011 23:50:31"; 

    if(start ??) {
     //20 minutes were passed from start
    }

what's the best way to do this? Thanks :)

解决方案

  1. You should convert your start time to a UTC time, say 'start'.
  2. You can now compare your start time to the current UTC time using:

    start.AddMinutes(20) > DateTime.UtcNow

This approach means that you will get the correct answer around daylight savings time changes.

By adding time to the start time instead of subtracting and comparing the total time on a TimeSpan you have a more readable syntax AND you can handle more date difference cases, e.g. 1 month from the start, 2 weeks from the start, ...

这篇关于日期比较 - 如何检查假如为20分钟过去了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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