1 //今天
2 DateTime.Now.Date.ToShortDateString();
3 //昨天,也就是今天的日期减一
4 DateTime.Now.AddDays(-1).ToShortDateString();
5 //明天,同理,加一
6 DateTime.Now.AddDays(1).ToShortDateString();
7 //本周(要知道本周第一天就得先知道今天是星期几,从而得知
8 本周第一天就是几天前的那一天;每一周是从周日始至周六止[0-6])
9 DateTime.Now.AddDays(Convert.ToDouble((0-Convert.ToInt16
10 (DateTime.Now.DayOfWeek)))).ToShortDateString();
11 //DateTime.Now.DayOfWeek获得今天是本周的第几天(0<=n<=6)
12 DateTime.Now.AddDays(Convert.ToDouble((6-Convert.ToInt16
13 (DateTime.Now.DayOfWeek)))).ToShortDateString();
14 //中文显示星期几
15 //由于DayOfWeek返回的是数字的星期几,需要把它转换成汉字以方便人们
16 阅读,有些人会用SWITCH来一个一个地对照,其实我们有更优的策略
17 string[] Day=new string[]{"星期日","星期一",星期二","星期三",
18 "星期四","星期五","星期六"};
19 Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];//使用它来获取
20 //上周,同理,一个周是7天,上周就是本周在减去7天,下周也是一样
21 DateTime.Now.AddDays(Convert.ToDouble((0-Convert.ToInt16(
22 DateTime.Now.DayOfWeek)))-7).ToShortDateString();
23 DateTime.Now.AddDays(Convert.ToDouble((6-Convert.ToInt16(
24 DateTime.Now.DayOfWeek)))-7).ToShortDateString();
25 //下周
26 DateTime.Now.AddDays(Convert.ToDouble((0-Convert.ToInt16(
27 Datetime.Now.DayOfWeek)))+7).ToShortDateString();
28 DateTime.Now.AddDays(Convert.ToDouble((6-Convert.ToInt16(
29 DateTime.Now.DayOfWeek)))+7).ToShortDateString();
30 //本月,本月的第一天是1号,最后一天就是下个月的1号再减一天
31 DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString(
32 )+"1";//第一天
33 DateTime.Parse(DateTime.Now.Year.ToString()+DateTime.Now.
34 Month.ToString()+"1").AddMonths(1).AddDays(-1).ToShortDateString();
35 //最后一天
36 //巧用C#里ToString的字符格式化更简便
37 DateTime.Now.ToString("yyyy-MM-01");
38 DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(1).AddDays
39 (-1).ToShortDateString();
40 //上个月,减去一个月份
41 DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(-1).ToShortDateString();
42 DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
43 //下个月,加上一个月份
44 DateTime.Parse(Datetime.Now.ToString("yyyy-MM-01")).AddMonths(1).ToShortDateString();
45 DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01")).AddMonths(2).AddDays(-1).ToShortString();
46 //7天后
47 DateTime.Now.Date.ToShortDateString();
48 DateTime.Now.AddDays(7).ToShortDateString();
49 //7天前
50 DateTime.Now.AddDays(-7).ToShortDateString();
51 DateTime.Now.Date.ToShortDateString();
52 //本年度,用ToString的字符格式化我们也很容易算出本年度的第一天和最后一天
53 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).ToShortDateString();
54 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).AddDays(-1).ToShortString();
55 //上年度
56 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(-1).ToShortDateString();
57 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddDays(-1).ToShortDateString();
58 //下年度
59 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(1).ToShortDateString();
60 DateTime.Parse(DateTime.Now.ToString("yyyy-01-01")).AddYears(2).AddDays(-1).ToShortDateString();
61 //本季度,很多人都会觉得这里是难点,需要写个长长的过程来判断;其实不用的,我们都知道一年四个季度,一个季度三个月
62 //首先我们把日期推到本季度第一个月,然后这个月的第一天就是本季度的第一天了
63 DateTime.Now.AddMonths(0-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01");
64 //同理,本季度的最后一天就是下个季度的第一天减一
65 DateTime.Parse.(DateTime.Now.AddMonth(3-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01")).AddDays(-1).ToShortDateString();
66 //下季度
67 DateTime.Now.AddMonths(3-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01");
68 DateTime.Parse(DateTime.Now.AddMonths(6-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01").AddDays(-1).ToShortDateString();
69 //上季度
70 DateTime.Now.AddMonths(-3-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01");
71 DateTime.Parse(DateTime.Now.AddMonths(0-((DateTime.Now.Month-1)%3)).ToString("yyyy-MM-01").AddDays(-1).ToShortDateString();
72 //获得当月有多少天
73 int m=System.DateTime.DaysInMonth(System.DateTime.Now.Year,System.DateTime.Now.Month);
74 //获取某月的实际工作日(即不包括周六日)
75 //int days=getDays(System.DateTime.Now);调用即可获得
76 private int getDays(System.DateTime date1)
77 {
78 int m=System.DateTime.DaysInMonth(date1.Year,date1.Month);
79 int mm=0;
80 for(int i=1;i<=m;i++)
81 {
82 System.DateTime date=Convert.ToDateTime(date1.Year+"-"+date1.Month+"-"+i);
83 switch(date.DayOfWeek)
84 {
85 case System.DayOfWeek.Monday:
86 case System.DayOfWeek.Tuessday:
87 case System.DayOfWeek.Wednesday:
88 case System.DayOfWeek.Thursday:
89 case System.DayOfWeek.Friday:
90 mm=mm+1;
91 break;
92 }
93 }
94 return mm;
95 }
96 //获得任意两日期之间的有效工作日(不包括周六日)
97 DateTime date1=Convert.ToDateTime("2008-8-8");
98 DateTime date2=Convert.ToDateTime("2008-10-1");
99 int days=getDays(date1,date2);
100 //调用如上即可获得
101 private int getDays(System.DateTime date1,System.DateTime date2)
102 {
103 string m=Microsoft.VisualBasic.DateAndTime.DateDiff(EnumDateCompare.day,date1,date2).ToString("f0");
104 int mm=0;
105 for(int i=0;i<=Convert.ToInt32(m);i++)
106 {
107 System.DateTime date=Convert.ToDateTime(date1.AddDays(i));
108 switch(date.DayOfWeek)
109 {
110 case System.DayOfWeek.Monday:
111 case System.DayOfWeek.Tuesday:
112 case System.DayOfWeek.Wednesday:
113 case System.DayOfWeek.Thursday:
114 case System.DayOfWeek.Friday:
115 mm=mm+1;
116 break;
117 }
118 }
119 return mm;
120 }
121 //格式输出
122 private void Page_Load(object sender,System.EventArgs e)
123 {
124 System.Globalization.DateTimeFormatInfo myDTFI=new System.Globalization.CultureInfo("en-US",false).DateTimeFormat;
125 //中国为zh-cn
126 DateTime myDT=System.DateTime.Now;
127 Response.Write(myDT.ToString("f",myDTFI));
128 //获得本周的周六和周日
129 public static void ConvertDateToWeek(DateTime date,out DateTime firstdate,out DateTime lastdate)
130 {
131 DateTime first System.DateTime.Now;
132 DateTime last=System.DateTime.Now;
133 switch(date.DayOfWeek)
134 {
135 case System.DayOfWeek.Monday:
136 first=date.AddDays(-1);
137 last=date.AddDays(5);
138 break;
139 case System.DayOfWeek.Tuesday:
140 first=date.AddDays(-2);
141 last=date.AddDays(4);
142 break;
143 case System.DayOfWeek.Wednesday:
144 first=date.AddDays(-3);
145 last=date.AddDays(3);
146 break;
147 case System.DayOfWeek.Thursday:
148 first=date.AddDays(-4);
149 last=date.AddDays(2);
150 break;
151 case System.DayOfWeek.Friday:
152 first=date.AddDays(-5);
153 last=date.AddDays(1);
154 break;
155 case System.DayOfWeek.Saturday:
156 first=dateAddDays(-6);
157 last=date;
158 break;
159 case System.DayOfWeek.Sunday:
160 first=date;
161 last=date.AddDays(6);
162 break;
163 }
164 firstdate=first;
165 lastdate=last;
166 }
167 //调用
168 DateTime firstdate=System.DateTime.Now;
169 DateTime lastdate=System.Datetime.Now;
170 ConvertDateToWeek(date,out firstdate,out lastdate);
171 //获得当前日期是该年度的第几周
172 DateTime dt=Convert.ToDateTime("2008-8-8");
173 int weeks=dt.DayOfYear/7+1;