简体   繁体   English

比较日期大于 C#

[英]Compare Date is Greater than in 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") if(DateTime.Parse(Parameters.TryGetValue("Last Pay Date") > "24/05/2018")

can anyone help please ?有人可以帮忙吗?

If you want to compare DateTime s, compare them, but not string s:如果要比较DateTime s,请比较它们,而不是string s:

//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
}

Thank you for respond.谢谢你的回应。 It did helped and I've managed to use below code and its working now, Much appreciate Help!它确实有帮助,我已经设法使用下面的代码并且现在可以正常工作,非常感谢帮助!

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

Why not use the DateTime.Compare() method of DateTime class.为什么不使用 DateTime 类的DateTime.Compare()方法。 For this you need to have both the variables/objects of type DateTime .为此,您需要同时拥有DateTime类型的变量/对象。

string staticDate = "24/05/2018"; //dd-MM-yyyy
string inputDate = "14/08/20"; //dd-MM-yy
string greaterDate = CalculateGreaterDate(inputDate, staticDate); // 14/08/20 is greater  

public static string CalculateGreaterDate(string iDate, string sDate)
{
   // input date
   string input = iDate;
   var inputElements = input.Split('/');
   int inputDay = Convert.ToInt32(inputElements[0]); //14 
   int inputMonth = Convert.ToInt32(inputElements[1]); //08
   int inputYear = Convert.ToInt32(inputElements[2]); //20

   // static date
   string static = sDate;
   var staticElements = static.Split('/');
   int staticDay = Convert.ToInt32(staticElements[0]); //24
   int staticMonth = Convert.ToInt32(staticElements[1]); //05
   int staticYear = Convert.ToInt32(staticElements[2]); //2018

   DateTime inputDate = new DateTime(inputYear, inputMonth, inputDay);
   DateTime staticDate = new DateTime(staticYear, staticMonth, staticDay);

   // DateTime.Compare(d1, d2) returns:
   // > 0 : d1 is greater than d2
   // = 0 : d1 & d2 are equal
   // < 0 : d1 is smaller than d2
   int result = DateTime.Compare(inputDate, staticDate);
   if (result > 0)
      return iDate + " is greater";
   else if (result < 0)
      return sDate + " is greater";
   else if (result == 0)
      return iDate + " is equal to " + sDate;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#可以使用#if来比较小于或大于的值吗? - Can C# use #if to compare a value with less than or greater than? 检查日期大于C#ASP.NET MVC - Check Date is Greater Than C# ASP.NET MVC 如何在C#或Linq中比较大于或小于运算符值的两个字节数组? - How to compare two byte arrays with greater than or less than operator value in C# or Linq? C# 使用 &lt; &gt;(不是大于/小于) - C# use of < > (not as greater than / less than) 在C#中是否有小于和大于的速记? - Is there a shorthand for smaller than AND greater than, in C#? 如何比较两个字符串,以查看一个字符串是否大于另一个字符串(按字母顺序)? C# - How do I compare two strings together to see if one is greater than the other(alphabetically)? c# 在Linqpad c#语句中,如何比较字符串是否大于(版本&gt; 4.0.38000) - In Linqpad c# statement how to compare string with is greater than (Version > 4.0.38000) 阻止用户输入大于C#的起始日期 - Prevent user from entering a from date greater than to date c# C#中的正则表达式大于符号的问题 - Problem with greater than symbol in Regex, C# 将文件从 C# 中的 FTP 服务器下载到修改日期大于指定的本地文件夹 - Download files from FTP server in C# to local folder with modification date greater than specified
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM