Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have Date time input field from where I am taking date and converting it to another format, my code for that
C#
try
  {
      DateTime dt = dtiFrom.Value.Date;
      string format = "DD-MM-YYYY";            // Use this format
      MessageBox.Show(dt.ToString(format));   // here its shows result as DD-10-YYYY
      DateTime dt1 = Convert.ToDateTime(dt.ToString(format)); // here Error "The string was not recognized as a valid DateTime. There is an unknown word starting at index 0."
      }
      catch (Exception ee)
      {
       MessageBox.Show(ee.Message, "Error Message!");
       }


I am not able to convert date according to my format. please could any body help me in code or suggest me some code. thanks in advance
Posted
Updated 29-Oct-13 23:12pm
v2
Comments
Thanks7872 30-Oct-13 5:37am    
What you want to do? I mean is it ok if the final output is string?

There are multiple ways.See following code in vb.net
C#
Shared Function ConvertDateFormatForPrint(ByVal dtVal As Object) As Object
       Return Date.Parse(dtVal).ToString("dd/MM/yyyy")
End Function
 
Share this answer
 
v2
1. Your format is incorrect. Change it to "dd-MM-yyyy"
2. Why do you want conversion of dt to the desired format than back to the original format? You already have both "MM-dd-yyyy" format date as well as "dd-MM-yyyy" format date.

C#
DateTime dt = System.DateTime.Now;
string format = "dd-MM-yyyy";           
string newDate = dt.ToString(format);   
 
Share this answer
 
This works for me. Try this.

C#
lbltime.Text = System.DateTime.Now.ToShortDateString();
        string d1 = DateTime.Parse(lbltime.Text).ToString("yyyy-MM-dd");
 
Share this answer
 
Comments
Abdul Saeed khan 30-Oct-13 5:40am    
by using this code
string dt1 = System.DateTime.Now.ToShortDateString();
string dd = DateTime.Parse(dt1).ToString("DD-MM-YYYY");
MessageBox.Show(dd);
it gives me same out put as "DD-10-YYYY"
Jignesh Khant 30-Oct-13 5:46am    
Change your date format to this:
dd-MM-yyyy
Jignesh Khant 30-Oct-13 5:43am    
That is because your format is wrong. Dont use "DD-MM-YYYY", use "dd-MM-yyyy". Change your format and this code will work.
Abdul Saeed khan 30-Oct-13 6:15am    
Thanks that worked
try
           {
               DateTime dt = DateTime.Now;//dtiFrom.Value.Date;
               string format = "DD-MM-YYYY";            // Use this format
              // MessageBox.Show(dt.ToString(format));   // here its shows result as DD-10-YYYY
               MessageBox.Show(dt.ToString("dd-MM-yyyy"));
               DateTime dt1 = Convert.ToDateTime(dt.ToString("MM/dd/yyy"));
           }
           catch (Exception ee)
           {
               MessageBox.Show(ee.Message, "Error Message!");
           }
 
Share this answer
 
v2
Your format should be as follows:

string format = "dd-MM-yyyy";
Casing is important with string formatting, you may think it strange that the month uses upper-case, but this is because lower case m and mm is used to represent minutes.

Note that the reason your output displays DD and YYYY is because any character that is not reserved as a format character will be outputted with no change. Uppercase D and Y are not reserved which is why they display in the output, just as - remains unchanged.

If you wish to output reserved format characters then you can escape them using \.

See here for a full list of date and time format values http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^]
 
Share this answer
 

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