...
1 var myDate = new Date(); 2 3 myDate.getYear(); //獲取當前年份(2位) 4 5 myDate.getFullYear(); //獲取完整的年份(4位,1970-????) 6 7 myDate.getMonth(); //獲取當前月份(0-11,0代表1月) 8 9 myDate.getDate(); //獲取當前日(1-31) 10 11 myDate.getDay(); //獲取當前星期X(0-6,0代表星期天) 12 13 myDate.getTime(); //獲取當前時間(從1970.1.1開始的毫秒數) 14 15 myDate.getHours(); //獲取當前小時數(0-23) 16 17 myDate.getMinutes(); //獲取當前分鐘數(0-59) 18 19 myDate.getSeconds(); //獲取當前秒數(0-59) 20 21 myDate.getMilliseconds(); //獲取當前毫秒數(0-999) 22 23 myDate.toLocaleDateString(); //獲取當前日期 24 25 var mytime=myDate.toLocaleTimeString(); //獲取當前時間 26 27 myDate.toLocaleString( ); //獲取日期與時間 28 29 /*日期時間腳本庫方法列表 30 Date.prototype.isLeapYear 判斷閏年 31 32 Date.prototype.Format 日期格式化 33 34 Date.prototype.DateAdd 日期計算 35 36 Date.prototype.DateDiff 比較日期差 37 38 Date.prototype.toString 日期轉字元串 39 40 Date.prototype.toArray 日期分割為數組 41 42 Date.prototype.DatePart 取日期的部分信息 43 44 Date.prototype.MaxDayOfDate 取日期所在月的最大天數 45 46 Date.prototype.WeekNumOfYear 判斷日期所在年的第幾周 47 48 StringToDate 字元串轉日期型 49 50 IsValidDate 驗證日期有效性 51 52 CheckDateTime 完整日期時間檢查 53 54 daysBetween 日期天數差 55 js代碼:*/ 56 //--------------------------------------------------- 57 58 // 判斷閏年 59 60 //--------------------------------------------------- 61 62 Date.prototype.isLeapYear = function() 63 64 { 65 66 return (0==this.getYear()%4&&((this.getYear()%100!=0)||(this.getYear()%400==0))); 67 68 } 69 70 71 //--------------------------------------------------- 72 73 // 日期格式化 74 75 // 格式 YYYY/yyyy/YY/yy 表示年份 76 77 // MM/M 月份 78 79 // W/w 星期 80 81 // dd/DD/d/D 日期 82 83 // hh/HH/h/H 時間 84 85 // mm/m 分鐘 86 87 // ss/SS/s/S 秒 88 89 //--------------------------------------------------- 90 91 Date.prototype.Format = function(formatStr) 92 93 { 94 95 var str = formatStr; 96 97 var Week = ['日','一','二','三','四','五','六']; 98 99 100 str=str.replace(/yyyy|YYYY/,this.getFullYear()); 101 102 str=str.replace(/yy|YY/,(this.getYear() % 100)>9?(this.getYear() % 100).toString():'0' + (this.getYear() % 100)); 103 104 105 str=str.replace(/MM/,this.getMonth()>9?this.getMonth().toString():'0' + this.getMonth()); 106 107 str=str.replace(/M/g,this.getMonth()); 108 109 110 str=str.replace(/w|W/g,Week[this.getDay()]); 111 112 113 str=str.replace(/dd|DD/,this.getDate()>9?this.getDate().toString():'0' + this.getDate()); 114 115 str=str.replace(/d|D/g,this.getDate()); 116 117 118 str=str.replace(/hh|HH/,this.getHours()>9?this.getHours().toString():'0' + this.getHours()); 119 120 str=str.replace(/h|H/g,this.getHours()); 121 122 str=str.replace(/mm/,this.getMinutes()>9?this.getMinutes().toString():'0' + this.getMinutes()); 123 124 str=str.replace(/m/g,this.getMinutes()); 125 126 127 str=str.replace(/ss|SS/,this.getSeconds()>9?this.getSeconds().toString():'0' + this.getSeconds()); 128 129 str=str.replace(/s|S/g,this.getSeconds()); 130 131 132 return str; 133 134 } 135 136 137 //+--------------------------------------------------- 138 139 //| 求兩個時間的天數差 日期格式為 YYYY-MM-dd 140 141 //+--------------------------------------------------- 142 143 function daysBetween(DateOne,DateTwo) 144 145 { 146 147 var OneMonth = DateOne.substring(5,DateOne.lastIndexOf ('-')); 148 149 var OneDay = DateOne.substring(DateOne.length,DateOne.lastIndexOf ('-')+1); 150 151 var OneYear = DateOne.substring(0,DateOne.indexOf ('-')); 152 153 154 var TwoMonth = DateTwo.substring(5,DateTwo.lastIndexOf ('-')); 155 156 var TwoDay = DateTwo.substring(DateTwo.length,DateTwo.lastIndexOf ('-')+1); 157 158 var TwoYear = DateTwo.substring(0,DateTwo.indexOf ('-')); 159 160 161 var cha=((Date.parse(OneMonth+'/'+OneDay+'/'+OneYear)- Date.parse(TwoMonth+'/'+TwoDay+'/'+TwoYear))/86400000); 162 163 return Math.abs(cha); 164 165 } 166 167 168 169 //+--------------------------------------------------- 170 171 //| 日期計算 172 173 //+--------------------------------------------------- 174 175 Date.prototype.DateAdd = function(strInterval, Number) { 176 177 var dtTmp = this; 178 179 switch (strInterval) { 180 181 case 's' :return new Date(Date.parse(dtTmp) + (1000 * Number)); 182 183 case 'n' :return new Date(Date.parse(dtTmp) + (60000 * Number)); 184 185 case 'h' :return new Date(Date.parse(dtTmp) + (3600000 * Number)); 186 187 case 'd' :return new Date(Date.parse(dtTmp) + (86400000 * Number)); 188 189 case 'w' :return new Date(Date.parse(dtTmp) + ((86400000 * 7) * Number)); 190 191 case 'q' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number*3, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); 192 193 case 'm' :return new Date(dtTmp.getFullYear(), (dtTmp.getMonth()) + Number, dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); 194 195 case 'y' :return new Date((dtTmp.getFullYear() + Number), dtTmp.getMonth(), dtTmp.getDate(), dtTmp.getHours(), dtTmp.getMinutes(), dtTmp.getSeconds()); 196 197 } 198 199 } 200 201 202 //+--------------------------------------------------- 203 204 //| 比較日期差 dtEnd 格式為日期型或者有效日期格式字元串 205 206 //+--------------------------------------------------- 207 208 Date.prototype.DateDiff = function(strInterval, dtEnd) { 209 210 var dtStart = this; 211 212 if (typeof dtEnd == 'string' )//如果是字元串轉換為日期型 213 214 { 215 216 dtEnd = StringToDate(dtEnd); 217 218 } 219 220 switch (strInterval) { 221 222 case 's' :return parseInt((dtEnd - dtStart) / 1000); 223 224 case 'n' :return parseInt((dtEnd - dtStart) / 60000); 225 226 case 'h' :return parseInt((dtEnd - dtStart) / 3600000); 227 228 case 'd' :return parseInt((dtEnd - dtStart) / 86400000); 229 230 case 'w' :return parseInt((dtEnd - dtStart) / (86400000 * 7)); 231 232 case 'm' :return (dtEnd.getMonth()+1)+((dtEnd.getFullYear()-dtStart.getFullYear())*12) - (dtStart.getMonth()+1); 233 234 case 'y' :return dtEnd.getFullYear() - dtStart.getFullYear(); 235 236 } 237 238 } 239 240 241 //+--------------------------------------------------- 242 243 //| 日期輸出字元串,重載了系統的toString方法 244 245 //+--------------------------------------------------- 246 247 Date.prototype.toString = function(showWeek) 248 249 { 250 251 var myDate= this; 252 253 var str = myDate.toLocaleDateString(); 254 255 if (showWeek) 256 257 { 258 259 var Week = ['日','一','二','三','四','五','六']; 260 261 str += ' 星期' + Week[myDate.getDay()]; 262 263 } 264 265 return str; 266 267 } 268 269 270 //+--------------------------------------------------- 271 272 //| 日期合法性驗證 273 274 //| 格式為:YYYY-MM-DD或YYYY/MM/DD 275 276 //+--------------------------------------------------- 277 278 function IsValidDate(DateStr) 279 280 { 281 282 var sDate=DateStr.replace(/(^\s+|\s+$)/g,''); //去兩邊空格; 283 284 if(sDate=='') return true; 285 286 //如果格式滿足YYYY-(/)MM-(/)DD或YYYY-(/)M-(/)DD或YYYY-(/)M-(/)D或YYYY-(/)MM-(/)D就替換為'' 287 288 //資料庫中,合法日期可以是:YYYY-MM/DD(2003-3/21),資料庫會自動轉換為YYYY-MM-DD格式 289 290 var s = sDate.replace(/[\d]{ 4,4 }[\-/]{ 1 }[\d]{ 1,2 }[\-/]{ 1 }[\d]{ 1,2 }/g,''); 291 292 if (s=='') //說明格式滿足YYYY-MM-DD或YYYY-M-DD或YYYY-M-D或YYYY-MM-D 293 294 { 295 296 var t=new Date(sDate.replace(/\-/g,'/')); 297 298 var ar = sDate.split(/[-/:]/); 299 300 if(ar[0] != t.getYear() || ar[1] != t.getMonth()+1 || ar[2] != t.getDate()) 301 302 { 303 304 //alert('錯誤的日期格式!格式為:YYYY-MM-DD或YYYY/MM/DD。註意閏年。'); 305 306 return false; 307 308 } 309 310 } 311 312 else 313 314 { 315 316 //alert('錯誤的日期格式!格式為:YYYY-MM-DD或YYYY/MM/DD。註意閏年。'); 317 318 return false; 319 320 } 321 322 return true; 323 324 } 325 326 327 //+--------------------------------------------------- 328 329 //| 日期時間檢查 330 331 //| 格式為:YYYY-MM-DD HH:MM:SS 332 333 //+--------------------------------------------------- 334 335 function CheckDateTime(str) 336 337 { 338 339 var reg = /^(\d+)-(\d{ 1,2 })-(\d{ 1,2 }) (\d{ 1,2 }):(\d{ 1,2 }):(\d{ 1,2 })$/; 340 341 var r = str.match(reg); 342 343 if(r==null)return false; 344 345 r[2]=r[2]-1; 346 347 var d= new Date(r[1],r[2],r[3],r[4],r[5],r[6]); 348 349 if(d.getFullYear()!=r[1])return false; 350 351 if(d.getMonth()!=r[2])return false; 352 353 if(d.getDate()!=r[3])return false; 354 355 if(d.getHours()!=r[4])return false; 356 357 if(d.getMinutes()!=r[5])return false; 358 359 if(d.getSeconds()!=r[6])return false; 360 361 return true; 362 363 } 364 365 366 //+--------------------------------------------------- 367 368 //| 把日期分割成數組 369 370 //+--------------------------------------------------- 371 372 Date.prototype.toArray = function() 373 374 { 375 376 var myDate = this; 377 378 var myArray = Array(); 379 380 myArray[0] = myDate.getFullYear(); 381 382 myArray[1] = myDate.getMonth(); 383 384 myArray[2] = myDate.getDate(); 385 386 myArray[3] = myDate.getHours(); 387 388 myArray[4] = myDate.getMinutes(); 389 390 myArray[5] = myDate.getSeconds(); 391 392 return myArray; 393 394 } 395 396 397 //+--------------------------------------------------- 398 399 //| 取得日期數據信息 400 401 //| 參數 interval 表示數據類型 402 403 //| y 年 m月 d日 w星期 ww周 h時 n分 s秒 404 405 //+--------------------------------------------------- 406 407 Date.prototype.DatePart = function(interval) 408 409 { 410 411 var myDate = this; 412 413 var partStr=''; 414 415 var Week = ['日','一','二','三','四','五','六']; 416 417 switch (interval) 418 419 { 420 421 case 'y' :partStr = myDate.getFullYear();break; 422 423 case 'm' :partStr = myDate.getMonth()+1;break; 424 425 case 'd' :partStr = myDate.getDate();break; 426 427 case 'w' :partStr = Week[myDate.getDay()];break; 428 429 case 'ww' :partStr = myDate.WeekNumOfYear();break; 430 431 case 'h' :partStr = myDate.getHours();break; 432 433 case 'n' :partStr = myDate.getMinutes();break; 434 435 case 's' :partStr = myDate.getSeconds();break; 436 437 } 438 439 return partStr; 440 441 } 442 443 444 //+--------------------------------------------------- 445 446 //| 取得當前日期所在月的最大天數 447 448 //+--------------------------------------------------- 449 450 Date.prototype.MaxDayOfDate = function() 451 452 { 453 454 var myDate = this; 455 456 var ary = myDate.toArray(); 457 458 var date1 = (new Date(ary[0],ary[1]+1,1)); 459 460 var date2 = date1.dateAdd(1,'m',1); 461 462 var result = dateDiff(date1.Format('yyyy-MM-dd'),date2.Format('yyyy-MM-dd')); 463 464 return result; 465 466 } 467 468 469 //+--------------------------------------------------- 470 471 //| 取得當前日期所在周是一年中的第幾周 472 473 //+--------------------------------------------------- 474 475 Date.prototype.WeekNumOfYear = function() 476 477 { 478 479 var myDate = this; 480 481 var ary = myDate.toArray(); 482 483 var year = ary[0]; 484 485 var month = ary[1]+1; 486 487 var day = ary[2]; 488 489 document.write('< script language=VBScript\> \n'); 490 491 document.write('myDate = Datue(''+month+'-'+day+'-'+year+'') \n'); 492 493 document.write('result = DatePart('ww', myDate) \n'); 494 495 document.write(' \n'); 496 497 return result; 498 499 } 500 501 502 //+--------------------------------------------------- 503 504 //| 字元串轉成日期類型 505 506 //| 格式 MM/dd/YYYY MM-dd-YYYY YYYY/MM/dd YYYY-MM-dd 507 508 //+--------------------------------------------------- 509 510 function StringToDate(DateStr) 511 512 { 513 514 515 var converted = Date.parse(DateStr); 516 517 var myDate = new Date(converted); 518 519 if (isNaN(myDate)) 520 521 { 522 523 //var delimCahar = DateStr.indexOf('/')!=-1?'/':'-'; 524 525 var arys= DateStr.split('-'); 526 527 myDate = new Date(arys[0],--arys[1],arys[2]); 528 529 } 530 531 return myDate; 532 533 } 534 535 若要顯示:當前日期加時間(如:2009-06-12 12:00) 536 function CurentTime() 537 538 { 539 540 var now = new Date(); 541 542 543 var year = now.getFullYear(); //年 544 545 var month = now.getMonth() + 1; //月 546 547 var day = now.getDate(); //日 548 549 550 var hh = now.getHours(); //時 551 552 var mm = now.getMinutes(); //分 553 554 555 var clock = year + "-"; 556 557 558 if(month < 10) 559 560 clock += "0"; 561 562 563 clock += month + "-"; 564 565 566 if(day < 10) 567 568 clock += "0"; 569 570 571 clock += day + " "; 572 573 574 if(hh < 10) 575 576 clock += "0"; 577 578 579 clock += hh + ":"; 580 581 if (mm < 10) clock += '0'; 582 583 clock += mm; 584 585 return(clock); 586 587 } 588 589 590 591 592 593 594 595 596 597 598 ;(function(window){ 599 600 /** 601 602 * [dateDiff 算時間差] 603 604 * @param {[type=Number]} hisTime [歷史時間戳,必傳] 605 606 * @param {[type=Number]} nowTime [當前時間戳,不傳將獲取當前時間戳] 607 608 * @return {[string]} [string] 609 610 */ 611 612 var dateDiff = function(hisTime,nowTime){ 613 614 if(!arguments.length) return ''; 615 616 var arg = arguments, 617 618 now =arg[1]?arg[1]:new Date().getTime(), 619 620 diffValue = now - arg[0], 621 622 result='', 623 624 625 626 minute = 1000 * 60, 627 628 hour = minute * 60, 629 630 day = hour * 24, 631 632 halfamonth = day * 15, 633 634 month = day * 30, 635 636 year = month * 12, 637 638 639 640 _year = diffValue/year, 641 642 _month =diffValue/month, 643 644 _week =diffValue/(7*day), 645 646 _day =diffValue/day, 647 648 _hour =diffValue/hour, 649 650 _min =diffValue/minute; 651 652 if(_year>=1) result=parseInt(_year) + "年前"; 653 else if(_month>=1) result=parseInt(_month) + "個月前"; 654 else if(_week>=1) result=parseInt(_week) + "周前"; 655 else if(_day>=1) result=parseInt(_day) +"天前"; 656 else if(_hour>=1) result=parseInt(_hour) +"個小時前"; 657 else if(_min>=1) result=parseInt(_min) +"分鐘前"; 658 else result="剛剛"; 659 return result; 660 } 661 window.dateDiff = dateDiff 662 })(window); 663 664 665 666 667