DateTime的转换 [英] Convertion of DateTime

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

问题描述

大家好,

.NET是否具有用于将DateTime转换为 mm/dd/yyyy hh:mm:ss 的API?
我需要的是:
08/09/2011 04:05:02

AND
8/9/2011 4:05:02

我曾尝试拆分和连接字符串以获取所需的输出,但是它的代码太长.我需要使用C#或T-SQL的简短代码.而且,时间格式为 24小时

我已经在Google搜索了2个小时,但仍然无法提出解决方案.任何答案将不胜感激.

谢谢:)

Hi all,

Does .NET has API for converting DateTime to mm/dd/yyyy hh:mm:ss?
What i need is:
08/09/2011 04:05:02

AND NOT
8/9/2011 4:05:02

I''ve tried splitting and concatenating strings to get my desired output but it''s code is too long. I need a short code either in C# or T-SQL. And also, the format of time would be 24hrs

i''ve been searching google for 2 hrs but still could not come up with a solution. Any answer would be appreciated.

Thanks :)

推荐答案

这不是转换.重要的是要理解:例如,您需要使用时间数据,而不是其字符串表示形式,直到需要在UI中呈现数据为止.您需要的是数据格式.

您需要将System.DateTime.ToString与一些参数一起使用,这些参数定义了格式设置规则,您可以在下面引用的MSDN帮助页面中找到它们.参见:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx [^ ](查看所有方法ToString),
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ](标准格式字符串),
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ](自定义格式字符串).

-SA
This is not a conversion. It''s important to understand: you need to work with time data, not its string representation until you need to present data in UI, for example. What you need is formatting of data.

You need to use System.DateTime.ToString with some parameters defining formatting rules which you can find in the MSDN help pages referenced below. See:
http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^] (see all the methods ToString),
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^] (standard format strings),
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^] (custom format strings).

—SA


最简单的方法是使用 [ ^ ],它将给出的输出为"yyyy"-"MM"-"dd" T"HH":"mm":'的". -2008-04-10T06:30:00

该模式反映了已定义的标准(ISO 8601),并且该属性是只读的.因此,无论使用哪种区域性或提供的格式提供者,它始终是相同的.
The most simple way is to use DateTime.ToString("s")[^] it will give an output that is "yyyy''-''MM''-''dd''T''HH'':''mm'':''ss". - 2008-04-10T06:30:00

The pattern reflects a defined standard (ISO 8601), and the property is read-only. Therefore, it is always the same, regardless of the culture used or the format provider supplied.




Hi,

DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("MM/dd/yyyy HH:mm:ss"));



这可能对您有帮助,

谢谢
-Amit.



This may help you,

thanks
-Amit.


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

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