C#中的日期处理函数
//2007年4月24日
this.TextBox6.Text = System.DateTime.Now.ToString("D");
//2007-4-24
this.TextBox7.Text = System.DateTime.Now.ToString("d");
//2007年4月24日 16:30:15
this.TextBox8.Text = System.DateTime.Now.ToString("F");
//2007年4月24日 16:30
this.TextBox9.Text = System.DateTime.Now.ToString("f");
//2007-4-24 16:30:15
this.TextBox10.Text = System.DateTime.Now.ToString("G");
//2007-4-24 16:30
this.TextBox11.Text = System.DateTime.Now.ToString("g");
//16:30:15
this.TextBox12.Text = System.DateTime.Now.ToString("T");
//16:30
this.TextBox13.Text = System.DateTime.Now.ToString("t");
//2007年4月24日 8:30:15
this.TextBox14.Text = System.DateTime.Now.ToString("U");
//2007-04-24 16:30:15Z
this.TextBox15.Text = System.DateTime.Now.ToString("u");
//4月24日
this.TextBox16.Text = System.DateTime.Now.ToString("m");
this.TextBox16.Text = System.DateTime.Now.ToString("M");
//Tue, 24 Apr 2007 16:30:15 GMT
this.TextBox17.Text = System.DateTime.Now.ToString("r");
this.TextBox17.Text = System.DateTime.Now.ToString("R");
//2007年4月
this.TextBox19.Text = System.DateTime.Now.ToString("y");
this.TextBox19.Text = System.DateTime.Now.ToString("Y");
//2007-04-24T15:52:19.1562500+08:00
this.TextBox20.Text = System.DateTime.Now.ToString("o");
this.TextBox20.Text = System.DateTime.Now.ToString("O");
//2007-04-24T16:30:15
this.TextBox18.Text = System.DateTime.Now.ToString("s");
//2007-04-24 15:52:19
this.TextBox21.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:ffff");
//2007年04月24 15时56分48秒
this.TextBox22.Text = System.DateTime.Now.ToString("yyyy年MM月dd HH时mm分ss秒");
//星期二, 四月 24 2007
this.TextBox1.Text = System.DateTime.Now.ToString("dddd, MMMM dd yyyy");
//二, 四月 24 '07
this.TextBox2.Text = System.DateTime.Now.ToString("ddd, MMM d \"'\"yy");
//星期二, 四月 24
this.TextBox3.Text = System.DateTime.Now.ToString("dddd, MMMM dd");
//4-07
this.TextBox4.Text = System.DateTime.Now.ToString("M/yy");
//24-04-07
this.TextBox5.Text = System.DateTime.Now.ToString("dd-MM-yy");
字符型转换转为字符串
12345.ToString("n"); //生成 12,345.00
12345.ToString("C"); //生成 ¥12,345.00
12345.ToString("e"); //生成 1.234500e+004
12345.ToString("f4"); //生成 12345.0000
12345.ToString("x"); //生成 3039 (16进制)
12345.ToString("p"); //生成 1,234,500
时间加减计算
DateTime dt = new DateTime(System.DateTime.Today.Year, System.DateTime.Today.Month, System.DateTime.Today.Day); //获取当前时间
DateTime dt = new DateTime(2010, 10, 17); //一个月中的哪一天
Console.WriteLine("tht day of {0},is {1}", dt.Date, dt.DayOfWeek); //增加月份
dt = dt.AddMonths(2);
Console.WriteLine("day light saving {0};{1}",
dt.IsDaylightSavingTime(),dt.Month); //构造函数接受小时,分钟,秒
TimeSpan ts = new TimeSpan(4, 30, 0);
Console.WriteLine(ts); //从当前减去15分钟
Console.WriteLine(ts.Subtract(new TimeSpan(0,15,0)));
当月第一天0时0分0秒:
DateTime.Now.AddDays(1 - DateTime.Now.Day).Date
当月最后一天23时59分59秒:
DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1)
上个月的第一天,
var _today=DateTime.Now.AddMonths(-1);
var startTM= _today.AddDays(1 - _today.Day).Date;
上个月的最后一天
var endTM=_today.AddDays(1 - _today.Day).Date.AddMonths(1).AddSeconds(-1);
N个月的第一天,
var totalMonth=n;//(n可以为正数也可以为负数,0为当前月,不带负号)
var _today=DateTime.Now.AddMonths(totalMonth);
var startTM= _today.AddDays(1 - _today.Day).Date;
N个月的最后一天,
var endTM=_today.AddDays(1 - _today.Day).Date.AddMonths(1).AddSeconds(-1);
//C#获取上个月第一天和最后一天
//上个月第一天0时0分0秒:
DateTime.Now.AddMonths(-1).Date.AddDays(1 - DateTime.Now.Day);
//上个月最后一天23时59分59秒:
DateTime.Parse(DateTime.Now.AddDays(1 - DateTime.Now.Day).ToShortDateString()).AddSeconds(-1).ToString();
//当天0时0分0秒:
DateTime start = Convert.ToDateTime(DateTime.Now.ToString("D").ToString());
//当天23时59分59秒:
DateTime end = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("D").ToString()).AddSeconds(-1);