Javascript is calculated one day before and one day after

1, page layout

   <button onclick="before()">上一天</button>
    <button onclick="after()">下一天</button>
    <div id="date">
    </div>

2, to instantiate Date

var date=new Date();

3, write the page

var EL = document.getElementById ( ' DATE ' ) // find the element ID 
el.innerHTML = myGetDate () // render page 
// this is a function of the package, to make them easier 
function myGetDate () { 
    return `$ {DATE. getFullYear ()} {date.getMonth of $ () + 1 } January $ {date.getDate ()} `day 
}

4, calculation of a day

function before () { 
    DATE = new new a Date (); // every call re-instantiate a Date 
    date.setTime (date.getTime () - (1000 * 60 * 60 * 24 )) // Get the current time stamp minus one day milliseconds number of milliseconds 
    el.innerHTML = myGetDate () // call the function written above layout, rendering pages 
}

5, calculates the next day

function after(){
    date=new Date();
    date.setTime(date.getTime()+(1000*60*60*24))
    el.innerHTML=myGetDate()
}

  On one day and the next day is calculated Similarly, simply add it into Save

Guess you like

Origin www.cnblogs.com/linxiaoran/p/12055813.html