例子,js日期操作。
复制代码 代码示例:
/****************************************
* 取前一天的日期
* 参数格式:2014-06-02
* 结果格式:2014-06-01
* 整理:www.jb200.com
***************************************/
function convertDate(beginTime)
{
var now = new Date(beginTime);
var years = now.getFullYear();
var months = now.getMonth()+1;
var days = now.getDate();
var NextNow = addDate(years+"-"+months+"-"+days,-1);
years = NextNow.getFullYear();
months = NextNow.getMonth()+1;
days = NextNow.getDate();
if((months+"").length==1)
{
months = "0" + months;
}
if((days+"").length==1)
{
days = "0" + days;
}
return years+"-"+months+"-"+days;
}
/*****************************************
* 根据 日期,偏移量 获取新的日期
* 参数dd的格式:2014-06-01
****************************************/
function addDate(dd,dadd)
{
var arr = dd.split("-");
var timestr = "";
for(var i=0;i<arr.length;i++)
{
if(timestr.length>0)
{
timestr = timestr + "-";
}
if(arr[i].length==2 || arr[i].length==4)
{
timestr = timestr + arr[i];
}
if(arr[i].length==1)
{
timestr = timestr + "0" + arr[i];
}
}
var a = new Date(timestr);
a = a.valueOf();
a = a + dadd * 24 * 60 * 60 * 1000;
a = new Date(a);
return a;
}