0

I have two strings which contains datetime i want to check if first string datetime less then second , how to do if have already try string.compare which not works for me

jazb
  • 5,498
  • 6
  • 37
  • 44
  • 6
    `if (DateTime.Parse(firstString) < DateTime.Parse(secondString))` – itsme86 Jan 04 '19 at 23:39
  • 7
    When you say "less than", do you mean *as a string* or *as a `DateTime` value*? If the former, you compare them. If the latter, you convert them and then compare them. Hint: It's not recommended to store non-string data as strings. – David Jan 04 '19 at 23:40
  • 3
    In general you should not store dates or times inside of strings. If you have to compare them like this, first [convert the string](https://stackoverflow.com/questions/919244/converting-a-string-to-datetime) and then [compare them](https://stackoverflow.com/questions/3059497/how-to-compare-datetime-in-c). That being said, if the dates are stored in a [sortable format](https://stackoverflow.com/questions/17819834/sortable-readable-and-standard-time-format-for-logs), you can take a shortcut and just compare the strings and it should work. – John Wu Jan 04 '19 at 23:42
  • 2
    Our code base was absolutely infested with `DateTime` bugs almost all due to handling them as strings, during comparisons and more. A common problem was forgetting about the milliseconds then not parsing as a "ShortDate" and wondering why the same dates (date-times) were not the same. DateTimes as strings is self induced hell. – radarbob Jan 05 '19 at 00:35

2 Answers2

1

The problem can be divided into 2 parts

  1. Parsing the datetime value from string to datetime
  2. Comparing the two datetime values to understand which one is larger.

Parsing: Use the DateTime.Parse() method to parse the 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

Refer this for DateTime.Parse(). You can also use DateTime.ParseExact() if you know the string pattern confirms to the pattern specified.

Comparing: Use the DateTime.Compare() to compare two datetime values.

Refer this link for Datetime.Compare()

So the actual code would become something like this:

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);
   }
} 
Nic
  • 12,220
  • 20
  • 77
  • 105
rtshmittl
  • 64
  • 1
  • 9
0
int result = date1.CompareTo(date2);

in which date1 and date2 must be datetime variables

Shubham
  • 443
  • 2
  • 10