萬盛學電腦網

 萬盛學電腦網 >> 腳本專題 >> javascript >> 如何使用Javascript獲取距今n天前的日期

如何使用Javascript獲取距今n天前的日期

本篇文章是對使用Javascript獲取距今n天前日期的實現代碼進行了詳細的分析介紹,需要的朋友參考下   復制代碼 代碼如下:


 function Test(day) {
                var today = new Date();
                var beforMilliseconds = today.getTime() - 1000 * 3600 * 24 * day;
                var beforday = new Date();
                beforday.setTime(beforMilliseconds);
                var strYear = beforday.getFullYear();
                var strDay = beforday.getDate();
                var strMonth = beforday.getMonth() + 1;
                if (strMonth < 10) {
                    strMonth = "0" + strMonth;
                }
                var strYesterday = strYear + "-" + strMonth + "-" + strDay;
                alert(strYesterday);
            }

copyright © 萬盛學電腦網 all rights reserved