JS 本周-本月

<html>
<head>
<script type="text/javascript">
    function getWeekStartDate() {
        var now = new Date();
        var start = new Date();
        var n = now.getDay();
        if (n == 0) {n = 7; }
        start.setDate(now.getDate() - n + 1);
        return new Date(start.getFullYear(), start.getMonth(), start.getDate());
    }
    function getWeekEndDate() {
        var now = new Date();
        var end = new Date();
        var n = now.getDay();
        if (n == 0) { n = 7; }
        end.setDate(now.getDate() - n + 7);
        return new Date(end.getFullYear(), end.getMonth(), end.getDate());
    }
    function getMonthStartDate() {
        var now = new Date();
        return new Date(now.getFullYear(),now.getMonth(),1);
    }
    function getMonthEndDate() {
        var now = new Date();
        var m_year = now.getFullYear();
        var m_month = now.getMonth();
        var lastMonth = new Date(m_year,m_month + 1, 1);
        var currentMonth = getMonthStartDate();
        var n = (lastMonth - currentMonth)/(1000*60*60*24);
        var end = new Date(m_year, m_month, n);
        return end;
    }
    function Time_DayType() {
        var now = new Date();
        Time_Display(now, now);
    }
    function Time_YesterdayType() {
        var now = new Date();
        now.setDate(now.getDate() - 1);
        Time_Display(now, now);
    }
    function Time_WeekType() {
        Time_Display(getWeekStartDate(), getWeekEndDate());
    }
    function Time_MonthType() {
        Time_Display(getMonthStartDate(),getMonthEndDate());
    }
    function Time_Display(m_start, m_end) {
        start = m_start.getFullYear() + "-" + (m_start.getMonth() + 1) + "-" + m_start.getDate();
        end = m_end.getFullYear() + "-" + (m_end.getMonth() + 1) + "-" + m_end.getDate();
        //
        alert("时间开始于:" + start + ",结束于" + end);
    }
</script>
</head>
<body>
<input type="button" οnclick="Time_DayType();" value="今日按钮" name="今日按钮">
<input type="button" οnclick="Time_YesterdayType();" value="昨日按钮" name="昨日按钮">
<input type="button" οnclick="Time_WeekType();" value="本周按钮" name="本周按钮">
<input type="button" οnclick="Time_MonthType();" value="本月按钮" name="本月按钮">
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值