js获取本周开始时间和结束时间|JS获取本周周一,周末及获取任意时间的周一周末功能示例

更新时间:2021-06-16    来源:js教程    手机版     字体:

【www.bbyears.com--js教程】

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

 代码如下

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

星期

<script type="text/javascript"language="javascript">

/**

 * @author zhuyangxing

 */

(function() {

  functionutil_date() {

    var_today=newDate();

    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) {

    varo = {

    "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()//毫秒

    };

    varweek = {

    "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(varkino){

      if(newRegExp("("+ k +")").test(fmt)){

        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));

      }

    }

    returnfmt;

  },

  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){

          returnthis.Monday;

        }else{

          var_monday =newDate(this.year,this.Month_a,this.day-this.date+1);

          this.Monday = _monday;

          return_monday;

        }

      },

      getSunday:function(){

        if(this.Sunday.length!=0){

          returnthis.Sunday;

        }else{

          var_Sunday =newDate(this.year,this.Month_a,this.day-this.date+7);

          this.Sunday = _Sunday;

          return_Sunday;

        }

      },

      getPreviousMonday:function(Monday){

          var_monday =newDate(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()-7);

          return_monday;

      },

      getPreviousSunday:function(Monday){

          var_Sunday =newDate(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()-1);

          this.Sunday = _Sunday;

          return_Sunday;

      },

      getNextMonday:function(Monday){

        var_monday =newDate(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()+7);

        return_monday;

      },

      getNextSunday:function(Monday){

        var_Sunday =newDate(Monday.getYear()+1900,Monday.getMonth(),Monday.getDate()+13);

        this.Sunday = _Sunday;

        return_Sunday;

      }

  };

  window.util_date =newutil_date();

})();

document.write(window.util_date.getMonday().pattern("yyyy-MM-dd"));

</script>

如果需要可直接在项目中引入该文件 使用window.util_date.getMonday().pattern("yyyy-MM-dd");可获得2017-1-24类型的字符串

 代码如下 window.util_date.newToday("2017-1-1");设置当前日期

本文来源:http://www.bbyears.com/wangyezhizuo/124199.html

热门标签

更多>>

本类排行