Click here to Skip to main content
15,923,006 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to develop a software project, i want my whole project to use the same date format no matter what system date format is even if i use DateTime.Now. Help me please give suggestion or solution anything.

What I have tried:

String.Format("{0:dd-MM-yyyy}", dt);

i use this but i don't want to write this code everytime i use date
Posted
Updated 25-Jul-17 4:59am
v2
Comments
PIEBALDconsult 25-Jul-17 11:02am    
Please don't, the user will expect your application to use the format he has specified via the Operating System and may be quite upset if your application doesn't honor that choice -- I know I would be.
Bear in mind also, that the International Standard is ISO 8601 -- YYYY-MM-DD
varuncodee 6-Aug-17 11:54am    
At first they were using DD-MM-YYYY format so all date store in database in same format and after sometime somebody by mistake changed system date format to MM/DD/YYYY so database contain to different type of format. so they are facing problem when to type of date show in crystal report or when they search some record by date it won't give result after date is changed.
PIEBALDconsult 6-Aug-17 12:05pm    
I sure hope you are not storing dates as strings. If you are; you're screwed.
BillWoodruff 25-Jul-17 14:18pm    
Why do you want to control the format ? Are you expecting multiple users in different cultures ?

You don't really have a choice: a DateTime value doesn't have a format at all, it's stored as a number of ticks since a fixed point in the past. It only gets a format when you decide to convert it to a string for display. And if you don't specify a format it uses the current PC culture.

You could set the default culture for your application:
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-GB");
And that would force the default format to match that culture, but ... that would also affect the default culture used for parsing user input dates so it's probably a bad idea.
 
Share this answer
 
When you ask .net to convert a date to a string without giving it a format it uses the ShortDatePattern, so you'd have to change this either by changing the OS settings (which I'm guessing you can't do) or by doing it in code;

asp.net - How to change the data format for the current culture so that it applies to the entire web application - Stack Overflow[^]

This is fairly bad practice though, you're better using explicit formats that you get from a config setting\helper function\extension method\whatever even if it does mean more code.

public static class MyExtensions
{
    public static string ToCustomDate(this DateTime dt)
    {
        return dt.ToString("dd-MM-yyyy");
    }
}


DateTime.Now.ToCustomDate()
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 26-Jul-17 0:29am    
5
Hi
During login you call below function it will help you.

C#
public string NewDateFormat(string InputDate, string Format)
{
    var CurrentDate = DateTime.Parse(InputDate);
    return CurrentDate.ToString(Format);
}
 
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