var date = new Date(Date.parse("2015-09-05".replace(/-/g,"/")));
'2015-09-05'是無法被各個瀏覽器中,使用new Date(str)來正確生成日期對象的。 正確的用法是'2015/09/06'.
-------------------------------------------------
javascript的日期用法:
var myDate = new Date(); myDate.getYear(); //獲取當前年份(2位) myDate.getFullYear(); //獲取完整的年份(4位,1970-????) myDate.getMonth(); //獲取當前月份(0-11,0代表1月) myDate.getDate(); //獲取當前日(1-31) myDate.getDay(); //獲取當前星期X(0-6,0代表星期天) 何問起 hovertree.com myDate.getTime(); //獲取當前時間(從1970.1.1開始的毫秒數) myDate.getHours(); //獲取當前小時數(0-23) myDate.getMinutes(); //獲取當前分鍾數(0-59) myDate.getSeconds(); //獲取當前秒數(0-59) myDate.getMilliseconds(); //獲取當前毫秒數(0-999) myDate.toLocaleDateString(); //獲取當前日期 var mytime=myDate.toLocaleTimeString(); //獲取當前時間 myDate.toLocaleString( ); //獲取日期與時間
==========================================================================
JS獲取當前時間戳的方法-JavaScript 獲取當前時間戳
JavaScript 獲取當前時間戳:
第一種方法:
var timestamp =Date.parse(new Date());
結果:1280977330000
第二種方法:
var timestamp =(new Date()).valueOf();
結果:1280977330748
第三種方法:
var timestamp=new Date().getTime();
結果:1280977330748
第一種:獲取的時間戳是把毫秒改成000顯示,
第二種和第三種是獲取了當前毫秒的時間戳。
我和同事在用js實現一個顯示出分析數據所剩大概時間的過程中,時間總是變給0,結果很怪異,最后發現獲取時間的時候用的是Date.parse(newDate())獲取的時間戳把毫秒改成了000顯示,所以時間差計算的不准確。
可以用第二種或第三種方法計算時間差。http://hovertree.com/menu/javascript/
js中單獨調用new Date(),例如document.write(new Date());
顯示的結果是:Mar 31 10:10:43 UTC+0800 2012 這種格式的時間
但是用new Date() 參與計算會自動轉換為從1970.1.1開始的毫秒數
--------------------------------------------------------------------------------------------------
將字符串形式的日期轉換成日期對象
var strTime="2011-04-16"; //字符串日期格式 var date= new Date(Date.parse(strTime.replace(/-/g, "/"))); //轉換成Data(); var month=date.getMonth()+1; //獲取當前月份
----------------------------------------------------
1 // 獲取當前時間戳(以s為單位) 2 var timestamp = Date.parse(new Date()); 3 timestamp = timestamp / 1000; 4 //當前時間戳為:1403149534 5 console.log("當前時間戳為:" + timestamp); 6 7 // 獲取某個時間格式的時間戳 8 var stringTime = "2014-07-10 10:21:12"; 9 var timestamp2 = Date.parse(new Date(stringTime)); 10 timestamp2 = timestamp2 / 1000; 11 //2014-07-10 10:21:12的時間戳為:1404958872 12 console.log(stringTime + "的時間戳為:" + timestamp2); 13 14 // 將當前時間換成時間格式字符串 15 var timestamp3 = 1403058804; 16 var newDate = new Date(); 17 newDate.setTime(timestamp3 * 1000); 18 // Wed Jun 18 2014 19 console.log(newDate.toDateString()); 20 // Wed, 18 Jun 2014 02:33:24 GMT 21 console.log(newDate.toGMTString()); 22 // 2014-06-18T02:33:24.000Z 23 console.log(newDate.toISOString()); 24 // 2014-06-18T02:33:24.000Z 25 console.log(newDate.toJSON()); 26 // 2014年6月18日 27 console.log(newDate.toLocaleDateString()); 28 // 2014年6月18日 上午10:33:24 29 console.log(newDate.toLocaleString()); 30 // 上午10:33:24 31 console.log(newDate.toLocaleTimeString()); 32 // Wed Jun 18 2014 10:33:24 GMT+0800 (中國標准時間) 33 console.log(newDate.toString()); 34 // 10:33:24 GMT+0800 (中國標准時間) 35 console.log(newDate.toTimeString()); 36 // Wed, 18 Jun 2014 02:33:24 GMT 37 console.log(newDate.toUTCString()); 38 39 Date.prototype.format = function(format) { 40 var date = { 41 "M+": this.getMonth() + 1, 42 "d+": this.getDate(), 43 "h+": this.getHours(), 44 "m+": this.getMinutes(), 45 "s+": this.getSeconds(), 46 "q+": Math.floor((this.getMonth() + 3) / 3), 47 "S+": this.getMilliseconds() 48 }; 49 if (/(y+)/i.test(format)) { 50 format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); 51 } 52 for (var k in date) { 53 if (new RegExp("(" + k + ")").test(format)) { 54 format = format.replace(RegExp.$1, RegExp.$1.length == 1 55 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length)); 56 } 57 } 58 return format; 59 } 60 console.log(newDate.format('yyyy-MM-dd h:m:s'));