前些天女友工作上需要一些根据天数以及小时算日期的东西,今天特地给她做出来,顺便留在这里,有希望能帮助到一些朋友
- <body>
- <form id="f1" name="form1" method="post" action="">
- 按天算日期,注意12点之前的情况 <p>
- 天数:<input type="text" id="t1" name="t1" onchange="addDay(this.value)" onpropertychange="addDay(this.value)" />
- <p>
- 结果:<input type="text" id="t2" name="t2" />
- </p>
- </form>
- ------------------------------------------------------------------
- <form id="f2" name="form2" method="post" action="">
- 按小时算日期 <p>
- 天数:<input type="text" id="t1" name="t1" onchange="addHours(this.value)" onpropertychange="addHours(this.value)" />
- <p>
- 结果:<input type="text" id="t2" name="t2" />
- </p>
- </form>
- </body>
- <script>
- function addHours(h){
- var now = new Date();
- now.addHours(parseInt(h));//加减日期操作
- f2.t2.value=now.Format("yyyy-MM-dd hh:mm:ss");
- }
- function addDay(d){
- var now = new Date();
- now.addDays(parseInt(d));//加减日期操作
- f1.t2.value=now.Format("yyyy-MM-dd hh:mm:ss");
- }
- Date.prototype.Format = function(fmt)
- {
- var o =
- {
- "M+" : this.getMonth() + 1, //月份
- "d+" : this.getDate(), //日
- "h+" : this.getHours(), //小时
- "m+" : this.getMinutes(), //分
- "s+" : this.getSeconds(), //秒
- "q+" : Math.floor((this.getMonth() + 3) / 3), //季度
- "S" : this.getMilliseconds() //毫秒
- };
- if (/(y+)/.test(fmt))
- fmtfmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
- for (var k in o)
- if (new RegExp("(" + k + ")").test(fmt))
- fmtfmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
- return fmt;
- }
- Date.prototype.addHours = function(h)
- {
- this.setHours(this.getHours() + h);
- };
- Date.prototype.addDays = function(d)
- {
- this.setDate(this.getDate() + d);
- };
- Date.prototype.addWeeks = function(w)
- {
- this.addDays(w * 7);
- };
- Date.prototype.addMonths= function(m)
- {
- var d = this.getDate();
- this.setMonth(this.getMonth() + m);
- if (this.getDate() < d)
- this.setDate(0);
- };
- Date.prototype.addYears = function(y)
- {
- var m = this.getMonth();
- this.setFullYear(this.getFullYear() + y);
- if (m < this.getMonth())
- {
- this.setDate(0);
- }
- };
- </script>
转载于:https://blog.51cto.com/javas/453169