Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone


Please how can i change date format from mm/dd/yyyy to dd/mm/yyyy, i have tried these:

C#
DateTime res = DateTime.Parse( value, CultureInfo.GetCultureInfo( "en-gb") );


and

C#
IFormatProvider culture = new CultureInfo( "en-gb", true );
                                            DateTime    date = DateTime.Parse( value , culture );


But still could not get it done.

the variable value is a string variable that contains date in this format: dd mm yyyy, i want to be able to display it in that format, but what is in date and res variables is in this format: mm/dd/yyyy. is there anyway i can still display it in this format: dd mm yyyy? Thanks in advance.
Posted

Try this:
C#
using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        string value = "25 06 2014";
        string format = "dd mm yyyy";
        DateTime result;
        if (DateTime.TryParseExact(value,format, CultureInfo.InvariantCulture, DateTimeStyles.None, out result)){

            Console.WriteLine(result.ToString(format));
        } else {
            Console.WriteLine("Date invalid");
        }
    }
}
 
Share this answer
 
v2
Comments
Uwakpeter 25-Jun-14 8:56am    
I have tried this, it is reporting cannot convert source type string to target type System.DateTime while trying to assign result.ToString(format) to a dateTime property of a class file.

cogCardAttr.TransactionDate =result.ToString( format );
Use ParseExact instead:
C#
string value = "03 02 2014";
DateTime res = DateTime.ParseExact(value, "dd MM yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(res.ToString("MM/dd/yyyy"));
 
Share this answer
 
Comments
Uwakpeter 25-Jun-14 8:59am    
I have tried this, it is reporting cannot convert source type string to target type System.DateTime while trying to assign result.ToString(format) to a dateTime property of a class file.
and what is in res is: mm/dd/yyyy
cogCardAttr.TransactionDate =result.ToString( format );
Further to (the correct) Solutions 1 and 2 a key point to note
Quote:
what is in date and res variables is in this format: mm/dd/yyyy
is not the case.

date and res are of type DateTime so there is no concept of "format", if you have omitted any formatting when displaying them as text, or when looking at the values in the Locals window you will see the values displayed in the default culture for your machine - US format by the look of it
 
Share this answer
 
Comments
Uwakpeter 25-Jun-14 9:08am    
i am not displaying it, i am assigning it to property of a class file, whose type was datetime, so i have this error: cannot convert source type string to target type System.DateTime
CHill60 25-Jun-14 12:41pm    
How are you assigning the value of date (or res) to the property. They are both DateTime so would not produce the error you have quoted.
Uwakpeter 25-Jun-14 13:17pm    
i am assigning res, it is in this format: mm/dd/yyyy, instead of dd/mm/ yyyy
Hi,
If you want to change in entire solution...Use this code inside system.web of web.config file.Your date format will change for British english.Similarly,use en-US for American english..


XML
<system.web>
<globalization culture="en-GB" uiculture="en-GB" />
</system.web>
 
Share this answer
 
v2
Comments
Uwakpeter 25-Jun-14 9:04am    
I am using C# Windows application, i have added your code to the app.config file, it is reporting uiculture is not allowed!
[no name] 25-Jun-14 9:13am    
Hi peter,
Look this http://msdn.microsoft.com/en-in/library/windows/apps/windows.globalization.aspx It may help you...

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