C#时间日期操作

来源:互联网 发布:日历记事本软件下载 编辑:程序博客网 时间:2024/05/01 22:45
 一、C# 日期格式


DateTime dt = DateTime.Now;  
dt.ToString();//2005-11-5 13:21:25 
dt.ToFileTime().ToString();//127756416859912816 
dt.ToFileTimeUtc().ToString();//127756704859912816 
dt.ToLocalTime().ToString();//2005-11-5 21:21:25 
dt.ToLongDateString().ToString();//2005年11月5日 
dt.ToLongTimeString().ToString();//13:21:25 
dt.ToOADate().ToString();//38661.5565508218 
dt.ToShortDateString().ToString();//2005-11-5 
dt.ToShortTimeString().ToString();//13:21 
dt.ToUniversalTime().ToString();//2005-11-5 5:21:25 
dt.Year.ToString();//2005 
dt.Date.ToString();//2005-11-5 0:00:00 
dt.DayOfWeek.ToString();//Saturday 
dt.DayOfYear.ToString();//309 
dt.Hour.ToString();//13 
dt.Millisecond.ToString();//441 
dt.Minute.ToString();//30 
dt.Month.ToString();//11 
22. dt.Second.ToString();//28 
23. dt.Ticks.ToString();//632667942284412864 
24. dt.TimeOfDay.ToString();//13:30:28.4412864 
25. dt.ToString();//2005-11-5 13:47:04 
dt.AddYears(1).ToString();//2006-11-5 13:47:04 
dt.AddDays(1.1).ToString();//2005-11-6 16:11:04 
dt.AddHours(1.1).ToString();//2005-11-5 14:53:04 
dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04 
dt.AddMonths(1).ToString();//2005-12-5 13:47:04 
dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05 
dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10 
dt.AddTicks(1000).ToString();//2005-11-5 13:47:04 
dt.Add(?).ToString();//问号为一个时间段 
dt.Equals("2005-11-6 16:11:04").ToString();//False 
dt.Equals(dt).ToString();//True 
dt.GetHashCode().ToString();//1474088234 
dt.GetType().ToString();//System.DateTime 
dt.GetTypeCode().ToString();//DateTime 
  
dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25 
dt.GetDateTimeFormats('t')[0].ToString();//14:06 
dt.GetDateTimeFormats('y')[0].ToString();//2005年11月 
dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日 
dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05 
dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05 
dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日 
dt.GetDateTimeFormats('M')[0].ToString();//11月5日 
dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06 
dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06 
dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT 
  
string.Format("{0:d}",dt);//2005-11-5 
string.Format("{0:D}",dt);//2005年11月5日 
string.Format("{0:f}",dt);//2005年11月5日 14:23 
string.Format("{0:F}",dt);//2005年11月5日 14:23:23 
string.Format("{0:g}",dt);//2005-11-5 14:23 
string.Format("{0:G}",dt);//2005-11-5 14:23:23 
string.Format("{0:M}",dt);//11月5日 
string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT 
string.Format("{0:s}",dt);//2005-11-05T14:23:23 
string.Format("{0:t}",dt);//14:23 
string.Format("{0:T}",dt);//14:23:23 
string.Format("{0:u}",dt);//2005-11-05 14:23:23Z 
string.Format("{0:U}",dt);//2005年11月5日 6:23:23 
string.Format("{0:Y}",dt);//2005年11月 
string.Format("{0}",dt);//2005-11-5 14:23:23 
string.Format("{0:yyyyMMddHHmmssffff}",dt); 
二、日期格式模式 说明


d 月中的某一天。一位数的日期没有前导零。


dd 月中的某一天。一位数的日期有一个前导零。


ddd 周中某天的缩写名称,在 AbbreviatedDayNames 中定义。


dddd 周中某天的完整名称,在 DayNames 中定义。


M 月份数字。一位数的月份没有前导零。


MM 月份数字。一位数的月份有一个前导零。


MMM 月份的缩写名称,在 AbbreviatedMonthNames 中定义。


MMMM 月份的完整名称,在 MonthNames 中定义。


y 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示不具有前导零的年份。


yy 不包含纪元的年份。如果不包含纪元的年份小于 10,则显示具有前导零的年份。


yyyy 包括纪元的四位数的年份。


gg 时期或纪元。如果要设置格式的日期不具有关联的时期或纪元字符串,则忽略该模式。


h 12 小时制的小时。一位数的小时数没有前导零。


hh 12 小时制的小时。一位数的小时数有前导零。


H 24 小时制的小时。一位数的小时数没有前导零。


HH 24 小时制的小时。一位数的小时数有前导零。


m 分钟。一位数的分钟数没有前导零。


mm 分钟。一位数的分钟数有一个前导零。


