Click here to Skip to main content
15,748,748 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to convert string format date into date format using c sharp.

Code as follows;
C#
string[] datecheck;
string dates = "";
datecheck = dates.Split(',');

from the above date in the string format, i am converting string format date into date format for that code as follows;
C#
string[] datecheck;
    string dates = "";
    string[] Coursecheck;
    datecheck = dates.Split(',');
    string dt = Convert.ToDateTime(dates).ToString("ddMMyyyy");
    Coursecheck = PKGMinorCode.Split(',');
    int len = datecheck.Length;
    for (int z = 0; z < len-1; z++)
    {
        for (int j = z + 1; j < len-1 ; j++)
        {
            if (Coursecheck[z] == "ARPA" && Coursecheck[j] == "ROC")
            {

                if (datecheck[z] < datecheck[j])
                {
                   ScriptManager.RegisterStartupScript(this, GetType(), "Invalid date", "alert('ROC Date is greater than the ARPA Date')", true);
                   return;
                }
            }
          }
         }

when i run the above code shows errors as follows;
operator '<' cannot be applied to operands of type string
the above error shows in the below line as follows;
C#
if (datecheck[z] < datecheck[j]).

please help me. what is the problem in my above code.
Regards,
Narasiman P
Posted
Updated 2-Apr-13 21:31pm
v2

try this
C#
if (Convert.ToDatetime(datecheck[z]) < Convert.ToDatetime(datecheck[j]))

Note this will work if you have proper date value in datecheck[z] and datecheck[j]

HappyCoding!
:)
 
Share this answer
 
datecheck[] array type is string. Values stored in it are: '2013-03-29', '2013-03-30' and so on. You can't use ">" operand to compare dates stored in string variable. You need to convert string to date to enable comparison.

Please, read these articles:
DateTime.Parse() method[^]
DateTime.TryParse() method[^]
Convert.ToDateTime() method[^]

C#
if (Convert.ToDateTime(datecheck[z])<convert.todatetime(datecheck[z]))
{
//do something
}
 
Share this answer
 
v2
Comments
Ian A Davidson 3-Apr-13 4:28am    
+5 for Parse, TryParse. I don't know what the "convert" code is supposed to be here - it won't work. Parse, TryParse, ParseExact or TryParseExact are the way to go; the one you use will depend on what you need to do and what the likely inputs will be. You should, if you like, "compare and contrast" those methods of the DateTime class. Regards, Ian.
Maciej Los 3-Apr-13 5:24am    
Thank you, Ian ;)
BTW: I don't see your 5...
There is a static function Compare in class string ,such as :
C#
if (string.Compare(datecheck[z] , datecheck[j])) 
{
   //your code
}
 
Share this answer
 
v3
Comments
[no name] 3-Apr-13 4:12am    
if(Convert.ToDateTime(datecheck[z]) < Convert.ToDateTime(datecheck[j]))
{

}

when i run the above code shows the error message as follows;

Unable to cast object of type 'System.String[]' to type 'System.IConvertible'.
please help me.

what is the problem in my code.

regards,
Narasiman P.

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