在C#中获取前天的日期只需调用内部的方法即可
//计算前天
string strYersterday = DateTime.Now.AddDays(-2).ToString("yyyy-MM-dd");
在Flex中不知道有没有这种方法,自己用最原始的办法写了一个,没有判断是否有闰年的情况
//从时间格式里面提取昨天的年月日
private function getYearMonDay_Before(date:Date):String{
var strYMD:String;
var strMon:String;
var strDay:String;
if(date == null)
{
strYMD = "2020/12/31";
}
else
{
if((date.getMonth()+1)<10)
{
strMon = "0" + (date.getMonth()+1).toString();
if((date.getDate()-2)<10&&(date.getDate()-2)>0)
strDay = "0" + (date.getDate()-2).toString();
else if((date.getDate() - 2) == 0)
{
switch(strMon)
{
case "01":
strDay = "31";
strMon = "12";
break;
case "02":
strDay = "31";
strMon = "01";
break;
case "03":
strDay = "28";
strMon = "02";
break;
case "04":
strDay = "31";
strMon = "03";
break;
case "05":
strDay ="30";
strMon = "04";
break;
case "06":
strDay = "31";
strMon = "05";
break;
case "07":
strDay = "30";
strMon = "06";
break;
case "08":
strDay = "31";
strMon = "07";
break;
case "09":
strDay = "31";
strMon = "08";
break;
case "10":
strDay = "30";
strMon = "09";
break;
case "11":
strDay = "31";
strMon ="10";
break;
case "12":
strDay ="30";
strMon = "11";
break;
}
}
else if (date.getDate()-2 < 0)
{
switch(strMon)
{
case "01":
strDay = "30";
strMon = "12";
break;
case "02":
strDay = "30";
strMon = "01";
break;
case "03":
strDay = "27";
strMon = "02";
break;
case "04":
strDay = "30";
strMon = "03";
break;
case "05":
strDay ="29";
strMon = "04";
break;
case "06":
strDay = "30";
strMon = "05";
break;
case "07":
strDay = "29";
strMon = "06";
break;
case "08":
strDay = "30";
strMon = "07";
break;
case "09":
strDay = "30";
strMon = "08";
break;
case "10":
strDay = "29";
strMon = "09";
break;
case "11":
strDay = "30";
strMon ="10";
break;
case "12":
strDay ="29";
strMon = "11";
break;
}
}
else
strDay = (date.getDate()-2).toString();
}
else
{
strMon = (date.getMonth()+1).toString();
if((date.getDate()-2)<10&&(date.getDate()-2)>0)
strDay = "0" + (date.getDate()-2).toString();
else if((date.getDate() - 2) == 0)
{
switch(strMon)
{
case "01":
strDay = "31";
strMon = "12";
break;
case "02":
strDay = "31";
strMon = "01";
break;
case "03":
strDay = "28";
strMon = "02";
break;
case "04":
strDay = "31";
strMon = "03";
break;
case "05":
strDay ="30";
strMon = "04";
break;
case "06":
strDay = "31";
strMon = "05";
break;
case "07":
strDay = "30";
strMon = "06";
break;
case "08":
strDay = "31";
strMon = "07";
break;
case "09":
strDay = "31";
strMon = "08";
break;
case "10":
strDay = "30";
strMon = "09";
break;
case "11":
strDay = "31";
strMon ="10";
break;
case "12":
strDay ="30";
strMon = "11";
break;
}
}
else if (date.getDate()-2 < 0)
{
switch(strMon)
{
case "01":
strDay = "30";
strMon = "12";
break;
case "02":
strDay = "30";
strMon = "01";
break;
case "03":
strDay = "27";
strMon = "02";
break;
case "04":
strDay = "30";
strMon = "03";
break;
case "05":
strDay ="29";
strMon = "04";
break;
case "06":
strDay = "30";
strMon = "05";
break;
case "07":
strDay = "29";
strMon = "06";
break;
case "08":
strDay = "30";
strMon = "07";
break;
case "09":
strDay = "30";
strMon = "08";
break;
case "10":
strDay = "29";
strMon = "09";
break;
case "11":
strDay = "30";
strMon ="10";
break;
case "12":
strDay ="29";
strMon = "11";
break;
}
}
else
strDay = (date.getDate()-2).toString();
}
strYMD = date.getFullYear().toString()+"/"+strMon+"/"+
strDay;
}
return strYMD;
}