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

<script type="text/javascript">
        function getLastDate() {
            var date = new Date();
            var month = date.getMonth() + 1;
            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 - 10;
            if (day <= 0) {
                day = mygetday(day, month, year);
                var months = mygetmonth(month);
                ym = months.split(",");
                year1 = ym[0];
                day = FormatDate(day);
                month = FormatDate(ym[1]);
                return year1 + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
            }
            else {
                return year + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
            }

        }

        // 获取某一天的日期
        function mygetday(day, month, year) {
            var daynum = 30;
            if (month == 2 || month == 4 || month == 6 || month == 8 || month == 9
            || month == 1 || month == 11) {
                daynum = 31;
            } else if (month == 3) {
                if (year % 4 == 0) {
                    daynum = 29;
                } else {
                    daynum = 28;
                }
            }
            day = daynum + day;
            return day;
        }
        // 获取某一天的月份
        function mygetmonth(month) {
            var year = new Date().getFullYear();
            if (month == 1) {
                month = 12;
                year = year - 1;
            } else {
                month = month - 1;
            }
            return year + "," + month;
        }
        function FormatDate(d) {
            if (d.toString().length == 1) {
                d = "0" + d;
            }
            return d;
        }
        $(function () {
            $(#birthday).datetimebox(setValue, getLastDate());
        });

    </script>


 <script type="text/javascript">
        function getLastDate() {
            var date = new Date(); //获取当前时间
            var month = date.getMonth() + 1;
            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 - 10; //获取十天前的时间
            if (day <= 0) {
                day = mygetday(day, month, year);
                var months = mygetmonth(month);
                ym = months.split(",");
                year1 = ym[0];
                day = FormatDate(day);
                month = FormatDate(ym[1]);
                return year1 + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
            }
            else {
                return year + "-" + month + "-" + day + "&nbsp;" + hh + ":" + mm + ":" + ss;
            }

        }

        // 获取某一天的日期
        function mygetday(day, month, year) {
            var daynum = 30;
            if (month == 2 || month == 4 || month == 6 || month == 8 || month == 9
            || month == 1 || month == 11) {
                daynum = 31;
            } else if (month == 3) {
                if (year % 4 == 0) {
                    daynum = 29;
                } else {
                    daynum = 28;
                }
            }
            day = daynum + day;
            return day;
        }
        // 获取某一天的月份
        function mygetmonth(month) {
            var year = new Date().getFullYear();
            if (month == 1) {
                month = 12;
                year = year - 1;
            } else {
                month = month - 1;
            }
            return year + "," + month;
        }
        function FormatDate(d) {
            if (d.toString().length == 1) {
                d = "0" + d;
            }
            return d;
        }
//测试填充文本框
        $(function () {
            $(#birthday).datetimebox(setValue, getLastDate());
        });

    </script>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。