如何将日期转换为字符串 [英] How do I convert date to string

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

问题描述

如何从日期转换为字符串c#

解决方案

与大多数表面看起来简单的东西一样,这里隐藏着复杂性。当你谈到将日期转换为字符串时,重要的是要实现一些事情。



首先,.NET中没有固有的Date类型。有一个 DateTime 对象,这表明日期总是有一个时间组件。因此,如果您仅在日期部分之后,则需要考虑使用 ToShortDateString() ToLongDateString()



默认输出日期使用 CurrentCulture DateTimeFormatInfo 控制日期的显示方式。这是一个非常罕见的情况,你应该干扰这一点。用户倾向于将其设置为他们自己国家的文化,因此改变其输出方式最终会导致混淆。考虑这个例子 - 用户想要在2014年11月13日检查他们是否没有错过约会,并且您的应用程序弹出以显示约会是在2014年12月11日 - 用户认为 - 很棒,我我有充足的时间。但是,您的应用程序已覆盖格式,因此它使用MM / dd / yyyy并且用户认为它是dd / MM / yyyy(这就是他们设置的内容)。预约日期实际上是11月12日。



如果需要更改日期格式,可以使用<指定需要输出的格式code> .ToString(dateFormatSpecifier);
其中 dateFormatSpecifier 标识你想要的日期要输出 [ ^ ]。或者,您可以使用 ToString CultureInfo 匹配您想要的输出。例如,如果要将日期格式设置为美国日期,请使用:

 myDate.ToString(  d new  CultureInfo(  en-US)); 


将DateTime转换为字符串的方法很多;试试这些:

 DateTime aDate = DateTime.Now; 

string s1 = aDate.ToString();

string s2 = aDate.ToString( '日期:'yyyy-MM-dd'时间:'HH:mm:ss);

string s3 = String .Format( {0},aDate);

在代码中设置断点,并检查值回来了解什么是可能的。还有其他高级选项,使用IFormatProvider处理DateTime到字符串转换,同时考虑到不同的语言;请参阅:[ ^ ]。


DateTime.ToString [< a href =http://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]

How you convert from a date to string in c#

解决方案

As with most things that appear simple on the surface, there is a hidden complexity here. When you talk about converting a date to a string, it's important to realise a few things.

First of all, there is no inherent Date type in .NET. There is a DateTime object, and this indicates that dates always have a time component. So, if you are after the date part alone then you need to consider outputting the date using either ToShortDateString() or ToLongDateString().

Outputting a date, by default, uses the CurrentCulture's DateTimeFormatInfo to control how the date is displayed. It's a very rare case that you should be interfering with this. User's tend to set this to the culture of their own country, so changing how it's output can end up causing confusion. Consider this example - the user wants to check on the 13th of November 2014 to see that they haven't missed an appointment and your application pops up to show that the appointment is on 11/12/2014 - the user thinks - great, I've got plenty of time. However, your application has overridden the format so it's using MM/dd/yyyy and the user thinks it's dd/MM/yyyy (that's what they set). The appointment date was actually on the 12th November.

If you need to change the format of the date, you can specify the format that it needs to be output using .ToString(dateFormatSpecifier); where the dateFormatSpecifier identifies how you want the date to be output[^]. Alternatively, you can use ToString with the CultureInfo that matches the output that you want. For instance, if you want to format the date as a US date, use:

myDate.ToString("d", new CultureInfo("en-US"));


Lots of ways to turn a DateTime into a string; try these:

DateTime aDate = DateTime.Now;

string s1 = aDate.ToString();

string s2 = aDate.ToString("'Date:' yyyy-MM-dd 'Time:' HH:mm:ss");

string s3 = String.Format("{0}", aDate);

Set a break-point in your code, and inspect the values returned to get a sense of what is possible. There are other, advanced options, using IFormatProvider for dealing with DateTime to string conversion taking into account different languages; see: [^].


DateTime.ToString[^]


这篇关于如何将日期转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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