C#日期时间格式化
C#日期时间格式化日期转化一
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif为了达到不同的显示效果有时,我们需要对时间进行转化,默认格式为:2007-01-03 14:33:34 ,要转化为IT论坛格式,要用到DateTime.ToString的方法(String, IFormatProvider),如下所示:
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifusing System.Globalization;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifString format="D";
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifDateTime date=DataTime,Now;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifResponse.Write(date.ToString(format, DateTimeFormatInfo.InvariantInfo));
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif结果输出
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifThursday, June 16, 2005
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif参数format格式详细用法:
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif格式字符 关联属性/说明
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifd ShortDatePattern
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifD LongDatePattern
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.giff 完整日期和时间(长日期和短时间)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifF FullDateTimePattern(长日期和长时间)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifg 常规(短日期和短时间)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifG 常规(短日期和长时间)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifm、M MonthDayPattern
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifr、R RFC1123Pattern
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifs 使用当地时间的 SortableDateTimePattern(基于 ISO 8601)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gift ShortTimePattern
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifT LongTimePattern
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifu UniversalSortableDateTimePattern 用于显示通用时间的格式
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifU 使用通用时间的完整日期和时间(长日期和长时间)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gify、Y YearMonthPattern
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif下表列出了可被合并以构造自定义模式的模式。这些模式是区分大小写的;例如,识别“MM”,但不识别“mm”。如果自定义模式包含空白字符或用单引号括起来的字符,则输出字符串页也将包含这些字符。未定义为格式模式的一部分或未定义为格式字符的字符按其原义复制。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif格式模式 说明
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifd 月中的某一天。一位数的日期没有前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifdd 月中的某一天。一位数的日期有一个前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifdddd 周中某天的完整名称,在 DayNames 中定义。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifM 月份数字。一位数的月份没有前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifMM 月份数字。一位数的月份有一个前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifMMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifMMMM 月份的完整名称,在 MonthNames 中定义。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gify 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifyy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifyyyy 包括纪元的四位数的年份。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifgg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifh 12 小时制的小时。一位数的小时数没有前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifhh 12 小时制的小时。一位数的小时数有前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifH 24 小时制的小时。一位数的小时数没有前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifHH 24 小时制的小时。一位数的小时数有前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifm 分钟。一位数的分钟数没有前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifmm 分钟。一位数的分钟数有一个前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifs 秒。一位数的秒数没有前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifss 秒。一位数的秒数有一个前导零。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.giff 秒的小数精度为一位。其余数字被截断。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifff 秒的小数精度为两位。其余数字被截断。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.giffff 秒的小数精度为三位。其余数字被截断。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifffff 秒的小数精度为四位。其余数字被截断。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.giffffff 秒的小数精度为五位。其余数字被截断。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifffffff 秒的小数精度为六位。其余数字被截断。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.giffffffff 秒的小数精度为七位。其余数字被截断。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gift 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.giftt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifzz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifzzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif: 在 TimeSeparator 中定义的默认时间分隔符。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif/ 在 DateSeparator 中定义的默认日期分隔符。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif% c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或IT论坛格式模式合并,则可以省略“%”字符。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\”。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。自定义模式的长度至少为两个字符;例如,
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifDateTime.ToString( "d") 返回 DateTime 值;“d”是标准短日期模式。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifDateTime.ToString( "%d") 返回月中的某天;“%d”是自定义模式。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifDateTime.ToString( "d ") 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif比较方便的是,上面的参数可以随意组合,并且不会出错,多试试,肯定会找到你要的时间格式
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif如要得到2005年06月 这样格式的时间
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif可以这样写:
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifdate.ToString("yyyy年MM月", DateTimeFormatInfo.InvariantInfo)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif日期转化二
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifDateTime dt = DateTime.Now;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel1.Text = dt.ToString();//2005-11-5 13:21:25
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel2.Text = dt.ToFileTime().ToString();//127756416859912816
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel5.Text = dt.ToLongDateString().ToString();//2005年11月5日
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel6.Text = dt.ToLongTimeString().ToString();//13:21:25
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel7.Text = dt.ToOADate().ToString();//38661.5565508218
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel8.Text = dt.ToShortDateString().ToString();//2005-11-5
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel9.Text = dt.ToShortTimeString().ToString();//13:21
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel1.Text = dt.Year.ToString();//2005
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel2.Text = dt.Date.ToString();//2005-11-5 0:00:00
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel3.Text = dt.DayOfWeek.ToString();//Saturday
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel4.Text = dt.DayOfYear.ToString();//309
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel5.Text = dt.Hour.ToString();//13
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel6.Text = dt.Millisecond.ToString();//441
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel7.Text = dt.Minute.ToString();//30
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel8.Text = dt.Month.ToString();//11
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel9.Text = dt.Second.ToString();//28
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel10.Text = dt.Ticks.ToString();//632667942284412864
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel1.Text = dt.ToString();//2005-11-5 13:47:04
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel10.Text = dt.CompareTo(dt).ToString();//0
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel11.Text = dt.Add(?).ToString();//问号为一个时间段
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel2.Text = dt.Equals(dt).ToString();//True
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel3.Text = dt.GetHashCode().ToString();//1474088234
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel4.Text = dt.GetType().ToString();//System.DateTime
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel5.Text = dt.GetTypeCode().ToString();//DateTime
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel3.Text = dt.GetDateTimeFormats('y')[0].ToString();//2005年11月
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel4.Text = dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel6.Text = dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel7.Text = dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel8.Text = dt.GetDateTimeFormats('M')[0].ToString();//11月5日
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel9.Text = dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel1.Text = string.Format("{0:d}",dt);//2005-11-5
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel2.Text = string.Format("{0:D}",dt);//2005年11月5日
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel3.Text = string.Format("{0:f}",dt);//2005年11月5日 14:23
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel4.Text = string.Format("{0:F}",dt);//2005年11月5日 14:23:23
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel5.Text = string.Format("{0:g}",dt);//2005-11-5 14:23
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel6.Text = string.Format("{0:G}",dt);//2005-11-5 14:23:23
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel7.Text = string.Format("{0:M}",dt);//11月5日
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel8.Text = string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel9.Text = string.Format("{0:s}",dt);//2005-11-05T14:23:23
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel10.Text string.Format("{0:t}",dt);//14:23
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel11.Text = string.Format("{0:T}",dt);//14:23:23
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel14.Text = string.Format("{0:Y}",dt);//2005年11月
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifLabel16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);
C#比较两时间大小1、比较时间大小的实验
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、计算两个时间差值的函数,返回时间差的绝对值:
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()+"小时"
+ts.Minutes.ToString()+"分钟"
+ts.Seconds.ToString()+"秒";
}
catch
{ }
return dateDiff;
}3、实现计算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();http://www.cnblogs.com/hantianwei/archive/2010/09/23/1833228.html
C#日期时间格式化
页:
[1]