js 获取据当前时间n天前的时间

时间:2022-05-01 06:48:59
<script type="text/javascript">
function getLastDate() {
var date = new Date();
var month = date.getMonth() + ;
var day = date.getDate();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
var year = date.getFullYear();
alert(year); var ym;
var year1;
day = day - ;
if (day <= ) {
day = mygetday(day, month, year);
var months = mygetmonth(month);
ym = months.split(",");
year1 = ym[];
day = FormatDate(day);
month = FormatDate(ym[]);
return year1 + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
}
else {
return year + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
} } // 获取某一天的日期
function mygetday(day, month, year) {
var daynum = ;
if (month == || month == || month == || month == || month ==
|| month == || month == ) {
daynum = ;
} else if (month == ) {
if (year % == ) {
daynum = ;
} else {
daynum = ;
}
}
day = daynum + day;
return day;
}
// 获取某一天的月份
function mygetmonth(month) {
var year = new Date().getFullYear();
if (month == ) {
month = ;
year = year - ;
} else {
month = month - ;
}
return year + "," + month;
}
function FormatDate(d) {
if (d.toString().length == ) {
d = "" + d;
}
return d;
}
$(function () {
$('#birthday').datetimebox('setValue', getLastDate());
}); </script> <script type="text/javascript">
function getLastDate() {
var date = new Date(); //获取当前时间
var month = date.getMonth() + ;
var day = date.getDate();
var hh = date.getHours();
var mm = date.getMinutes();
var ss = date.getSeconds();
var year = date.getFullYear();
alert(year); var ym;
var year1;
day = day - ; //获取十天前的时间
if (day <= ) {
day = mygetday(day, month, year);
var months = mygetmonth(month);
ym = months.split(",");
year1 = ym[];
day = FormatDate(day);
month = FormatDate(ym[]);
return year1 + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
}
else {
return year + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
} } // 获取某一天的日期
function mygetday(day, month, year) {
var daynum = ;
if (month == || month == || month == || month == || month ==
|| month == || month == ) {
daynum = ;
} else if (month == ) {
if (year % == ) {
daynum = ;
} else {
daynum = ;
}
}
day = daynum + day;
return day;
}
// 获取某一天的月份
function mygetmonth(month) {
var year = new Date().getFullYear();
if (month == ) {
month = ;
year = year - ;
} else {
month = month - ;
}
return year + "," + month;
}
function FormatDate(d) {
if (d.toString().length == ) {
d = "" + d;
}
return d;
}
//测试填充文本框
$(function () {
$('#birthday').datetimebox('setValue', getLastDate());
}); </script>