Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 17:00pm
Rate this: bad
good
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:",
                       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")
  Permalink  
Rate this: bad
good
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:
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
  Permalink  
Comments
Shahin Khorshidnia at 9-Jun-12 2:42am
   
Yes SA. I think it's a perfect answer.
+5
SAKryukov at 9-Jun-12 22:44pm
   
Thank you very much, Shahin.
--SA
VJ Reddy at 9-Jun-12 3:17am
   
Nice answer. 5!
SAKryukov at 9-Jun-12 22:44pm
   
Thank you, VJ.
--SA
losmac at 21-Jun-12 13:04pm
   
Good answer, +5!
Sergey Alexandrovich Kryukov at 29-Jun-12 21:00pm
   
Thank you, Maciej.
--SA
Rate this: bad
good
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 http://msdn.microsoft.com/en-us/library/03ybds8y[^] for more details on DateTime structure.
 
Thanks
Rushikesh Joshi
  Permalink  
Rate this: bad
good
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;          
}
  Permalink  
v2
Comments
phantomlakshan at 8-Jun-12 22:22pm
   
How can i integrate this in to a datetimepicker?
violetqa at 8-Jun-12 22:25pm
   
FormatDateTimeUS();
DateTime date=Convert.ToDateTime(DateTimePicker.Values);
Shahin Khorshidnia at 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
0 DamithSL 320
1 OriginalGriff 275
2 Sergey Alexandrovich Kryukov 187
3 Afzaal Ahmad Zeeshan 154
4 Peter Leow 115
0 OriginalGriff 7,510
1 DamithSL 5,494
2 Sergey Alexandrovich Kryukov 4,954
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 9 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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