C#4.0中的DateTime格式 [英] DateTime format in C# 4.0

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

问题描述



我正在尝试将C#代码从.Net 3.5迁移到.Net 4.0.面临一个奇怪的问题.
以下代码在.Net 3.5中运行良好.

Hi,

I am trying to move my C# code from .Net 3.5 to .Net 4.0. Facing a strange problem.
The following code worked well in .Net 3.5

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



但是,由于某种原因,在4.0运行中,它产生的结果类似于yyyy-MM-dd而不是yyyy/MM/dd.输出中的``/''替换为``-''.

我在Google上搜索了一段时间,但是没有运气.



But, for some reason, in the 4.0 run, it produces result like yyyy-MM-dd rather than yyyy/MM/dd . The ''/'' is replaced by ''-'' in the output.

I googled around for quite some time.. but no luck. Has someone seen this problem before?

推荐答案

尝试以下代码,

Try the below code,

DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo)



问题在于转换功能正在使用计算机日期/时间设置中的分隔符.上面的代码应该可以正常工作.


如果有帮助,则将其标记为答案



The problem is the convert function is using the separator from the Date/Time settings of your machine. The above code should work perfectly.


Mark it as answer if it is helpful


实际上.Net Framework 4.0中的格式已被更改.与.Net3.5相比,它接受Format作为DateTime.Now. ToString("dd/MMM/yyyy")此格式将日期显示为
2011年10月11日,如果将月份更改为2 MM,即DateTime.Now.ToString("dd/MM/yyyy"),现在日期将以以下格式显示:2011年11月10日.
Actually The Format Was Chaged In .Net Framework 4.0 Compare To .Net3.5 It Accepts Format as DateTime.Now.ToString("dd/MMM/yyyy") this format displays the date as
11/Oct/2011 and if changed the month to 2 MM i,e DateTime.Now.ToString("dd/MM/yyyy") and now the date will display in this format: 11/10/2011.


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

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