Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
convert date from 17-July-2017 to 17-07-2017 in c#

What I have tried:

[removed duplicate content]
Posted
Updated 25-Jul-17 3:50am
v2
Comments
F-ES Sitecore 25-Jul-17 7:11am    
Use a combination of DateTime.TryParse and DateTime.ToString to convert dates\strings. Google both for code samples and implementations. Note that DateTime variables and dates in databases don't have a "format" though, so if changing that is your ultimate goal then it can't be done, instead you need to properly understand how dates are managed.
Tomas Takac 25-Jul-17 7:12am    
Parse the string into a DateTime the format as needed. Where is the problem?

Try this


var n = DateTime.Parse("10-july-2017");
           string m = n.ToString("dd/MM/yyyy");
           MessageBox.Show(m);
 
Share this answer
 
Use DateTime.TryParseExact to convert it to a DateTime value, then use ToString to output the new format:
C#
string input = "17-July-2017";
DateTime dt;
if (!DateTime.TryParseExact(input, "dd-MMMM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
    {
    // Report problem
    ....
    return;
    }
Console.WriteLine(dt.ToString("dd-MM-yyyy"));
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 25-Jul-17 7:32am    
5ed; to bring this solution on top of the page as it requires more visibility.
C#
string shortDate = "ERROR!";
string longDate = "17-July-2017";
DateTime date;
if (DateTime.TryParse(longDate, out date))
    shortDate = date.ToShortDateString();

Console.WriteLine($"Long: {longDate} > Short: {shortDate}");
 
Share this answer
 
 
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