Click here to Skip to main content
15,896,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i have visual studio winform c#
i want to compare the time from DateTime in hh:mm format in a specific range.
but it is not working

in my source code below the range is [start_time ,End_time].

In other words, from the datetime i want to extract the time only
e.g: 2015-04-05 2:14:00 extract only 2:14:00
and compare it with a specific range that has also the same format.

What I have tried:

C#
DateTime current_time = DateTime.Now;
var End_time=current_time.ToString("hh:mm"); //End_time
var start_time = current_time.AddHours(-2).ToString("hh:mm");//start_time 

DataTable dchild= new DataTable();
string query=SELECT DATE_FORMAT( date_created,  '%h:%i:%s' ) time_created from table ;
adap_child = new MySqlDataAdapter(query, strconnection);
            adap_child.Fill(dchild);   
  
foreach (DataRow items in dchild.Rows)
{
  var  date_to_be_compared = (int)items["time_created"];
if(time_to_compared >=start_time && date_to_be_compared<=End_time)//error appears here
   {
     //code
    }
else
{
//code
}

}
Posted
Updated 7-Apr-16 6:44am
v2
Comments
Richard MacCutchan 7-Apr-16 9:03am    
Why are you converting the DateTime values to strings? Just compare them using the numeric values.
Armel_Djient 7-Apr-16 9:55am    
because i want to fetch only the time from DateTime. Is there another way to fetch only time without converting the DateTime values to string?
PIEBALDconsult 7-Apr-16 9:57am    
Yes, as Solution 1 states.
PIEBALDconsult 7-Apr-16 9:56am    
Please don't do that.

Change what you get back from the SELECT to be the DATETIME value that is actually stored in the DB. Then get the TimeOfDay part and use that everywhere else!

Try something like:
C#
DateTime current_time = DateTime.Now;
TimeSpan End_time=current_time.TimeOfDay; //End_time
TimeSpan start_time = End_time.SubtractHours(-2);//start_time 

DataTable dchild= new DataTable();
string query = "SELECT date_created FROM table";
adap_child = new MySqlDataAdapter(query, strconnection);
adap_child.Fill(dchild);   
  
foreach (DataRow items in dchild.Rows)
{
  TimeSpan time_to_be_compared = ((DateTime)items["date_created"]).TimeOfDay;
  if (start_time <= time_to_be_compared && time_to_be_compared <= End_time)
  {
     //code
  }
  else
  {
     //code
  }
}
 
Share this answer
 
v2
You can get the time as a TimeSpan object

C#
DateTime current_time = DateTime.Now;
DateTime date_to_be_compared = DateTime.ParseExact("12 Aug 2014 13:45:22", "dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);

TimeSpan ts_current = current_time.TimeOfDay;
TimeSpan ts_compare = date_to_be_compared.TimeOfDay;

if (ts_current > ts_compare)
{
    System.Diagnostics.Debug.WriteLine("Current time is after compare time");
}
else
{
    System.Diagnostics.Debug.WriteLine("Current time is not after compare time");
}
 
Share this answer
 
Comments
Armel_Djient 7-Apr-16 10:07am    
thanks for your reply. the problem in your source code is :
DateTime date_to_be_compared = DateTime.ParseExact("12 Aug 2014 13:45:22", "dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);

the date_to_be_compared that you parse is static "12 Aug 2014 13:45:22" .
But mine is not.
in my source code, the date_to_be_compared is fetched from the datatable in this format "hh:mm"
PIEBALDconsult 7-Apr-16 10:11am    
Dates should _never_ be stored as strings in databases. Please check the datatype.
F-ES Sitecore 7-Apr-16 10:44am    
That's just sample code, make date_to_be_compared the datetime you actually have.
Armel_Djient 7-Apr-16 10:37am    
in the database the date is stored in format of DATETIME.(eg: 2015-11-16 10:45:48)
now through a sql command i fetched only the time from this datetime, ie 10:45:48
which represents the date_to_be compared or the time_to_be_compared

As you can imagine i have more than one row in my database. that's why i fetched them and put in the datatable.

In your source code u know in advance what is date_to_be_compared that's why you wrote
DateTime date_to_be_compared = DateTime.ParseExact("12 Aug 2014 13:45:22", "dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);

if i modified your code and write something like that, i will get error
foreach (DataRow items in dchild.Rows)
{
DateTime date_to_be_compared = DateTime.ParseExact(items["time_created"], "dd MMM yyyy HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
}

where items["time_created"] represents for example the first row's value 10:45:48

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900