js Date对象的扩展函数

日期 http://momentjs.com/

Java代码  

  1. <script type="text/javascript">  
  2. Date.prototype.format = function(format) {  
  3.     var o = {  
  4.         "M+": this.getMonth() + 1,  
  5.         //month  
  6.         "d+": this.getDate(),  
  7.         //day  
  8.         "h+": this.getHours(),  
  9.         //hour  
  10.         "m+": this.getMinutes(),  
  11.         //minute  
  12.         "s+": this.getSeconds(),  
  13.         //second  
  14.         "q+": Math.floor((this.getMonth() + 3) / 3),  
  15.         //quarter  
  16.         "S": this.getMilliseconds() //millisecond  
  17.     }  
  18.     if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));  
  19.     for (var k in o)  
  20.     if (new RegExp("(" + k + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));  
  21.     return format;  
  22. }  
  23. // 日期相差天数  
  24. Date.prototype.diff = function(date){  
  25.     return Math.ceil((this - date) / (1000 * 60 * 60 * 24));  
  26. }  
  27.   
  28. // 日期加减计算  
  29. Date.prototype.add = function(days){  
  30.     return new Date(this.getTime() + days * (1000 * 60 * 60 * 24));  
  31. }  
  32.   
  33. // 日期加减计算  
  34. Date.prototype.addMonth = function(months){  
  35.     var day = this.getDate();  
  36.     var month = this.getMonth() + 1;  
  37.     var year = this.getFullYear();  
  38.     month += months;  
  39.     if(month > 12){  
  40.         year += Math.floor(month / 12);  
  41.         month = month % 12;  
  42.     }  
  43.     return Date.parse(month + '/' + day + '/' + year);  
  44. }  
  45. var ddd = new Date();  
  46. document.write(ddd.format('yyyy-MM-dd'));  
  47. </script>  

 

对象

Java代码  

  1. Object.prototype.clone = function()  
  2. {  
  3.     if(typeof(this) != "object")  
  4.         return this;  
  5.   
  6.     var $cloneDepth = (arguments.length >= 1) ? ((isNaN(parseInt(arguments[0]))) ? null : parseInt(arguments[0])) : null;  
  7.     if($cloneDepth)  
  8.         $cloneDepth = ($cloneDepth <= 0) ? null : $cloneDepth;  
  9.   
  10.     var $cloneObject = null;  
  11.     var $thisConstructor = this.constructor;  
  12.     var $thisConstructorPrototype = $thisConstructor.prototype;  
  13.   
  14.     if($thisConstructor == Array)  
  15.         $cloneObject = new Array();  
  16.     else if($thisConstructor == Object)  
  17.         $cloneObject = new Object();  
  18.     else  
  19.     {  
  20.         try  
  21.         {  
  22.             $cloneObject = new $thisConstructor;  
  23.         }  
  24.         catch(e)  
  25.         {  
  26.             $cloneObject = new Object();  
  27.             $cloneObject.constructor = $thisConstructor;  
  28.             $cloneObject.prototype = $thisConstructorPrototype;  
  29.         }  
  30.     }  
  31.   
  32.     var $propertyName = "";  
  33.     var $newObject = null;  
  34.   
  35.     for($propertyName in this)  
  36.     {  
  37.         $newObject = this[$propertyName];  
  38.   
  39.         if(!$thisConstructorPrototype[$propertyName])  
  40.         {  
  41.             if(typeof($newObject) == "object")  
  42.             {  
  43.                 if($newObject === null)  
  44.                     $cloneObject[$propertyName] = null;  
  45.                 else  
  46.                 {  
  47.                     if($cloneDepth)  
  48.                     {  
  49.                         if($cloneDepth == 1)  
  50.                             $cloneObject[$propertyName] = $newObject;  
  51.                         else  
  52.                             $cloneObject[$propertyName] = $newObject.clone(--$cloneDepth);  
  53.                     }  
  54.                     else  
  55.                         $cloneObject[$propertyName] = $newObject.clone();  
  56.                 }  
  57.             }  
  58.             else  
  59.                 $cloneObject[$propertyName] = $newObject;  
  60.         }  
  61.     }  
  62.   
  63.     return $cloneObject;  
  64. }  

