#region 得到一周的周一和周日的日期 /// 计算本周的周一日期 public static DateTime GetMondayDate() { return GetMondayDate(DateTime.Now); } /// 计算本周周日的日期 public static DateTime GetSundayDate() { return GetSundayDate(DateTime.Now); } /// 计算某日起始日期(礼拜一的日期) /// 该周中任意一天 /// 返回礼拜一日期,后面的具体时、分、秒和传入值相等 public static DateTime GetMondayDate(DateTime someDate) { int i = someDate.DayOfWeek - DayOfWeek.Monday; if (i == -1) i = 6;// i值 > = 0 ,因为枚举原因,Sunday排在最前,此时Sunday-Monday=-1,必须+7=6。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Subtract(ts); } /// 计算某日结束日期(礼拜日的日期) /// 该周中任意一天 /// 返回礼拜日日期,后面的具体时、分、秒和传入值相等 public static DateTime GetSundayDate(DateTime someDate) { int i = someDate.DayOfWeek - DayOfWeek.Sunday; if (i != 0) i = 7 - i;// 因为枚举原因,Sunday排在最前,相减间隔要被7减。 TimeSpan ts = new TimeSpan(i, 0, 0, 0); return someDate.Add(ts); } #endregion
当月第一天和当月最后一天
DateTime now = DateTime.Now; DateTime dt1 = new DateTime(now.Year, now.Month, 1); DateTime dt2 = dt1.AddMonths(1).AddDays(-1);
给日期添加或减少日期
方式一:
var b = new Date(); var c = new Date(b.getFullYear(), b.getMonth(), b.getDate()); alert(c.getFullYear() + "-" + (c.getMonth()) + "-" + (c.getDate() - 6));
方式二:
//开始时间
var begin;
//结束时间
var end;
var b = new Date();
var c = new Date(b.getFullYear(), b.getMonth(), b.getDate());
alert(day)
if (day == 3) {
//开始时间
begin = c.getFullYear() + "-" + (c.getMonth() + 1) + "-" + (c.getDate() - 2);
//结束时间
end = c.getFullYear() + "-" + (c.getMonth() + 1) + "-" + (c.getDate());
}
if (day==7) {
//开始时间
begin = c.getFullYear() + "-" + (c.getMonth() + 1) + "-" + (c.getDate() - 6);
//结束时间
end = c.getFullYear() + "-" + (c.getMonth() + 1) + "-" + (c.getDate());
}
if (day==30) {
//开始时间
begin = c.getFullYear() + "-" + (c.getMonth() + 1) + "-" + (c.getDate() - 29);
//结束时间
end = c.getFullYear() + "-" + (c.getMonth() + 1) + "-" + (c.getDate());
}
如果上面的方法不能满足需求那么这个方法应该可以:
方式三:
function getthedate(dd, dadd) { //可以加上错误处理 var a = new Date(dd) a = a.valueOf() a = a + dadd * 24 * 60 * 60 * 1000 a = new Date(a); var m = a.getMonth(); if (m.toString().length == 1) { m = '0' + m; } var d = a.getDate(); if (d.toString().length == 1) { d = '0' + d; } return a.getFullYear() + "-" + m + "-" + d; }