茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    javascript中對Date類型的常用操作小結(jié)
    來源:易賢網(wǎng) 閱讀:1034 次 日期:2016-06-27 13:34:31
    溫馨提示:易賢網(wǎng)小編為您整理了“javascript中對Date類型的常用操作小結(jié)”,方便廣大網(wǎng)友查閱!

    下面小編就為大家?guī)硪黄猨avascript中對Date類型的常用操作小結(jié)。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。

    javascript中對Date類型的常用操作小結(jié)

    /** 

    3. * 日期時間腳本庫方法列表: 

    4. * (1)Date.isValiDate:日期合法性驗證 

    5. * (2)Date.isValiTime:時間合法性驗證 

    6. * (3)Date.isValiDateTime:日期和時間合法性驗證 

    7. * (4)Date.prototype.isLeapYear:判斷是否閏年 

    8. * (5)Date.prototype.format:日期格式化 

    9. * (6)Date.stringToDate:字符串轉(zhuǎn)成日期類型 

    10. * (7)Date.daysBetween:計算兩個日期的天數(shù)差 

    11. * (8)Date.prototype.dateAdd:日期計算,支持正負數(shù) 

    12. * (9)Date.prototype.dateDiff:比較日期差:比較兩個時期相同的字段,返回相差值 

    13. * (10)Date.prototype.toArray:把日期分割成數(shù)組:按數(shù)組序號分別為:年月日時分秒 

    14. * (11)Date.prototype.datePart:取得日期數(shù)據(jù)信息 

    15. */

    16. 

    17. 

    18./** 

    19. * 日期合法性驗證:判斷dataStr是否符合formatStr指定的日期格式 

    20. * 示例: 

    21. * (1)alert(Date.isValiDate('2008-02-29','yyyy-MM-dd'));//true 

    22. * (2)alert(Date.isValiDate('aaaa-58-29','yyyy-MM-dd'));//false 

    23. * dateStr:必選,日期字符串 

    24. * formatStr:可選,格式字符串,可選格式有:(1)yyyy-MM-dd(默認格式)或YYYY-MM-DD (2)yyyy/MM/dd或YYYY/MM/DD (3)MM-dd-yyyy或MM-DD-YYYY (4)MM/dd/yyyy或MM/DD/YYYY 

    25. */

    26.Date.isValiDate = function(dateStr, formatStr) 

    27.{ 

    28.  if(!dateStr){ 

    29.    return false; 

    30.  } 

    31.  if(!formatStr){ 

    32.    formatStr = "yyyy-MM-dd";//默認格式:yyyy-MM-dd  

    33.  } 

    34.  if(dateStr.length!=formatStr.length){ 

    35.    return false; 

    36.  }else{ 

    37.    if(formatStr=="yyyy-MM-dd"||formatStr=="YYYY-MM-DD"){ 

    38.      var r1=/^(((((([02468][048])|([13579][26]))(00))|(\d{2}(([02468][48])|([13579][26]))))\-((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-([0-2][0-9]))))|(\d{2}(([02468][1235679])|([13579][01345789]))\-((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-(([0-1][0-9])|(2[0-8]))))))$/; 

    39.      return r1.test(dateStr); 

    40.    }else if(formatStr=="yyyy/MM/dd"||formatStr=="YYYY/MM/DD"){ 

    41.      var r2=/^(((((([02468][048])|([13579][26]))(00))|(\d{2}(([02468][48])|([13579][26]))))\/((((0[13578])|(1[02]))\/(([0-2][0-9])|(3[01])))|(((0[469])|(11))\/(([0-2][0-9])|(30)))|(02\/([0-2][0-9]))))|(\d{2}(([02468][1235679])|([13579][01345789]))\/((((0[13578])|(1[02]))\/(([0-2][0-9])|(3[01])))|(((0[469])|(11))\/(([0-2][0-9])|(30)))|(02\/(([0-1][0-9])|(2[0-8]))))))$/; 

    42.      return r2.test(dateStr); 

    43.    }else if(formatStr=="MM-dd-yyyy"||formatStr=="MM-DD-YYYY"){ 

    44.      var r3=/^((((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-([0-2][0-9])))\-(((([02468][048])|([13579][26]))(00))|(\d{2}(([02468][48])|([13579][26])))))|(((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-(([0-1][0-9])|(2[0-8])))))\-\d{2}(([02468][1235679])|([13579][01345789])))$/; 

    45.      return r3.test(dateStr); 

    46.    }else if(formatStr=="MM/dd/yyyy"||formatStr=="MM/DD/YYYY"){ 

    47.      var r4=/^((((((0[13578])|(1[02]))\/(([0-2][0-9])|(3[01])))|(((0[469])|(11))\/(([0-2][0-9])|(30)))|(02\/([0-2][0-9])))\/(((([02468][048])|([13579][26]))(00))|(\d{2}(([02468][48])|([13579][26])))))|(((((0[13578])|(1[02]))\/(([0-2][0-9])|(3[01])))|(((0[469])|(11))\/(([0-2][0-9])|(30)))|(02\/(([0-1][0-9])|(2[0-8])))))\/\d{2}(([02468][1235679])|([13579][01345789])))$/; 

    48.      return r4.test(dateStr); 

    49.    }else{ 

    50.      alert("日期格式不正確!"); 

    51.      return false; 

    52.    } 

    53.  } 

    54.  return false; 

    55.} 

    56. 

    57. 

    58./** 

    59. * 時間合法性驗證:判斷timeStr是否符合formatStr指定的時間格式 

    60. * 示例: 

    61. * (1)alert(Date.isValiTime('23:59:59','hh:mm:ss'));//true 

    62. * (2)alert(Date.isValiTime('24-68-89','hh:mm:ss'));//false 

    63. * timeStr:必選,日期字符串 

    64. * formatStr:可選,格式字符串,可選格式有:(1)hh:mm:ss(默認格式) (2)hh-mm-ss (3)hh/mm/ss 

    65. */

    66.Date.isValiTime = function(timeStr, formatStr) 

    67.{ 

    68.  if(!timeStr){ 

    69.    return false; 

    70.  } 

    71.  if(!formatStr){ 

    72.    formatStr = "hh:mm:ss";//默認格式:hh:mm:ss  

    73.  } 

    74.  if(timeStr.length!=formatStr.length){ 

    75.    return false; 

    76.  }else{ 

    77.    if(formatStr=="hh:mm:ss"){ 

    78.      var r1=/^(([0-1][0-9])|(2[0-3]))\:([0-5][0-9])\:([0-5][0-9])$/; 

    79.      return r1.test(timeStr); 

    80.    }else if(formatStr=="hh-mm-ss"){ 

    81.      var r2=/^(([0-1][0-9])|(2[0-3]))\-([0-5][0-9])\-([0-5][0-9])$/; 

    82.      return r2.test(timeStr); 

    83.    }else if(formatStr=="hh/mm/ss"){ 

    84.      var r3=/^(([0-1][0-9])|(2[0-3]))\/([0-5][0-9])\/([0-5][0-9])$/; 

    85.      return r3.test(timeStr); 

    86.    }else{ 

    87.      alert("時間格式不正確!"); 

    88.      return false; 

    89.    } 

    90.  } 

    91.  return false; 

    92.} 

    93. 

    94. 

    95./** 

    96. * 日期和時間合法性驗證 

    97. * 格式:yyyy-MM-dd hh:mm:ss 

    98. */

    99.Date.isValiDateTime = function(dateTimeStr) 

    100.{ 

    101.  var dateTimeReg=/^(((((([02468][048])|([13579][26]))(00))|(\d{2}(([02468][48])|([13579][26]))))\-((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-([0-2][0-9]))))|(\d{2}(([02468][1235679])|([13579][01345789]))\-((((0[13578])|(1[02]))\-(([0-2][0-9])|(3[01])))|(((0[469])|(11))\-(([0-2][0-9])|(30)))|(02\-(([0-1][0-9])|(2[0-8]))))))(\s{1}(([0-1][0-9])|(2[0-3]))\:([0-5][0-9])\:([0-5][0-9]))?$/ 

    102.  return dateTimeReg.test(dateTimeStr); 

    103.} 

    104. 

    105. 

    106./** 

    107. * 判斷閏年 :一般規(guī)律為:四年一閏,百年不閏,四百年再閏。 

    108. */

    109.Date.prototype.isLeapYear = function() 

    110.{ 

    111.  return (this.getYear()%4==0&&((this.getYear()%100!=0)||(this.getYear()%400==0))); 

    112.} 

    113. 

    114. 

    115./** 

    116. * 日期格式化: 

    117. * formatStr:可選,格式字符串,默認格式:yyyy-MM-dd hh:mm:ss 

    118. * 約定如下格式: 

    119. * (1)YYYY/yyyy/YY/yy 表示年份 

    120. * (2)MM/M 月份 

    121. * (3)W/w 星期 

    122. * (4)dd/DD/d/D 日期 

    123. * (5)hh/HH/h/H 時間 

    124. * (6)mm/m 分鐘 

    125. * (7)ss/SS/s/S 秒 

    126. * (8)iii 毫秒 

    127. */

    128.Date.prototype.format = function(formatStr) 

    129.{ 

    130.  var str = formatStr; 

    131.  if(!formatStr){ 

    132.    str = "yyyy-MM-dd hh:mm:ss";//默認格式  

    133.  } 

    134.  var Week = ['日','一','二','三','四','五','六']; 

    135.   

    136.  str=str.replace(/yyyy|YYYY/,this.getFullYear()); 

    137.  str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100)); 

    138.   

    139.  str=str.replace(/MM/,this.getMonth()>=9?(parseInt(this.getMonth())+1).toString():'0' + (parseInt(this.getMonth())+1)); 

    140.  str=str.replace(/M/g,(parseInt(this.getMonth())+1)); 

    141.   

    142.  str=str.replace(/w|W/g,Week[this.getDay()]); 

    143.   

    144.  str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate()); 

    145.  str=str.replace(/d|D/g,this.getDate()); 

    146.   

    147.  str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours()); 

    148.  str=str.replace(/h|H/g,this.getHours()); 

    149.  str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes()); 

    150.  str=str.replace(/m/g,this.getMinutes()); 

    151.   

    152.  str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds()); 

    153.  str=str.replace(/s|S/g,this.getSeconds()); 

    154.   

    155.  str=str.replace(/iii/g,this.getMilliseconds()<10?'00'+this.getMilliseconds():(this.getMilliseconds()<100?'0'+this.getMilliseconds():this.getMilliseconds())); 

    156.   

    157.  return str; 

    158.} 

    159. 

    160. 

    161./** 

    162. * 字符串轉(zhuǎn)成日期類型: 

    163. * dateStr:必選,日期字符串,如果無法解析成日期類型,返回null 

    164. * 格式: 

    165. * (1)yyyy/MM/dd:IE和FF通用 

    166. * (2)MM/dd/yyyy:IE和FF通用 

    167. * (3)MM-dd-yyyy:僅IE 

    168. * (4)yyyy-MM-dd:非IE,且時鐘被解析在8點整 

    169. */

    170.Date.stringToDate = function(dateStr) 

    171.{ 

    172.  if(!dateStr){ 

    173.    alert("字符串無法解析為日期"); 

    174.    return null; 

    175.  }else{ 

    176.    if(Date.isValiDate(dateStr,"yyyy/MM/dd")||Date.isValiDate(dateStr,"MM/dd/yyyy")){ 

    177.      return new Date(Date.parse(dateStr)); 

    178.    }else{ 

    179.      if((!-[1,])){//IE  

    180.        if(Date.isValiDate(dateStr,"MM-dd-yyyy")){ 

    181.          return new Date(Date.parse(dateStr)); 

    182.        }else{ 

    183.          alert("字符串無法解析為日期"); 

    184.          return null; 

    185.        } 

    186.      }else{//非IE  

    187.        if(Date.isValiDate(dateStr,"yyyy-MM-dd")){ 

    188.          return new Date(Date.parse(dateStr)); 

    189.        }else{ 

    190.          alert("字符串無法解析為日期"); 

    191.          return null; 

    192.        } 

    193.      } 

    194.    } 

    195.  } 

    196.  return null; 

    197.} 

    198. 

    199. 

    200./** 

    201. * 計算兩個日期的天數(shù)差: 

    202. * dateOne:必選,必須是Data類型的實例 

    203. * dateTwo:必選,必須是Data類型的實例 

    204. */

    205.Date.daysBetween = function(dateOne,dateTwo) 

    206.{ 

    207.  if((dateOne instanceof Date)==false||(dateTwo instanceof Date)==false){ 

    208.    return 0; 

    209.  }else{ 

    210.    return Math.abs(Math.floor((dateOne.getTime()-dateTwo.getTime())/1000/60/60/24)); 

    211.  } 

    212.} 

    213. 

    214. 

    215./** 

    216. * 日期計算:支持負數(shù),即可加可減,返回計算后的日期 

    217. * num:必選,必須是數(shù)字,且正數(shù)是時期加,負數(shù)是日期減 

    218. * field:可選,標識是在哪個字段上進行相加或相減,字段見如下的約定。無此參數(shù)時,默認為d 

    219. * 約定如下格式: 

    220. * (1)Y/y 年 

    221. * (2)M 月 

    222. * (3)W/w 周 

    223. * (4)D/d 日 

    224. * (5)H/h 時 

    225. * (6)m 分 

    226. * (7)S/s 秒 

    227. * (8)Q/q 季 

    228. */

    229.Date.prototype.dateAdd = function(num, field) 

    230.{ 

    231.  if((!num)||isNaN(num)||parseInt(num)==0){ 

    232.    return this; 

    233.  } 

    234.  if(!field){ 

    235.    field = "d"; 

    236.  } 

    237.  switch(field){ 

    238.    case 'Y': 

    239.    case 'y':return new Date((this.getFullYear()+num), this.getMonth(), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());break; 

    240.    case 'Q': 

    241.    case 'q':return new Date(this.getFullYear(), (this.getMonth()+num*3), this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());break; 

    242.    case 'M':return new Date(this.getFullYear(), this.getMonth()+num, this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds());break; 

    243.    case 'W': 

    244.    case 'w':return new Date(Date.parse(this) + ((86400000 * 7) * num));break; 

    245.    case 'D': 

    246.    case 'd':return new Date(Date.parse(this) + (86400000 * num));break; 

    247.    case 'H': 

    248.    case 'h':return new Date(Date.parse(this) + (3600000 * num));break; 

    249.    case 'm':return new Date(Date.parse(this) + (60000 * num));break; 

    250.    case 'S': 

    251.    case 's':return new Date(Date.parse(this) + (1000 * num));break; 

    252.    default: return this; 

    253.  } 

    254.  return this; 

    255.} 

    256. 

    257. 

    258./** 

    259. * 比較日期差:比較兩個時期相同的字段,返回相差值 

    260. * dtEnd:必選,必須是Data類型的實例 

    261. * field:可選,標識是在哪個字段上進行比較,字段見如下的約定。無此參數(shù)時,默認為d 

    262. * 約定如下格式: 

    263. * (1)Y/y 年 

    264. * (2)M 月 

    265. * (3)W/w 周 

    266. * (4)D/d 日 

    267. * (5)H/h 時 

    268. * (6)m 分 

    269. * (7)S/s 秒 

    270. */

    271.Date.prototype.dateDiff = function(dtEnd, field) 

    272.{ 

    273.  var dtStart = this; 

    274.  if((dtEnd instanceof Date)==false){ 

    275.    return 0; 

    276.  }else{ 

    277.    if(!field){ 

    278.      field = "d"; 

    279.    } 

    280.    switch(field){ 

    281.      case 'Y': 

    282.      case 'y':return dtEnd.getFullYear() - dtStart.getFullYear();break; 

    283.      case 'M':return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1);break; 

    284.      case 'W': 

    285.      case 'w':return parseInt((dtEnd - dtStart) / (86400000 * 7));break; 

    286.      case 'D': 

    287.      case 'd':return parseInt((dtEnd - dtStart) / 86400000);break; 

    288.      case 'H': 

    289.      case 'h':return parseInt((dtEnd - dtStart) / 3600000);break; 

    290.      case 'm':return parseInt((dtEnd - dtStart) / 60000);break; 

    291.      case 'S': 

    292.      case 's':return parseInt((dtEnd - dtStart) / 1000);break; 

    293.      default: return 0; 

    294.    } 

    295.    return 0; 

    296.  } 

    297.} 

    298. 

    299. 

    300./** 

    301. * 把日期分割成數(shù)組:按數(shù)組序號分別為:年月日時分秒 

    302. */

    303.Date.prototype.toArray = function() 

    304.{ 

    305.  var myArray = new Array(); 

    306.  myArray[0] = this.getFullYear(); 

    307.  myArray[1] = this.getMonth(); 

    308.  myArray[2] = this.getDate(); 

    309.  myArray[3] = this.getHours(); 

    310.  myArray[4] = this.getMinutes(); 

    311.  myArray[5] = this.getSeconds(); 

    312.  return myArray; 

    313.} 

    314. 

    315. 

    316./** 

    317. * 取得日期數(shù)據(jù)信息: 

    318. * field:可選,標識是在哪個字段上進行比較,字段見如下的約定。無此參數(shù)時,默認為d 

    319. * (1)Y/y 年 

    320. * (2)M 月 

    321. * (3)W/w 周 

    322. * (4)D/d 日 

    323. * (5)H/h 時 

    324. * (6)m 分 

    325. * (7)S/s 秒 

    326. */

    327.Date.prototype.datePart = function(field) 

    328.{ 

    329.  if(!field){ 

    330.    field = "d"; 

    331.  } 

    332.  var Week = ['日','一','二','三','四','五','六']; 

    333.  switch (field){ 

    334.    case 'Y' : 

    335.    case 'y' :return this.getFullYear();break; 

    336.    case 'M' :return (this.getMonth()+1);break; 

    337.    case 'W' : 

    338.    case 'w' :return Week[this.getDay()];break; 

    339.    case 'D' : 

    340.    case 'd' :return this.getDate();break; 

    341.    case 'H' : 

    342.    case 'h' :return this.getHours();break; 

    343.    case 'm' :return this.getMinutes();break; 

    344.    case 's' :return this.getSeconds();break; 

    345.    default:return this.getDate(); 

    346.  } 

    347.  return this.getDate(); 

    348.}

    以上這篇javascript中對Date類型的常用操作小結(jié)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考

    更多信息請查看網(wǎng)絡編程
    易賢網(wǎng)手機網(wǎng)站地址:javascript中對Date類型的常用操作小結(jié)

    2026上岸·考公考編培訓報班

    • 報班類型
    • 姓名
    • 手機號
    • 驗證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
    工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)