Click here to Skip to main content
13,590,229 members
Rate this:
Please Sign up or sign in to vote.
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 8-Jun-12 16:00pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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:

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

The culture for UK is "en-UK" (english UK). The above will print out in US ("en-US")
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

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:[^].

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:[^].

Shahin Khorshidnia 9-Jun-12 2:42am
Yes SA. I think it's a perfect answer.
Thank you very much, Shahin.
VJ Reddy 9-Jun-12 3:17am
Nice answer. 5!
Thank you, VJ.
Maciej Los 21-Jun-12 13:04pm
Good answer, +5!
Sergey Alexandrovich Kryukov 29-Jun-12 21:00pm
Thank you, Maciej.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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[^] for more details on DateTime structure.

Rushikesh Joshi
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can use if you do not want use Format datetime in C# support
 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;          
phantomlakshan 8-Jun-12 22:22pm
How can i integrate this in to a datetimepicker?
violetqa 8-Jun-12 22:25pm
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.180618.1 | Last Updated 9 Jun 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100