s 秒。一位数的秒数没有前导零。


ss 秒。一位数的秒数有一个前导零。


f 秒的小数精度为一位。其余数字被截断。


ff 秒的小数精度为两位。其余数字被截断。


fff 秒的小数精度为三位。其余数字被截断。


ffff 秒的小数精度为四位。其余数字被截断。


fffff 秒的小数精度为五位。其余数字被截断。


ffffff 秒的小数精度为六位。其余数字被截断。


fffffff 秒的小数精度为七位。其余数字被截断。


t 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项的第一个字符(如果存在)。


tt 在 AMDesignator 或 PMDesignator 中定义的 AM/PM 指示项(如果存在)。


z 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数没有前导零。例如,太平洋标准时间是“-8”。


zz 时区偏移量(“+”或“-”后面仅跟小时)。一位数的小时数有前导零。例如,太平洋标准时间是“-08”。


zzz 完整时区偏移量(“+”或“-”后面跟有小时和分钟)。一位数的小时数和分钟数有前导零。例如,太平洋标准时间是“-08:00”。


: 在 TimeSeparator 中定义的默认时间分隔符。


/ 在 DateSeparator 中定义的默认日期分隔符。


% c 其中 c 是格式模式(如果单独使用)。如果格式模式与原义字符或其他格式模式合并,则可以省略“%”字符。


\ c 其中 c 是任意字符。照原义显示字符。若要显示反斜杠字符,请使用“\\”。


只有上面第二个表中列出的格式模式才能用于创建自定义模式;在第一个表中列出的标准格式字符不能用于创建自定义模式。


自定义模式的长度至少为两个字符;例如,


1. DateTime.ToString( "d"); // 返回 DateTime 值;“d”是标准短日期模式。


2. DateTime.ToString( "%d"); // 返回月中的某天;“%d”是自定义模式。


3. DateTime.ToString( "d "); // 返回后面跟有一个空白字符的月中的某天;“d”是自定义模式。


三、C# 常用日期处理函数


 // 计算2个日期之间的天数差


DateTime dt1 = Convert.DateTime("2007-8-1"); 
 DateTime dt2 = Convert.DateTime("2007-8-15"); 
 TimeSpan span = dt2.Subtract(dt1); 
int dayDiff = span.Days + 1; 
  // 计算某年某月的天数


int days = DateTime.DaysInMonth(2007, 8); 
days = 31; 
 // 给日期增加一天、减少一天


DateTime dt =DateTime.Now; 
dt.AddDays(1); //增加一天 
dt.AddDays(-1);//减少一天 
*注:以下的毫秒都采用最大997,而不是999 因为SQL SERVER的精度为3毫秒


//本月的天数


int daysInMonth = DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month);


//本年的天数 是否是闰年           


int daysInYear = DateTime.IsLeapYear(DateTime.Now.Year) ? 366 : 365;


//本月第一天


DateTime firstDayInMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);


//本月的最后一天 本月1号加一个月得下月1号,再剪掉一天就是本月最后一天


DateTime lastDayInMonth = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddDays(-1);


//本月最后一天的午夜


DateTime lastDayInMonth2 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(1).AddMilliseconds(-3);


//本年第一天


DateTime firstDayInYear = new DateTime(DateTime.Now.Year, 1, 1);


//本年最后一天


DateTime lastDayInYear = new DateTime(DateTime.Now.Year, 12, 31);


//本年最后一天的午夜


DateTime lastDayInYear2 = new DateTime(DateTime.Now.Year, 12, 31, 23, 59, 59, 997);


//得到星期几 星期天为7


int dayOfWeek = Convert.ToInt32(DateTime.Now.DayOfWeek) < 1 ? 7 : Convert.ToInt32(DateTime.Now.DayOfWeek);


//本周一


DateTime monday = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day).AddDays(1 - dayOfWeek);


//本周 星期天


DateTime sunday = monday.AddDays(6);


//本周 星期天的午夜


DateTime sunday2 = monday.AddDays(7).AddMilliseconds(-3);


//本季度第一天


DateTime firsyDayInQuarter = new DateTime(DateTime.Now.Year, DateTime.Now.Month - (DateTime.Now.Month - 1) % 3, 1);


//本季度最后一天


DateTime lastDayInQuarter = firsyDayInQuarter.AddMonths(3).AddDays(-1);


//本季度最后一天的午夜


DateTime lastDayInQuarter2 = firsyDayInQuarter.AddMonths(3).AddMilliseconds(-3);


string datetime = "10-Jul-09";


string newdatetime = DateTime.Parse(datetime, new System.Globalization.CultureInfo("de-de")).ToString("yyyyMMdd");


四、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/hf556/archive/2011/02/28/csdilltimer.html

原创粉丝点击