Hi all,
i am working on one app which will compare two different time. According to comparison it will do further task.
i am taking both time in strings like below;
string tym_one = "17:04";
string tym_two = "19:00";
I am not able to compare both strings.
please help me to compare this.
Rishikesh Kumar SinghPosted Feb 26, 2013, 7:10 AM
string tym_one = "17:04";
string tym_two = "19:00";
TimeSpan first = TimeSpan.Parse(tym_one );
TimeSpan second = TimeSpan.Parse(tym_two );
if (first.CompareTo(second)==1)
{
MessageBox.Show("first time is greater");
}
if (first.CompareTo(second) == -1)
{
MessageBox.Show("second time is greater");
}
if (first.CompareTo(second) == 0)
{
MessageBox.Show("both are equal");
}
I have made a small project for this, get it downloaded and use it...
if it helps you then mark it as an accepted answer otherwise let me know..!!
VulpesPosted Feb 26, 2013, 9:09 AM
The output, as expected is :
However, if you might have time strings such as "6:13" or "6:3", then string comparison won't work and you'll need to parse first to TimeSpan (or DateTime) as Rishikesh suggested.