datetime dt = datetime.now; //当前时间
datetime startweek = dt.adddays(1 - convert.toint32(dt.dayofweek.tostring("d"))); //本周周一
datetime endweek = startweek.adddays(6); //本周周日
datetime startmonth = dt.adddays(1 - dt.day); //本月月初
datetime endmonth = startmonth.addmonths(1).adddays(-1); //本月月末
//datetime endmonth = startmonth.adddays((dt.addmonths(1) - dt).days - 1); //本月月末
datetime startquarter = dt.addmonths(0 - (dt.month - 1) % 3).adddays(1 - dt.day); //本季度初
datetime endquarter = startquarter.addmonths(3).adddays(-1); //本季度末
datetime startyear = new datetime(dt.year, 1, 1); //本年年初
datetime endyear = new datetime(dt.year, 12, 31); //本年年末
#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
日期转换成字符型
string t = row["time"].tostring()
string time = datetime.parse(t).tostring("yyyy-mm-dd");
convert.todatetime( "07-05-2007 ").tostring( "yyyy-mm-dd ") 这句代码转换后是string类型