Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i save the time in dd/mm/yyyy (for UK) and mm/dd/yyyy (for US) formats in variables apart from the system time settings? I'm taking the data from a DateTimePicker control. How can i achieve this? Can i have a piece of code?
Posted

I am not too sure what you are asking. You can save a date in DatTime format, and then can convert to string in the right format by setitng the culture of the current thread before doing a ToShortDate:

C#
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Console.WriteLine("Displaying short date for {0} culture:",
                       Thread.CurrentThread.CurrentCulture.Name);
Console.WriteLine("   {0} (Short Date String)",
                       dateToDisplay.ToShortDateString());


The culture for UK is "en-UK" (english UK). The above will print out in US ("en-US")
 
Share this answer
 
Time does not have format. It is represented by the structure System.DateTime without any strings, in a binary form, exactly as it should be. You should understand how to work with data, not with strings representing data. If you need remember some point of time, you should store this time as System.DateTime. You should use date-time formats only when you need to present time on screen in text form, which is, strictly speaking, is not a must: you could show some kind of calendar or purely graphical watch. Only when presented on screen (or in similar situations), this data should be presented in the culture-dependent manner.

Further steps depend on what do you mean by "save". If you mean saving in some persistent storage like a file or a database, you will need some persistent presentation, but this has nothing to do with what you show on screen and hence with date-time formats. It should be saved in some culture-independent format, such as RFC1123, sortable or universal sortable patterns, please see:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^].

Normally, you do it not "manually", but using one or another data-agnostic serializer, which takes care about presentation detail for you. On approach I would recommend is based on Data Contract. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

—SA
 
Share this answer
 
Comments
Shahin Khorshidnia 9-Jun-12 2:42am    
Yes SA. I think it's a perfect answer.
+5
Sergey Alexandrovich Kryukov 9-Jun-12 22:44pm    
Thank you very much, Shahin.
--SA
VJ Reddy 9-Jun-12 3:17am    
Nice answer. 5!
Sergey Alexandrovich Kryukov 9-Jun-12 22:44pm    
Thank you, VJ.
--SA
Maciej Los 21-Jun-12 13:04pm    
Good answer, +5!
The alternate solution is, you can use String or Integer values.

Strings can be stored as a string and when you want to consube/cast date you can convert string to Date.

Every Date has an integer value representation in various form.

DateTime.Now.Ticks : Gives total ticks in nenoseconds for specified date from beginning, and a single tick represent 10000 miliseconds. It gives ticks from January 1, 0001.

DateTime.Now.ToBinary() : Returns date value in Binary format, you can store this in other variable and from this binary value you can retrive date at any time.

There are few more ways but I think this is enough for you now. Refer http://msdn.microsoft.com/en-us/library/03ybds8y[^] for more details on DateTime structure.

Thanks
Rushikesh Joshi
 
Share this answer
 
You can use if you do not want use Format datetime in C# support
C#
 public static void FormatDateTimeUS()
{           
       CultureInfo culture = new CultureInfo("en-US", true);
       DateTimeFormatInfo dfi = (DateTimeFormatInfo)culture.DateTimeFormat.Clone();

       dfi.ShortDatePattern = "MM/dd/yyyy";
       dfi.LongDatePattern = "MM/dd/yyyy";
       dfi.FullDateTimePattern = "MM/dd/yyyy";

       culture.DateTimeFormat = dfi;
       Application.CurrentCulture = culture;          
}
 
Share this answer
 
v2
Comments
phantomlakshan 8-Jun-12 22:22pm    
How can i integrate this in to a datetimepicker?
violetqa 8-Jun-12 22:25pm    
FormatDateTimeUS();
DateTime date=Convert.ToDateTime(DateTimePicker.Values);
Shahin Khorshidnia 9-Jun-12 2:44am    
Sorry about editing your solution (format of the code). But you could do it yourself ;)

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