Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何比较表示日期时间的字符串?_C#_Sql Server_Datetime - Fatal编程技术网

C# 如何比较表示日期时间的字符串?

C# 如何比较表示日期时间的字符串?,c#,sql-server,datetime,C#,Sql Server,Datetime,我有两个包含datetime的字符串,我想检查第一个字符串datetime是否小于第二个字符串,如果已经尝试了字符串,该怎么办 将datetime值从字符串解析为datetime 比较两个日期时间值以了解哪一个更大 解析: 使用DateTime.Parse()方法来解析DateTime string dateInput = "Jan 1, 2009"; DateTime parsedDate = DateTime.Parse(dateInput); Console.WriteLine(pa

我有两个包含datetime的字符串,我想检查第一个字符串datetime是否小于第二个字符串,如果已经尝试了字符串,该怎么办

  • 将datetime值从字符串解析为datetime
  • 比较两个日期时间值以了解哪一个更大
  • 解析: 使用DateTime.Parse()方法来解析DateTime

     string dateInput = "Jan 1, 2009";
     DateTime parsedDate = DateTime.Parse(dateInput);
     Console.WriteLine(parsedDate);
     // Displays the following output on a system whose culture is en-US:
     // 1/1/2009 12:00:00 AM
    
    请参阅DateTime.Parse()。如果知道字符串模式符合指定的模式,也可以使用DateTime.ParseExact()

    比较:使用DateTime.Compare()比较两个DateTime值

    有关Datetime.Compare()的信息,请参阅此文档

    所以实际的代码会变成这样:

    using System;
    
    public class Example
    {
       public static void Main()
       {    
        string d1 = "Jan 1, 2009";
        string d2 = "Feb 2, 2008";
        DateTime date1 = DateTime.Parse(d1);
        DateTime date2 = DateTime.Parse(d2);
        int result = DateTime.Compare(date1, date2);
        string relationship;
    
        if (result < 0)
         relationship = "is earlier than";
        else if (result == 0)
         relationship = "is the same time as";
        else
         relationship = "is later than";
    
        Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
       }
    } 
    
    使用系统;
    公开课范例
    {
    公共静态void Main()
    {    
    字符串d1=“2009年1月1日”;
    string d2=“2008年2月2日”;
    DateTime date1=DateTime.Parse(d1);
    DateTime date2=DateTime.Parse(d2);
    int result=DateTime.Compare(date1,date2);
    字符串关系;
    如果(结果<0)
    relationship=“早于”;
    否则如果(结果==0)
    relationship=“与同时”;
    其他的
    relationship=“晚于”;
    WriteLine({0}{1}{2}),date1,relationship,date2);
    }
    } 
    

    其中date1和date2必须是datetime变量

    if(datetime.Parse(firstString)
    当您说“小于”时,是指字符串还是
    datetime
    值?如果是前者,你就比较它们。如果是后者,则转换它们,然后比较它们。提示:不建议将非字符串数据存储为字符串。通常,不应在字符串中存储日期或时间。如果你必须这样比较,先比较,然后比较。这就是说,如果日期存储在中,您可以使用快捷方式,只需比较字符串就可以了。我们的代码库中绝对存在
    DateTime
    错误,几乎所有这些错误都是由于在比较过程中将它们作为字符串处理而产生的。一个常见的问题是忘记毫秒,然后不作为“短日期”进行解析,并想知道为什么相同的日期(日期时间)不相同。DateTimes作为字符串是自诱导的。
    int result = date1.CompareTo(date2);