C#日期時(shí)間格式化小結(jié)
日期轉(zhuǎn)化一
為了達(dá)到不同的顯示效果有時(shí),我們需要對(duì)時(shí)間進(jìn)行轉(zhuǎn)化,默認(rèn)格式為:2007-01-03 14:33:34 ,要轉(zhuǎn)化為其他格式,要用到DateTime.ToString的方法(String, IFormatProvider),如下所示:
using System;
using System.Globalization;
String format="D";
DateTime date=DataTime,Now;
Response.Write(date.ToString(format, DateTimeFormatInfo.InvariantInfo));
結(jié)果輸出
Thursday, June 16, 2005
參數(shù)format格式詳細(xì)用法:
格式字符 關(guān)聯(lián)屬性/說(shuō)明
d ShortDatePattern
D LongDatePattern
f 完整日期和時(shí)間(長(zhǎng)日期和短時(shí)間)
F FullDateTimePattern(長(zhǎng)日期和長(zhǎng)時(shí)間)
g 常規(guī)(短日期和短時(shí)間)
G 常規(guī)(短日期和長(zhǎng)時(shí)間)
m、M MonthDayPattern
r、R RFC1123Pattern
s 使用當(dāng)?shù)貢r(shí)間的 SortableDateTimePattern(基于 ISO 8601)
t ShortTimePattern
T LongTimePattern
u UniversalSortableDateTimePattern 用于顯示通用時(shí)間的格式
U 使用通用時(shí)間的完整日期和時(shí)間(長(zhǎng)日期和長(zhǎng)時(shí)間)
y、Y YearMonthPattern
下表列出了可被合并以構(gòu)造自定義模式的模式。這些模式是區(qū)分大小寫(xiě)的;例如,識(shí)別“MM”,但不識(shí)別“mm”。如果自定義模式包含空白字符或用單引號(hào)括起來(lái)的字符,則輸出字符串頁(yè)也將包含這些字符。未定義為格式模式的一部分或未定義為格式字符的字符按其原義復(fù)制。
格式模式 說(shuō)明
d 月中的某一天。一位數(shù)的日期沒(méi)有前導(dǎo)零。
dd 月中的某一天。一位數(shù)的日期有一個(gè)前導(dǎo)零。
ddd 周中某天的縮寫(xiě)名稱,在 AbbreviatedDayNames 中定義。
dddd 周中某天的完整名稱,在 DayNames 中定義。
M 月份數(shù)字。一位數(shù)的月份沒(méi)有前導(dǎo)零。
MM 月份數(shù)字。一位數(shù)的月份有一個(gè)前導(dǎo)零。
MMM 月份的縮寫(xiě)名稱,在 AbbreviatedMonthNames 中定義。
MMMM 月份的完整名稱,在 MonthNames 中定義。
y 不包含紀(jì)元的年份。如果不包含紀(jì)元的年份小于 10,則顯示不具有前導(dǎo)零的年份。
yy 不包含紀(jì)元的年份。如果不包含紀(jì)元的年份小于 10,則顯示具有前導(dǎo)零的年份。
yyyy 包括紀(jì)元的四位數(shù)的年份。
gg 時(shí)期或紀(jì)元。如果要設(shè)置格式的日期不具有關(guān)聯(lián)的時(shí)期或紀(jì)元字符串,則忽略該模式。
h 12 小時(shí)制的小時(shí)。一位數(shù)的小時(shí)數(shù)沒(méi)有前導(dǎo)零。
hh 12 小時(shí)制的小時(shí)。一位數(shù)的小時(shí)數(shù)有前導(dǎo)零。
H 24 小時(shí)制的小時(shí)。一位數(shù)的小時(shí)數(shù)沒(méi)有前導(dǎo)零。
HH 24 小時(shí)制的小時(shí)。一位數(shù)的小時(shí)數(shù)有前導(dǎo)零。
m 分鐘。一位數(shù)的分鐘數(shù)沒(méi)有前導(dǎo)零。
mm 分鐘。一位數(shù)的分鐘數(shù)有一個(gè)前導(dǎo)零。
s 秒。一位數(shù)的秒數(shù)沒(méi)有前導(dǎo)零。
ss 秒。一位數(shù)的秒數(shù)有一個(gè)前導(dǎo)零。
f 秒的小數(shù)精度為一位。其余數(shù)字被截?cái)唷?
ff 秒的小數(shù)精度為兩位。其余數(shù)字被截?cái)唷?
fff 秒的小數(shù)精度為三位。其余數(shù)字被截?cái)唷?
ffff 秒的小數(shù)精度為四位。其余數(shù)字被截?cái)唷?
fffff 秒的小數(shù)精度為五位。其余數(shù)字被截?cái)唷?
ffffff 秒的小數(shù)精度為六位。其余數(shù)字被截?cái)唷?
fffffff 秒的小數(shù)精度為七位。其余數(shù)字被截?cái)唷?
t 在 AMDesignator 或 PMDesignator 中定義的 AM/PM 指示項(xiàng)的第一個(gè)字符(如果存在)。
tt 在 AMDesignator 或 PMDesignator 中定義的 AM/PM 指示項(xiàng)(如果存在)。
z 時(shí)區(qū)偏移量(“+”或“-”后面僅跟小時(shí))。一位數(shù)的小時(shí)數(shù)沒(méi)有前導(dǎo)零。例如,太平洋標(biāo)準(zhǔn)時(shí)間是“-8”。
zz 時(shí)區(qū)偏移量(“+”或“-”后面僅跟小時(shí))。一位數(shù)的小時(shí)數(shù)有前導(dǎo)零。例如,太平洋標(biāo)準(zhǔn)時(shí)間是“-08”。
zzz 完整時(shí)區(qū)偏移量(“+”或“-”后面跟有小時(shí)和分鐘)。一位數(shù)的小時(shí)數(shù)和分鐘數(shù)有前導(dǎo)零。例如,太平洋標(biāo)準(zhǔn)時(shí)間是“-08:00”。
: 在 TimeSeparator 中定義的默認(rèn)時(shí)間分隔符。
/ 在 DateSeparator 中定義的默認(rèn)日期分隔符。
% c 其中 c 是格式模式(如果單獨(dú)使用)。如果格式模式與原義字符或其他格式模式合并,則可以省略“%”字符。
c 其中 c 是任意字符。照原義顯示字符。若要顯示反斜杠字符,請(qǐng)使用“\”。
只有上面第二個(gè)表中列出的格式模式才能用于創(chuàng)建自定義模式;在第一個(gè)表中列出的標(biāo)準(zhǔn)格式字符不能用于創(chuàng)建自定義模式。自定義模式的長(zhǎng)度至少為兩個(gè)字符;例如,
DateTime.ToString( "d") 返回 DateTime 值;“d”是標(biāo)準(zhǔn)短日期模式。
DateTime.ToString( "%d") 返回月中的某天;“%d”是自定義模式。
DateTime.ToString( "d ") 返回后面跟有一個(gè)空白字符的月中的某天;“d”是自定義模式。
比較方便的是,上面的參數(shù)可以隨意組合,并且不會(huì)出錯(cuò),多試試,肯定會(huì)找到你要的時(shí)間格式
如要得到2005年06月 這樣格式的時(shí)間
可以這樣寫(xiě):
date.ToString("yyyy年MM月", DateTimeFormatInfo.InvariantInfo)
日期轉(zhuǎn)化二
DateTime dt = DateTime.Now;
Label1.Text = dt.ToString();//2005-11-5 13:21:25
Label2.Text = dt.ToFileTime().ToString();//127756416859912816
Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816
Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日
Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
Label7.Text = dt.ToOADate().ToString();//38661.5565508218
Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
Label9.Text = dt.ToShortTimeString().ToString();//13:21
Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
Label1.Text = dt.Year.ToString();//2005
Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
Label3.Text = dt.DayOfWeek.ToString();//Saturday
Label4.Text = dt.DayOfYear.ToString();//309
Label5.Text = dt.Hour.ToString();//13
Label6.Text = dt.Millisecond.ToString();//441
Label7.Text = dt.Minute.ToString();//30
Label8.Text = dt.Month.ToString();//11
Label9.Text = dt.Second.ToString();//28
Label10.Text = dt.Ticks.ToString();//632667942284412864
Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
Label1.Text = dt.ToString();//2005-11-5 13:47:04
Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
Label10.Text = dt.CompareTo(dt).ToString();//0
Label11.Text = dt.Add(?).ToString();//問(wèn)號(hào)為一個(gè)時(shí)間段
Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
Label2.Text = dt.Equals(dt).ToString();//True
Label3.Text = dt.GetHashCode().ToString();//1474088234
Label4.Text = dt.GetType().ToString();//System.DateTime
Label5.Text = dt.GetTypeCode().ToString();//DateTime
Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06
Label3.Text = dt.GetDateTimeFormats('y')[0].ToString();//2005年11月
Label4.Text = dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
Label6.Text = dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
Label7.Text = dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日
Label8.Text = dt.GetDateTimeFormats('M')[0].ToString();//11月5日
Label9.Text = dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06
Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
Label1.Text = string.Format("{0:d}",dt);//2005-11-5
Label2.Text = string.Format("{0:D}",dt);//2005年11月5日
Label3.Text = string.Format("{0:f}",dt);//2005年11月5日 14:23
Label4.Text = string.Format("{0:F}",dt);//2005年11月5日 14:23:23
Label5.Text = string.Format("{0:g}",dt);//2005-11-5 14:23
Label6.Text = string.Format("{0:G}",dt);//2005-11-5 14:23:23
Label7.Text = string.Format("{0:M}",dt);//11月5日
Label8.Text = string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
Label9.Text = string.Format("{0:s}",dt);//2005-11-05T14:23:23
Label10.Text string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005年11月
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23
Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);
[/code]
C#比較兩時(shí)間大小
1、比較時(shí)間大小的實(shí)驗(yàn)
string st1="12:13";
string st2="14:14";
DateTime dt1=Convert.ToDateTime(st1);
DateTime dt2=Convert.ToDateTime(st2);
DateTime dt3=DateTime.Now;
if(DateTime.Compare(dt1,dt2)>0)
msg.Text=st1+">"+st2;
else
msg.Text=st1+"<"+st2;
msg.Text+="\r\n"+dt1.ToString();
if(DateTime.Compare(dt1,dt3)>0)
msg.Text+="\r\n"+st1+">"+dt3.ToString();
else
msg.Text+="\r\n"+st1+"<"+dt3.ToString();
2、計(jì)算兩個(gè)時(shí)間差值的函數(shù),返回時(shí)間差的絕對(duì)值:
private string DateDiff(DateTime DateTime1,DateTime DateTime2)
{
string dateDiff=null;
try
{
TimeSpan ts1=new TimeSpan(DateTime1.Ticks);
TimeSpan ts2=new TimeSpan(DateTime2.Ticks);
TimeSpan ts=ts1.Subtract(ts2).Duration();
dateDiff=ts.Days.ToString()+"天"
+ts.Hours.ToString()+"小時(shí)"
+ts.Minutes.ToString()+"分鐘"
+ts.Seconds.ToString()+"秒";
}
catch
{ }
return dateDiff;
}
3、實(shí)現(xiàn)計(jì)算DateTime1-36天=DateTime2的功能
TimeSpan ts=new TimeSpan(40,0,0,0);
DateTime dt2=DateTime.Now.Subtract(ts);
msg.Text=DateTime.Now.ToString()+"-"+ts.Days.ToString()+"天\r\n";
msg.Text+=dt2.ToString();
相關(guān)文章
UnityRTS實(shí)現(xiàn)相機(jī)移動(dòng)縮放功能
這篇文章主要為大家詳細(xì)介紹了UnityRTS實(shí)現(xiàn)相機(jī)的移動(dòng)縮放功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03C#實(shí)現(xiàn)將網(wǎng)頁(yè)保存成圖片的網(wǎng)頁(yè)拍照功能
這篇文章主要介紹了C#實(shí)現(xiàn)將網(wǎng)頁(yè)保存成圖片的網(wǎng)頁(yè)拍照功能,很實(shí)用的一個(gè)功能,需要的朋友可以參考下2014-07-07Unity實(shí)現(xiàn)簡(jiǎn)單換裝系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單換裝系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04C#的鋸齒數(shù)組以及C++實(shí)現(xiàn)代碼
鋸齒數(shù)組首先是二維數(shù)組,第一維的維數(shù)是確定的。之所以在C#中能夠出現(xiàn)靈活的鋸齒數(shù)組,是因?yàn)?,C#的數(shù)組是引用類(lèi)型(本質(zhì)上存放的是指針)。根據(jù)這個(gè)引用類(lèi)型(指針)的概念,C++中用指針數(shù)組同樣可以實(shí)現(xiàn)2013-09-09C#實(shí)現(xiàn)復(fù)雜XML的序列化與反序列化
這篇文章主要介紹了C#實(shí)現(xiàn)復(fù)雜XML的序列化與反序列化的方法,是非常實(shí)用的一個(gè)技巧,需要的朋友可以參考下2014-09-09C#實(shí)現(xiàn)類(lèi)似jQuery的方法連綴功能
這篇文章主要介紹了C#實(shí)現(xiàn)類(lèi)似jQuery的方法連綴功能,可以簡(jiǎn)化語(yǔ)句,使代碼變得清晰簡(jiǎn)單,感興趣的小伙伴們可以參考一下2015-11-11C#?Winform消息通知之系統(tǒng)本地通知local?toast?notification
這篇文章主要為大家介紹了C#?Winform消息通知之系統(tǒng)本地通知local?toast?notification使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08C#編程實(shí)現(xiàn)動(dòng)態(tài)改變配置文件信息的方法
這篇文章主要介紹了C#編程實(shí)現(xiàn)動(dòng)態(tài)改變配置文件信息的方法,涉及C#針對(duì)xml格式文件的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06