Home >Web Front-end >JS Tutorial >How to initialize js date range to get the date of the previous month_javascript skills

How to initialize js date range to get the date of the previous month_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:01:061006browse

本文实例讲述了js日期范围初始化得到前一个月日期的方法。分享给大家供大家参考。具体分析如下:

今天做时间范围的初始化设定,开始时间是当前时间的前一个月,终于找到完美的解决方案了。

Date.prototype.format = function(format) {
 var o = {
  "M+" : this.getMonth() + 1, // month
  "d+" : this.getDate(), // day
  "h+" : this.getHours(), // hour
  "m+" : this.getMinutes(), // minute
  "s+" : this.getSeconds(), // second
  "q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
  "S" : this.getMilliseconds()
 // millisecond
 }
 if (/(y+)/.test(format))
  format = format.replace(RegExp.$1, (this.getFullYear() + "")
    .substr(4 - RegExp.$1.length));
 for ( var k in o)
  if (new RegExp("(" + k + ")").test(format))
   format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
     : ("00" + o[k]).substr(("" + o[k]).length));
 return format;
}
var begin=new Date();
var end=new Date();
new Date(begin.setMonth((new Date().getMonth()-1)));
var begintime= begin.Format("yyyy-MM-dd");
var endtime=end.Format("yyyy-MM-dd");
$('#txtBeginVisitTime').val(begintime);
$('#txtEndVisitTime').val(endtime);

希望本文所述对大家的javascript程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn