Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hell Sir,
I want to compare two date. I am picking date from comobobox (cboDate, cboMonth, cboYear). and save it into gridview. before save i am checking this date to previous save date in gridview.

Means I am comparing two date in which one is in gridview and another one in combobox (cboDate, cboMonth, cboYear). but i am getting error for comparing dates. i am using DateTime.Compare method.

When i am taking date from combobox its only take date but from gridview its taking date and time.

C#
string doj = Convert.ToString(cboFromdate.SelectedItem.Text) + Convert.ToString(cboFromMonth.SelectedItem.Value) + Convert.ToString(cboFromYear.SelectedItem.Text);

DataTable dtTemp = new DataTable();
      dtTemp = (DataTable)Session["PreEmploymentDetail"];
      if (dtTemp != null)
      {
          for (int i = 0; i <= dtTemp.Rows.Count - 1; i++)
          {
             int result = DateTime.Compare(Convert.ToDateTime(doj), Convert.ToDateTime(dtTemp.Rows[i] ["ToDate"]));
             if(result < 0)
             {

             }



Please any one help me....


Thanks
Posted
Updated 5-Aug-12 20:58pm
v3
Comments
Rajesh Buddaraju 6-Aug-12 2:22am    
Let us know what error you are getting.
ritesh88sharma 6-Aug-12 2:30am    
When i am taking date from combobox its only take date but from gridview its taking date and time.
Sangramsingh Pawar 6-Aug-12 2:23am    
Which error or exception you got there?
ritesh88sharma 6-Aug-12 2:33am    
When i am taking date from combobox its only take date but from gridview its taking date and time.
AshishChaudha 6-Aug-12 2:24am    
Please check the date format before comparing....

Try following way:
C#
//your date from comobobox, i.e. cboMonth + cboDate + cboYear
DateTime d1 = ....
//your date from gridview
DateTime d2 = Convert.ToDateTime(GridView.rows(0).cells(1).text.ToString().Trim()).Date; 
if (d1 >= d2)
{
            MessageBox.Show("dt1 >= dt2");
}
else
{
            MessageBox.Show("dt1 < dt2");
}


You can read here about DateTime.Compare Method and see other example:
http://msdn.microsoft.com/en-us/library/system.datetime.compare.aspx[^]
 
Share this answer
 
Use Convert.ToDateTime function to convert the combobox string to DateTime.
Refer the links below:
MSDN : Convert.ToDateTime Method [^]
MSDN : Convert.ToDateTime Method (String, IFormatProvider)[^]
MSDN : Convert.ToDateTime Method (String)[^]



--Amit
 
Share this answer
 
You have to convert both date in perticular format like "MM/dd/yyyy"
or "dd/MM/yyyy" then apply your logic to compare date which are in same format

means you should do folowing steps
1 convert both date in same date format you can convert this using
ToString("your date format")
2 if you want use DateTime.Compare() then convert both date in DateTime.


C#
//Correct this
//Convert This date to "MM/dd/yyyy" format
string doj = Convert.ToString(cboFromMonth.SelectedItem.Value) + Convert.ToString(cboFromdate.SelectedItem.Text)
+ Convert.ToString(cboFromYear.SelectedItem.Text);


//Add This 
DateTime cmb_date=Convert.ToDateTime(doj)
DataTable dtTemp = new DataTable();
      dtTemp = (DataTable)Session["PreEmploymentDetail"];
      if (dtTemp != null)
      {
          for (int i = 0; i <= dtTemp.Rows.Count - 1; i++)
          {
           //Correct this
           //Convert gridview date to "MM/dd/yyyy" format then compare both date 
             int result = DateTime.Compare(Convert.ToDateTime(doj), Convert.ToDateTime(dtTemp.Rows[i] ["ToDate"]));
             if(result < 0)
             {

             }
 
Share this answer
 
v2
Comments
ritesh88sharma 6-Aug-12 3:20am    
Sir, How to convert gridview date into "MM/dd/yyyy" formate.
Please tell me...
Sangramsingh Pawar 6-Aug-12 3:32am    
What is current date format of your grid view date?
ritesh88sharma 6-Aug-12 3:46am    
4/12/2010 12:00:00 AM
(MM/dd/yyyy)
It take both date and time. and i want only date.
Sangramsingh Pawar 6-Aug-12 4:14am    
if your gridview date is in "MM/dd/yyyy" format then no need to convert date. use above code as it, tell me what happens
ritesh88sharma 6-Aug-12 7:34am    
sir i am trying same code but it is giving error... incorrect date format

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