项目中用到的js日期函数

<script type="text/javascript">
    //替换字符串 
    function Replace(str, from, to) {
        return str.split(from).join(to);
    }
    // 日期类型格式成指定的字符串
    function FormatDate(date, format) {
        format = Replace(format, "yyyy", date.getFullYear());
        format = Replace(format, "MM", GetFullMonth(date));
        format = Replace(format, "dd", GetFullDate(date));
        format = Replace(format, "HH", GetFullHour(date));
        return format;
    }
    //js日期字符串转换成日期类型
    function parseDate(dateStr) {
        return new Date(Replace(dateStr, "-", "/"));
    }
    //增加月 
    function AddMonths(date, value) {
        date.setMonth(date.getMonth() + value);
        return date;
    }
    //增加天 
    function AddDays(date, value) {
        date.setDate(date.getDate() + value);
        return date;
    }
    //增加时
    function AddHours(date, value) {
        date.setHours(date.getHours() + value);
        return date;
    }
    //返回月份(两位数) 
    function GetFullMonth(date) {
        var v = date.getMonth() + 1;
        if (v > 9) return v.toString();
        return "0" + v;
    }
 
    //返回日(两位数) 
    function GetFullDate(date) {
        var v = date.getDate();
        if (v > 9) return v.toString();
        return "0" + v;
    }
    //返回时(两位数)
    function GetFullHour(date) {
        var v = date.getHours();
        if (v > 9) return v.toString();
        return "0" + v;
    }
    //比较两个时间
    function compareDate() {
        var mydate = AddDays(parseDate("2012-08-23"), 1);
        var nowdate = new Date();
        if (nowdate.getTime() < mydate.getTime()) {
            return FormatDate(nowdate, "yyyy-MM-dd");
        }
        return FormatDate(mydate, "yyyy-MM-dd");
    }
</script>

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