格式日期在C# [英] Format date in C#
本文介绍了格式日期在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串用于显示日期时间,如Mon,dd Dec YYYY hh:mm:ss。我想像dd Dec YYYY显示它。有没有什么简单的方法呢?
I have a string used to display the datetime like Mon, dd Dec YYYY hh:mm:ss. I want to show it like dd Dec YYYY. Is there any simple way to do it?
推荐答案
从 http://www.csharp-examples.net/string-format-datetime/
// create date time 2008-03-09 16:05:07.123
DateTime dt = new DateTime(2008, 3, 9, 16, 5, 7, 123);
String.Format("{0:y yy yyy yyyy}", dt); // "8 08 008 2008" year
String.Format("{0:M MM MMM MMMM}", dt); // "3 03 Mar March" month
String.Format("{0:d dd ddd dddd}", dt); // "9 09 Sun Sunday" day
String.Format("{0:h hh H HH}", dt); // "4 04 16 16" hour 12/24
String.Format("{0:m mm}", dt); // "5 05" minute
String.Format("{0:s ss}", dt); // "7 07" second
String.Format("{0:f ff fff ffff}", dt); // "1 12 123 1230" sec.fraction
String.Format("{0:F FF FFF FFFF}", dt); // "1 12 123 123" without zeroes
String.Format("{0:t tt}", dt); // "P PM" A.M. or P.M.
String.Format("{0:z zz zzz}", dt); // "-6 -06 -06:00" time zone
另请参见:
如果您只有字符串,只需将字符串拆分为数组,并以其他顺序连接您想要的部分
if you only have the string, just split the string to an array, and concatenate the parts you want in another order
String str = "Mon, dd Dec YYYY hh:mm:ss";
String[] strArr = str.Split(" ");
str = strArr[2] + " " + strArr[3];
如果日期可以更改,那么请在他的答案中发布SLaks。
If the date can change, then do what SLaks posted in his answer
这篇关于格式日期在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文