Click here to Skip to main content
15,935,728 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
in have string variable with value -- 9/10/2012 8:13:56 PM
its a MM/dd/yyyy hh:mm:ss tt format

But i want to convert it to

dd/MM/yyyy hh:mm:ss tt

so my code-->
C#
string strtime= getTime(Session["id"].ToString());
string format = "dd/MM/yyyy hh:mm:ss tt";
DateTime dt = DateTime.ParseExact(strtime, format, CultureInfo.InvariantCulture);
string str= dt.ToString(dd/MM/yyyy hh:mm:ss tt);



but its not working

exception :

"String was not recognized as a valid DateTime."
Please help!
Posted
Updated 10-Oct-12 1:55am
v3
Comments
[no name] 10-Oct-12 7:39am    
"but its not working ", is completely meaningless and does nothing to describe any kind of a problem.
rohit24c 10-Oct-12 7:42am    
its throughing exception
"String was not recognized as a valid DateTime."
MT_ 10-Oct-12 7:49am    
I think it is throwing exception at DateTime.ParseExact. Check the answer below.

If works if you do the following... of course, you might want to have two distinct infos, one for input and another to output, if you are reading/writing lots of dates.
C#
// Create date time format info
System.Globalization.DateTimeFormatInfo info = 
    new System.Globalization.DateTimeFormatInfo();
            
// Change formats
info.ShortDatePattern = "MM/dd/yyyy";
info.LongTimePattern = "hh:mm:ss tt";

string input = "9/10/2012 8:13:56 PM";

// parse // or try parse if you prefer
DateTime datetime = DateTime.Parse(input, info);

// Change to output format
info.LongDatePattern = "dd/MM/yyyy";

// Get parsed datetime as string
string output = datetime.ToString("dd/MM/yyyy hh:mm:ss tt", info);


Alternatively, if you know the input and output cultures, you can get the formats by calling "someCulture.DateTimeFormat" of the CultureInfo instances.
 
Share this answer
 
Try this...

C#
string strtime = "9/10/2012 8:13:56 PM ";
       // string format = "dd/MM/yyyy hh:mm:ss tt";
        DateTime dt = Convert.ToDateTime(strtime, ci);
        string str = dt.ToString();
        Response.Write(str);


Thanks
 
Share this answer
 
If I understand correctly, the value saved in the session is in MM/dd/yyyy hh:mm:ss tt format.
Hence you need to pass that while trying to do DateTime.ParseExact.


C#
string strtime= getTime(Session["id"].ToString());
string format = "MM/dd/yyyy hh:mm:ss tt";
DateTime dt = DateTime.ParseExact(strtime, format, CultureInfo.InvariantCulture);
string str= dt.ToString(dd/MM/yyyy hh:mm:ss tt);


Hope that helps, if it does, mark it as answer/upvote.
Thanks
Milind
 
Share this answer
 
v2
Comments
MT_ 11-Oct-12 0:56am    
What's wrong with the solution provided? Whoever has downvoted, any reason for doing so? Please update so everyone including myself can learn.

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