如何在C#中格式化字符串日期 [英] How to format a string date in C#

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

问题描述

我有一个字符串,我想格式化的格式如下:"yyyy/mm/dd hh:mm"
然后不设置为label.text = ....
你能帮我吗?
谢谢.

I have a string and I want to format is the same as follow:"yyyy/mm/dd hh:mm"
and then set not to a label.text=....
Can you help me?
thanks you.

推荐答案

将字符串轻松转换为DateTime,DateTime到字符串和格式 [^ ]


每次我说请先用谷歌搜索您的问题
我用谷歌搜索了您的问题,并在0.02秒内找到了它. 为什么不尝试自己搜索:confused:

以下代码将为您提供帮助!

every time i said that please first google your question
i google your question and found just in 0.02sec :)
why you don''t try to search by yourself :confused:

below code will help you!

// 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




源链接




source link


为什么不使用此功能:
why dont you use this:
string dt;
DateTime date = DateTime.Now;    // display format: 4/25/2008 11:45:44 AM
dt = date.ToLongDateString();          // display format:  Friday, April 25, 2008 
dt = date.ToShortDateString();         // display format:  4/25/2008
dt = date.ToShortTimeString();        // display format: 11:45 AM
dt = date.ToLongTimeString();        // display format:  11:45:44 AM


或这就是您想要的:


or this is what you want:

Console.WriteLine("s = {0:s}", CurrTime );  // yyyy-mm-dd hh:mm:ss  can be sorted!


这篇关于如何在C#中格式化字符串日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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