JS获取本周周一,周末及获取任意时间的周一周末功能示例

发布时间:2017-03-14 13:54 编辑:站点网

本文实例讲述了JS获取本周周一,周末及获取任意时间的周一周末功能。分享给大家供大家参考,具体如下:

项目需要获取本周及任意一天的周一及周末 需格式化,示例代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>星期</title>
<script type="text/javascript" language="javascript" >
/**
 * @author zhuyangxing
 */
(function() {
  function util_date() {
    var _today=new Date();
    this.today=_today;
    this.year=_today.getYear()+1900;//当前年份
    this.Month_a=_today.getMonth();
    this.Month=this.Month_a+1;//当前月份
    this.day=_today.getDate();//当前日期
    this.date=_today.getDay()==0?7:_today.getDay();//本周第几天 因系统会把周日作为第0天
    this.Monday="";
    this.Sunday="";
    this.day_one="";
  }
  Date.prototype.pattern=function(fmt) {
    var o = {
    "M+" : this.getMonth()+1, //月份
    "d+" : this.getDate(), //日
    "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
    "H+" : this.getHours(), //小时
    "m+" : this.getMinutes(), //分
    "s+" : this.getSeconds(), //秒
    "q+" : Math.floor((this.getMonth()+3)/3), //季度
    "S" : this.getMilliseconds() //毫秒
    };
    var week = {
    "0" : "/u65e5",
    "1" : "/u4e00",
    "2" : "/u4e8c",
    "3" : "/u4e09",
    "4" : "/u56db",
    "5" : "/u4e94",
    "6" : "/u516d"
    };
    if(/(y+)/.test(fmt)){
      fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    if(/(E+)/.test(fmt)){
      fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);
    }
    for(var k in o){
      if(new RegExp("("+ k +")").test(fmt)){
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
      }
    }
    return fmt;
  },
  util_date.prototype = {
      newToday : function(_today){
        this.today=_today;
        this.year=_today.getYear()+1900;//当前年份
        this.Month_a=_today.getMonth();
        this.Month=this.Month_a+1;//当前月份
        this.day=_today.getDate();//当前日期
        this.date=_today.getDay()==0?7:_today.getDay();//本周第几天 因系统会把周日作为第0天
        this.Monday="";
        this.Sunday="";
        this.day_one="";
      },
      getMonday:function(){
        if(this.Monday.length!=0){
          return this.Monday;
        }else{
          var _monday = new Date(this.year,this.Month_a,this.day-this.date+1);
          this.Monday = _monday;
          return _monday;
        }
      },
      getSunday:function(){
        if(this.Sunday.length!=0){
          return this.Sunday;
        }else{
          var _Sunday = new Date(this.year,this.Month_a,this.day-this.date+7);
          this.Sunday = _Sunday;
          return _Sunday;
        }
      },
      getPreviousMonday:function(Monday){
          var _monday = new Date(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()-7);
          return _monday;
      },
      getPreviousSunday:function(Monday){
          var _Sunday = new Date(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()-1);
          this.Sunday = _Sunday;
          return _Sunday;
      },
      getNextMonday:function(Monday){
        var _monday = new Date(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()+7);
        return _monday;
      },
      getNextSunday:function(Monday){
        var _Sunday = new Date(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()+13);
        this.Sunday = _Sunday;
        return _Sunday;
      }
  };
  window.util_date = new util_date();
})();
document.write(window.util_date.getMonday().pattern("yyyy-MM-dd"));
</script>
</head>
<body >
</body>
</html>

                
更多相关内容:
    无相关信息