DateTimeFormat问题 [英] DateTimeFormat Issue

查看:237
本文介绍了DateTimeFormat问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白下面从日期 - 对象所需的输出:

I dont get following desired output from a DateTime-Object:

2010-03-29 13点15分00秒

2010-03-29 13:15:00

我的默认的本地化是脱DE,所以一个简单的ToString给我:

My default localization is de-DE, so a simple ToString gives me:

29.03.2010 13点15分00秒

29.03.2010 13:15:00

我已经试过是创建从美国文化的DateTimeFormatInfo对象,并使用其DateTimePatterns但没有成功:

What I've tried is to create a DateTimeFormatInfo Object from US-Culture and use its DateTimePatterns but without success:

Dim usDateFormat As Globalization.DateTimeFormatInfo = New Globalization.CultureInfo("en-US", False).DateTimeFormat
usDateFormat.DateSeparator = "-"
ruleResult.Claim.Last_Updated_Date.ToString(usDateFormat.ShortDatePattern)

输出: 2010年3月29日

什么是最好的方式(可读,最快)让我的期望DateTimeFormat(我需要有时短日期,有时日期和时间就像在我上面的例子)?

What is the best way (readable,fastest) to get my desired DateTimeFormat(i need sometimes the short Date and sometimes the Date and Time like in my example above)?

感谢你在期待, 蒂姆·

Thanking you in anticipation, Tim

修改:解决方案,而不是为什么使用的String.Format或的ToString(:毫米SS YYYY-MM-DD HH)什么时候我的DateTimeFormatInfo方法不仅工作,使其成为性能差异私人共享(静态)的DateTimeFormatInfo-对象?我打电话这个功能至少30,000次,并有许多的日期里面格式化。

EDIT: why does my DateTimeFormatInfo approach not work and makes it a performance difference when using the String.Format or ToString("yyyy-MM-dd HH:mm:ss") solutions instead of a private shared(static) DateTimeFormatInfo-Object? I'm calling this function at least 30k times and there are many Dates to format inside.

推荐答案

我觉得你在找什么是:

DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

下面是一些例子,我想如果看那么你可以理解它是如何工作的:

Here are some examples, I think if look at then you can understand how it works:

// create date time 2008-03-09 16:05:07.123
DateTime dateTime = new DateTime(2008, 3, 9, 16, 5, 7, 123);

dateTime.ToString("y yy yyy yyyy");  // "8 08 008 2008"   year
dateTime.ToString("M MM MMM MMMM");  // "3 03 Mar March"  month
dateTime.ToString("d dd ddd dddd");  // "9 09 Sun Sunday" day
dateTime.ToString("h hh H HH"    );  // "4 04 16 16"      hour 12/24
dateTime.ToString("m mm"         );  // "5 05"            minute
dateTime.ToString("s ss"         );  // "7 07"            second
dateTime.ToString("f ff fff ffff");  // "1 12 123 1230"   sec.fraction
dateTime.ToString("F FF FFF FFFF");  // "1 12 123 123"    without zeroes
dateTime.ToString("t tt"         );  // "P PM"            A.M. or P.M.
dateTime.ToString("z zz zzz"     );  // "-6 -06 -06:00"   time zone

这篇关于DateTimeFormat问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