日期格式以不同格式显示 [英] date format is displaying in different format

查看:90
本文介绍了日期格式以不同格式显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在excel表格中以下面的格式显示日期

row [i] .Cells.Add(date.ToString(MM / dd / yyyy),DataType.String,style2 );

日期格式MM / dd / yyyy在输出中显示为MM-dd-yyyy ..如何解决这个问题?

我希望输出为MM / dd / yyyy。

Iam displaying date in the excel sheet in the below format
row[i].Cells.Add(date.ToString("MM/dd/yyyy"), DataType.String, "style2");
The date format MM/dd/yyyy is displaying in output as MM-dd-yyyy..how to resolve this?
I want the output to be MM/dd/yyyy.

推荐答案



使用Numberformat



http://msdn.microsoft.com/en-us/library/office/ff196401.aspx [ ^ ]



格式:MM / dd / yyyy
Hi,
Use Numberformat

http://msdn.microsoft.com/en-us/library/office/ff196401.aspx[^]

Format : "MM/dd/yyyy"


为什么它发生很简单 - 最好留下它单独使用。

DateTime格式字符串中的'/'字符不是文字'/'字符,任何比MM更接近文字'M'后跟'M'。 MM替换为带有前导零的月份编号,/替换为当前系统设置中指定的日期分隔符。



因此,如果您运行代码的计算机设置为/,因此用户更喜欢它,那么将使用/。但是如果用户喜欢10-16-2013​​并且已经设置了他的系统,那么将使用' - '。



如果您正在展示直接向用户提供信息,那么您应该使用他喜欢的格式(但包括按照他预期的方式获取整个日期格式:只有美国将月份放在第一位),其中包括使用他期望的字符。



如果你绝对需要'/'。然后用转义字符作为前缀:

Why it happens is quite simple - and it's probably best to leave it alone.
The '/' character in a DateTime format string is not a literal '/' character, any more than "MM" is a literal 'M' followed by an 'M'. "MM" is replaced with "the Month number with leading zero", and '/' is replaced with "the date separator as specified in the current system settings".

So if the computer you run your code on is set to '/' becasu ethe user prefers it, then '/' will be used. But if the user likes "10-16-2013" instead and has set his system that way, then '-' will be used.

If you are presenting the info to the user directly, then you should probably use the format he prefers (but that includes getting the whole date format the way he expects: only the USA puts the month first) which includes using the characters he expects.

If you absolutely need a '/'. then prefix it with an escape character:
row[i].Cells.Add(date.ToString(@"MM\/dd\/yyyy"), DataType.String, "style2");


这篇关于日期格式以不同格式显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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