比较日期大于C# [英] Compare Date is Greater than in C#

查看:72
本文介绍了比较日期大于C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查传递的变量日期是否大于我包含在代码中并且当前尝试使用以下代码的静态日期,

I'm trying to check if the passing variable date is greater than a static date which I have included in the code and currently trying to use the following code,

private String LastPayDate {
  get { 
    string foo;

    if(Parameters.TryGetValue("Last Pay Date", out foo))
      return foo;
    else 
      return null; 
  } 
}

private Boolean IsLastPay() {
  if (!string.IsNullOrEmpty(LastPayDate)) {
    if(DateTime.Parse(Parameters.TryGetValue("Last Pay Date") >="24/05/2018")
       return true;
    else
      return false;
  } 

  return false;
}   

但是我唯一遇到的错误是在下面的代码部分中,

however the only error I get is within below code section,

if(DateTime.Parse(Parameters.TryGetValue("Last Pay Date")>"24/05/2018")

任何人都可以帮忙吗?

推荐答案

如果要比较 DateTime s,请比较它们,而不要比较 string s:

If you want to compare DateTimes, compare them, but not strings:

//TODO: what is the magic number (date) 24 May 2018?
private Boolean IsLastPay() {
  if (Parameters.TryGetValue("Last Pay Date", out var dateSt))
    if (DateTime.TryParse(dateSt, out var paramDate))
      return paramDate >= new DateTime(2018, 5, 24);
    else
      return false; // DateTime.TryParse failed to parse the parameter
  else
    return false;   // Parameters.TryGetValue failed to get the value
}

这篇关于比较日期大于C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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