ss

时间: 2024-08-03 04:33:20

js Date对象的扩展函数的相关文章

原生JS:Date对象全面解析_javascript技巧

Date对象:基于1970年1月1日(世界标准时间)起的毫秒数 本文参考MDN做的详细整理,方便大家参考MDN 构造函数: 1.new Date(); 依据系统设置的当前时间来创建一个Date对象. 2.new Date(value); value代表自1970年1月1日00:00:00 (世界标准时间) 起经过的毫秒数. 3.new Date(dateString); dateString表示日期的字符串值.该字符串应该能被 Date.parse() 方法识别(符合 IETF-complian

js String对象的扩展函数

Java代码   String.prototype.trim = function() {      return this.replace(/^\s+|\s+$/g,"");   }   String.prototype.ltrim = function() {      return this.replace(/^\s+/g,"");   }   String.prototype.rtrim = function() {      return this.rep

扩展JS Date对象时间格式化功能的小例子_javascript技巧

在自己JS代码中引入一下代码: 复制代码 代码如下: Date.prototype.format =function(format){    var o = {            "M+" : this.getMonth()+1, //month            "d+" : this.getDate(), //day            "h+" : this.getHours(), //hour            "

八百年一次,这个七月有5个礼拜五,5个礼拜六,5个礼拜天(js Date对象练习良好方案)[原]

今早看园友的文章八百年一次--,心想到底是不是八百年一遇,就用js自己验证了一下: 园友判断有5个礼拜五,5个礼拜六,5个礼拜天的方法: 1:该月必须有31天 2:该月1号必须是星期5. (我的判断是最后一天是星期天,求最后一天是为了方便判断这个月有的天数)     <script>         //move back one month        function prevMonth(thisMonth, thisYear) {             if (thisMonth =

js Array对象的扩展函数代码_javascript技巧

使用 复制代码 代码如下: <script language=javascript>var isNumeric = function(x) {   // returns true if x is numeric and false if it is not.   var RegExp = /^(-)?(\d*)(\.?)(\d*)$/;    return String(x).match(RegExp);}var myArray = [1,'two',3,'four',5,'six',7,'e

JS date对象的减法处理实现代码_时间日期

复制代码 代码如下: var time1="2010-11-01 10:12:12"; var time2="2010-11-01 11:12:12"; var t1=new Date(time1.replace(/-/g, '/')); var t2=new Date(time2.replace(/-/g, '/')); alert((t2-t1).toString()=="3600000"?"yes":"no&q

Date 对象

对象   启用基本存储器并取得日期和时间. dateObj = new Date()dateObj = new Date(dateVal)dateObj = new Date(year, month, date[, hours[, minutes[, seconds[,ms]]]]) 参数 dateObj 必选项.要赋值为 Date 对象的变量名. dateVal 必选项.如果是数字值,dateVal 表示指定日期与 1970 年 1 月 1 日午夜间全球标准时间 的毫秒数.如果是字符串,则 d

Js Date泣血整理

原文:Js Date泣血整理JS Date 对象用于处理日期和时间. 创建 Date 对象的语法: var myDate=new Date() Date 对象会自动把当前日期和时间保存为其初始值. 参数形式有以下5种:   new Date("month dd,yyyy hh:mm:ss"); new Date("month dd,yyyy"); new Date(yyyy,mth,dd,hh,mm,ss); new Date(yyyy,mth,dd); new D

javascript Date对象setMonth方法的bug

其实耶谈不上什么BUG,只是JS Date对象对溢出时间进行了自己的处理. 首先把你的系统时间设为2013年1月31号,这一步很重要,不然可能不会发生这个BUG,然后执行下面的代码    代码如下 复制代码 var t = new Date(); t.getMonth(); //输出0,现在获取的是正确的值,因为getMonth是从0开始索引的 t.setMonth(3); //设置月份为4月 t.getMonth(); //输出4,这里本应该输出3的var t = new Date(); t.