六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 48|回复: 0

javascript获取指定时间函数

[复制链接]

升级  15.33%

19

主题

19

主题

19

主题

秀才

Rank: 2

积分
73
 楼主| 发表于 2013-1-29 09:31:24 | 显示全部楼层 |阅读模式
function WeekDate() {   
  
    /**  
     * 基准时间,所有计算以此为基础  
     */  
    var _calcDate = new Date();   
  
    /**  
     * 一天的豪秒数  
     */  
    var _day = 1000 * 60 * 60 * 24;   
  
    this.getThisWeekDate = getThisWeekDate;   
    this.getPrevWeekDate = getPrevWeekDate;   
    this.getNextWeekDate = getNextWeekDate;   
    this.wrapDate = wrapDate;   
  
    this.getDayMillisecond = getDayMillisecond;   
  
    /**  
     * 取上周开始至上周结束日期  
     *  
     * @return Array [0]上周第一天 [1]上周最后一天  
     */  
    function getPrevWeekDate() {   
        // 取上周结束日期   
        var lastDay = new Date(_calcDate - (_calcDate.getDay()) * _day);   
        // 取上周开始日期   
        var firstDay = new Date((lastDay * 1) - 6 * _day);   
        // 更新基准时间   
        _calcDate = firstDay;   
  
        return [wrapDate(firstDay), wrapDate(lastDay)];   
    }   
  
    /**  
     * 取下周开始至下周结束日期  
     *  
     * @return Array [0]上周第一天 [1]上周最后一天  
     */  
    function getNextWeekDate() {   
        // 取下周开始日期   
        var firstDay = new Date((_calcDate * 1) + (6 - _calcDate.getDay() + 2) * _day);   
        // 取下周结束日期   
        var lastDay = new Date((firstDay * 1) + 6 * _day);   
        // 更新基准时间           
        _calcDate = firstDay;   
  
        return [wrapDate(firstDay), wrapDate(lastDay)];   
    }   
   
    function getNextWeekFirstDay(){
    var next_week=getNextWeekDate()[0]+"";
    var next_week_day=next_week.split("-");
    //alert(next_week_day);
    return Number(next_week_day[2]);
    }
    /**  
     * 取本周开始至本周结束日期  
     *  
     * @return Array [0]本周第一天 [1]本周最后一天  
     */  
    function getThisWeekDate() {   
        _calcDate = new Date();   
        // 第一天日期   
        var firstDay = new Date(_calcDate - (_calcDate.getDay() - 1) * _day);   
        // 最后一天日期   
        var lastDay = new Date((firstDay * 1) + 6 * _day);   
  
        return [wrapDate(firstDay), wrapDate(lastDay)];   
    }   
  
    function wrapDate($date) {   
        var m = $date.getMonth() + 1;   
        m = m < 10 ? "0" + m : m;   
  
        var d = $date.getDate();   
        d = d < 10 ? "0" + d : d;   
  
        return $date.getFullYear() + "-" + m + "-" + d;               
    }   
  
    function getDayMillisecond() {   
        return _day;   
    }   
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表