JavaScript怎么获得某一天的前一天日期

JavaScript026

JavaScript怎么获得某一天的前一天日期,第1张

js中获取当前时间可以使用new Date()而要获取前一天的时间,可以使用当前时间减去一天的毫秒数,得到毫秒数之后然后再转换成时间。 即var yesterdsay = new Date(new Date().getTime() - 86400000)new Date().getTime()用户获取当前时间的毫.

function GetDay()   

    {   

        var   today=new   Date()      

        var   yesterday_milliseconds=today.getTime()-1000*60*60*24    

  

        var   yesterday=new   Date()      

        yesterday.setTime(yesterday_milliseconds)      

           

        var strYear=yesterday.getFullYear() 

  

        var strDay=yesterday.getDate()   

        var strMonth=yesterday.getMonth()+1 

  

        if(strMonth<10)   

        {   

            strMonth="0"+strMonth   

        }   

        var strYesterday=strYear+"-"+strMonth+"-"+strDay   

        alert(strYesterday)   

    }

var today=new Date(2006,0,12) //月份为0-11

var yesterday_milliseconds=today.getTime()-1000*60*60*24

var yesterday=new Date()

yesterday.setTime(yesterday_milliseconds)

var strYear=yesterday.getFullYear()

var strDay=yesterday.getDate()

var strMonth=yesterday.getMonth()+1

if(strMonth<10)

{

strMonth="0"+strMonth

}

var strYesterday=strYear+"-"+strMonth+"-"+strDay

alert(strYesterday)