C#4.0中的DateTime格式

编程入门 行业动态 更新时间:2024-10-27 21:18:44
本文介绍了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格式

本文发布于:2023-11-12 00:45:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1580028.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:格式   DateTime

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!