js操作日期函数的一系列简单问题(日期加一个月)

来源:互联网 发布:js object 迭代 编辑:程序博客网 时间:2024/06/06 08:32
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> lianjiao </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>

 <body>
 

<input id="data" type="text" ></input>
<input type="button" value="ok" onclick="ok()"></input>

<script>

function ok(){
var date = document.getElementById("data").value;
date+="-01";
AddMonths(date,1);


}
// add Month
function AddMonths(date,value)
{
var date1 = getDate(date);
date1.setMonth(date1.getMonth()+value);
alert(date1.format('yyyy-MM'));

}

//string to date
 function getDate(strDate) {
            var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/,
             function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');
            return date;
        }

//fomate
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;  
}  
</script>

 </body>
</html>


原创粉丝点击