Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I convert 2011-07-24 00:00:00.000 into 07-24-2013(MM-DD-YYYY) in C# code. Please help me
Posted
Updated 17-Oct-13 2:55am
v2

Here samples using C#[^] & SQL Server[^]
 
Share this answer
 
Try to execute below code

C#
DateTime thisDate1 = new DateTime(2011, 6, 10);
Console.WriteLine("Today is " + thisDate1.ToString("MMMM dd, yyyy") + ".");

DateTimeOffset thisDate2 = new DateTimeOffset(2011, 6, 10, 15, 24, 16, 
                                              TimeSpan.Zero);
Console.WriteLine("The current date and time: {0:MM/dd/yy H:mm:ss zzz}", 
                   thisDate2); 
 
Share this answer
 
Following Single line code will help you out . . .
I am Converting TODAY'S date time to short date and short time.

To get current date and time:
C#
DateTime CurrentDateTime = DateTime.Now;

To Convert today's DateTime to short date:
C#
string CurrentDate= CurrentDateTime.ToShortDateString();

To Convert today's DateTime to short time:
C#
string CurrentTime= CurrentDateTime.ToShortTimeString()
 
Share this answer
 
C#
IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
        String datefromtime = txtfromdate.Text.Trim();
        String datetotime = txttodate.Text.Trim();
        DateTime dtf = DateTime.Parse(datefromtime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
        DateTime dtt = DateTime.Parse(datetotime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
 
Share this answer
 
v2
if current date is October 17, 2013
C#
System.DateTime.Now.ToString("MM-dd-yyyy");

the output will be
10-17-2013


In your solution will be as follows
C#
var dt = new DateTime(2011, 7, 24);
string newDt = dt.ToString(("MM-dd-yyyy");

the value of newDt will be

07-24-2011
 
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