Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
When I convert the date to format say "dd/MM/yyy"
C#
String strdate = Convert.ToDateTime(DateTime.Now.ToString()).ToString("dd-MM-yyyy")

I am receiving this error "String is not valid date time" but when I write like below I am not receiving any error. Why is it happening like this?
C#
String strDate = DateTime.ParseExact(Convert.ToDateTime(DateTime.Now.ToString()).ToString("dd-MM-yyyy"),"dd-MM-yyyy",CultureInfo.InvariantCulture).ToString("dd-MM-yyyy")
Posted
Updated 12-Oct-13 2:32am
v4

You coudlt not convert .MM/dd/yyyy format only convert Datetime data type other wise you coudlt not convert,Date Time data type convert only MM/dd/yyyy format only chage that after only you will convert This mistake do all the developer first time

C#
public const string DEFAULT_DATE_FORMAT = "dd/MM/yyyy";
public static string ConvertDtFormat(string dateTimeString)
           {
               dateTimeString = dateTimeString.Trim();
               while (dateTimeString.Contains("  "))
               {
                   dateTimeString = dateTimeString.Replace("  ", " ");
               }

               if (dateTimeString == null || dateTimeString.Length == 0)
               {
                   return string.Empty;
               }
               else
               {
                   DateTime convertedDateTime = new DateTime();
                   string userDateFormat =DEFAULT_DATE_FORMAT; 

                   try
                   {

                       if (userDateFormat.Trim().Contains("dd/MM/yyyy"))
                           convertedDateTime = DateTime.ParseExact(dateTimeString, format_dmy, CultureInfo.InvariantCulture,
                                                                   DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);
                       else if (userDateFormat.Trim().Contains("MM/dd/yyyy"))
                           convertedDateTime = DateTime.ParseExact(dateTimeString, format_mdy, CultureInfo.InvariantCulture,
                                                                   DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);

                       return convertedDateTime.ToString("MMM dd, yyyy hh:mm tt");
                   }
                   catch
                   {
                       return "Invalid DateTime";
                   }
               }
           }
 
Share this answer
 
v3
Comments
surajemo 12-Oct-13 3:49am    
I am Sorry but i am not able to understand what You trying to say
manikjeyam 12-Oct-13 3:54am    
show my new update solution pass you date(dd/mm/yyyy) after you do use datetime data type
surajemo 12-Oct-13 4:24am    
its very big
surajemo 12-Oct-13 4:25am    
what is HMS
manikjeyam 12-Oct-13 5:02am    
you can use this convert Dateformat
This is nonsense to take a DateTime, convert it to string, and convert it back to DateTime.
C#
string strdate = DateTime.Now.ToString("dd-MM-yyyy");

And the inverse:
C#
DateTime myDate = DateTime.ParseExact("29-09-2013", "dd-MM-yyyy", CultureInfo.InvariantCulture);

Always apply the KISS rule (Keep It Simple and Stupid).
 
Share this answer
 
Comments
surajemo 12-Oct-13 3:58am    
I have to intention take a DateTime, convert it to string, and convert it back to DateTime.

My date is coming from Database just for demonstration i added DateTime.Now.ToString("dd-MM-yyyy");
phil.o 12-Oct-13 4:00am    
Why convert it to string and back to DateTime? Use the DateTime value in the first place.
Your intention does not make sense.
surajemo 12-Oct-13 4:05am    
The value which is coming from the database its datatype is varchar(max) so the date is coming in string format "12/10/2013" that is y i am converting it to datetime and then i want to add further days to it also and further calculation on-the dates
phil.o 12-Oct-13 4:09am    
So convert from String to DateTime (ParseExact()), and then do whatever computations you need on Datetime type.
Richard MacCutchan 12-Oct-13 8:48am    
Storing a date in a database as a string is just wrong, and leads to exactly this sort of situation. Use the proper types.
2 line coding
datetime strdate =Convert.ToDateTime(DateTime.Now.ToString()).ToString("dd-MM-yyyy")
string s=strdate.tostring(dd/MM/yyyy)
 
Share this answer
 
Comments
Richard MacCutchan 12-Oct-13 8:48am    
Wrong!

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