当前位置:主页 > 网页前端 > JavaScript代码 >

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

时间:2022-11-17 10:15:31 | 栏目:JavaScript代码 | 点击:

复制代码 代码如下:

 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);
            }

您可能感兴趣的文章:

相关文章