帮助转换日期和时间 [英] help converting date and time

查看:66
本文介绍了帮助转换日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助转换此字符串:

2011-10-20T20:49:59-0400

我需要使字符串看起来像这样:

20:49 10/20/2011

I need help converting this string:

2011-10-20T20:49:59-0400

I need to get the string to look like this :

20:49 10/20/2011

help would be great.

推荐答案

第一件事是:首先考虑考虑必需的格式;重新格式化任何东西通常都表示某人犯了错误,但我知道那也许不是你的.

第二件事:您的输入格式是最好的输入格式之一,因为时间顺序与字母数字相同,因此请考虑继续使用它.

第三件事:尝试使用System.DateTime,而不是字符串.仅当您在屏幕上或其他地方显示最终结果时才需要字符串.

最后,如何重新格式化?这个想法是:将输入字符串解析为System.DateTime,一直使用System.DateTime,并且当您需要将结果显示为字符串时,请使用方法之一System.DateTime.String,指定适当的格式.有时您需要显式指定的格式,但更多情况下,它应基于当前的线程区域性,有时还应基于显式指定的区域性.

要进行解析,请使用System.DateTime.Parse, System.DateTime.ParseExact方法之一进行格式化:System.DateTime.ToString方法之一.

请注意显示如何将CultureInfo用作IFormatProvider的代码示例: http://msdn. microsoft.com/en-us/library/ht77y576.aspx [ ^ ].

请参阅:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ].

请注意,某些标准格式针对不同的文化会产生不同的结果;并且使用的区域性由调用线程的当前区域性定义.要更改线程的区域性,请使用System.Threading.Thread.CurrentCulture,请参见:
http://msdn.microsoft.com/en-us/library/system. threading.thread.currentculture.aspx [ ^ ].

—SA
First thing is: think about taking care of required format in first place; re-formatting of anything is usually a sign of someone''s mistake, but I understand that maybe not yours.

Second thing: your input format is one of the best as ordering in time is the same as alphanumeric, so think about keeping to use it.

Third thing: try to work with System.DateTime, not with strings. A string is only needed when you present a final result on screen or somewhere else.

Finally, how to reformat? The idea is: parse input string as System.DateTime, work with System.DateTime all the time, and when you need to present the results as a string, use one of the method System.DateTime.String, specifying appropriate format. Sometime you need explicitly specified format, but more often it should be based on current thread culture, sometimes of explicitly specified culture.

For parsing, use one of the methods System.DateTime.Parse, System.DateTime.ParseExact, for formatting: one of the System.DateTime.ToString methods.

Pay attention for the code sample showing how to use CultureInfo as IFormatProvider: http://msdn.microsoft.com/en-us/library/ht77y576.aspx[^].

Please see:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^],
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

Pay attention that some standard formats produce different results for different cultures; and the culture used is defined by the current culture of the calling thread. To change the culture of the thread, use System.Threading.Thread.CurrentCulture, please see:
http://msdn.microsoft.com/en-us/library/system.threading.thread.currentculture.aspx[^].

—SA


这篇关于帮助转换日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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