javascript日期操作详解(脚本之家整理)(2)

<html> <head> <title> New Document </title> </head> <body> <script language=javascript> Date.prototype.getRead = function() { var values = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九"); var returnValue, temp; returnValue = this.getYear()+"年"; temp = (this.getMonth()+1)+"月"+this.getDate()+"日"; temp = temp.replace(/(\d)(\d)/g,"$1十$2").replace(/1十/g,"十").replace(/十0/g,"十"); returnValue += temp; returnValue = returnValue.replace(/\d/g, function(sts){return values[parseInt(sts)]}); return returnValue; } var t=new Date(); document.write(t.getRead()); </script> </body> </html>

7、得到前N天或后N天的日期


方法一:

<script type="text/javascript"> function showdate(n) { var uom = new Date(new Date()-0+n*86400000); uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate(); return uom; } window.alert("今天是:"+showdate(0)); window.alert("昨天是:"+showdate(-1)); window.alert("明天是:"+showdate(1)); window.alert("10天前是:"+showdate(-10)); window.alert("5天后是:"+showdate(5)); </script>

方法二:

<script type="text/javascript"> function showdate(n) { var uom = new Date(); uom.setDate(uom.getDate()+n); uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate(); return uom; } window.alert("今天是:"+showdate(0)); window.alert("昨天是:"+showdate(-1)); window.alert("明天是:"+showdate(1)); window.alert("10天前是:"+showdate(-10)); window.alert("5天后是:"+showdate(5)); </script>

方法三(不好意思,这个用vsscript做的,仅作为学习使用,不建议网页中使用,毕竟 IE only):

<script language="vbscript"> function showdate(n) showdate=dateadd("d",date(),n) end function msgbox "今天是:"&showdate(0) msgbox "昨天是:"&showdate(-1) msgbox "明天是:"&showdate(1) msgbox "十天前是:"&showdate(-10) msgbox "五天后是:"&showdate(5) </script>

方法四:

<script language="Javascript"> Date.prototype.getDays=function(){ var _newDate=new Date(); _newDate.setMonth(_newDate.getMonth()+1); _newDate.setDate(0); $_days=_newDate.getDate(); delete _newDate; return $_days; } function showdate(n) { var uom = new Date(); uom.setDate(uom.getDate()+n); uom = uom.getFullYear() + "-" + (uom.getMonth()+1) + "-" + uom.getDate()+"\n星期"+('天一二三四五六'.charAt(uom.getDay()))+"\n本月有"+ uom.getDays()+"天"; return uom; } window.alert("今天是:"+showdate(0)); window.alert("昨天是:"+showdate(-1)); window.alert("明天是:"+showdate(1)); window.alert("10天前是:"+showdate(-10)); window.alert("5天后是:"+showdate(5)); </script>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wggdjy.html