JS how to set the time to calculate a few days ago?

Source: Internet
Author: User

  

Calculate how many days before the specific time. For example today is September 5, that 7 days ago Normal is August 29.

Before the direct use of time to add and subtract, eat a big loss, and then brain residual to directly write a very complicated calculation leap year, leap month, 30, 31, 28 months now share.

    function Initdatetime (day) {            var nowdate = new Date ()            var agodate = new Date (nowdate);            var nowday = Nowdate.getdate ();            var day =-day + nowday;            Agodate.setdate (day);            var nowmonth = nowdate.getmonth () + 1;            var nowyear = Nowdate.getfullyear ();            var agoday = Agodate.getdate ();            var agomonth = agodate.getmonth () + 1;            var agoyear = Agodate.getfullyear ();            var datefrom = (agoyear.tostring () + '-' + agomonth.tostring () + '-' + agoday.tostring ());            var Dateto = (nowyear.tostring () + '-' + nowmonth.tostring () + '-' + nowday);            $ (': text[name=startdate] '). Val (datefrom);    Start time            $ (': text[name=enddate] '). Val (Dateto);     End Time        }

Call:

Initdatetime (7)    //calculates the time 7 days ago.

JS How to set the time to calculate a few days ago?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.