Reputation: 37
Relatively new Windows Phone developer here, looking for some help. Basically I'm messing around with an app that I'm looking to eventually put on the marketplace.
Basically the app is counting down to a specific date, I've got the countdown working with no problems, however i do have a problem with the date format as I'm in the UK and the date format is dd/MM/yyyy whereas the states is MM/dd/yyyy. So the app goes into negative figures for anyone in the US. Basically i need help with some sort of workaround, whether it's setting a universal date format for my app or something like that. Here is the code for the countdown:
DateTime startDate = DateTime.Now;
var launch = DateTime.Parse("01/08/2012 00:00:00 AM");
TimeSpan t = launch - startDate;
Countdown.Text = string.Format("\r {0}\r Days \r {1}\r Hours \r {2}\r Minutes \r {3}\r Seconds", t.Days, t.Hours, t.Minutes, t.Seconds);
Upvotes: 0
Views: 1102
Reputation: 27561
The ToString() method for DateTime has an overload that takes a custom format string.
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
It would be fairly simple to ask for, or establish the users region, and send dates to him in the correct format.
Beyond that, there is an overload of ToString that takes a System.Globalization.CultureInfo object so you can do something like.
myDateTime.ToString(new System.Globalization.CultureInfo(Thread.CurrentThread.CurrentCulture.Name));
This should give you the datetime formatted correctly for the users region based on OS settings.
Upvotes: 0
Reputation: 6813
You can also parse and format dates for other cultures by setting the current culture or Using a CultureInfo to format the string.
See http://msdn.microsoft.com/en-us/library/5hh873ya.aspx for more info.
Upvotes: 0
Reputation: 54877
If you’re hard-coding the date, then you should use the DateTime(int,int,int)
constructor, rather than parsing it from a string. The three parameters would always be interpreted as year, month, day (in that order).
var launch = new DateTime(2012, 08, 11);
Upvotes: 2