在c#中为Windows Phone设置特定的日期格式
相对较新的 Windows Phone 开发人员在这里寻求一些帮助。基本上我正在摆弄一个我希望最终投放市场的应用程序。
基本上,该应用程序正在倒计时到特定日期,我的倒计时工作没有任何问题,但是我的日期格式确实有问题,因为我在英国,日期格式是 dd/MM/yyyy,而状态为 MM/dd/yyyy。因此,对于美国的任何人来说,该应用程序都会陷入负数。基本上我需要某种解决方法的帮助,无论是为我的应用程序设置通用日期格式还是类似的东西。这是倒计时的代码:
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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您对日期进行硬编码,则应使用
DateTime (int,int,int)
构造函数,而不是从字符串中解析它。这三个参数始终被解释为年、月、日(按该顺序)。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).您还可以通过设置当前区域性或使用 CultureInfo 来格式化字符串来解析和格式化其他区域性的日期。
请参阅 http://msdn.microsoft.com/en-us/library/5hh873ya。 aspx 了解更多信息。
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.
DateTime 的 ToString() 方法有一个重载,它采用自定义格式字符串。
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
询问或建立用户区域并以正确的格式向他发送日期将是相当简单的。
除此之外,还有一个 ToString 的重载,它接受一个 System.Globalization.CultureInfo 对象,这样你就可以做类似的事情。
这应该为您提供根据操作系统设置为用户区域正确格式化的日期时间。
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.
This should give you the datetime formatted correctly for the users region based on OS settings.