【JS】獲取當前時間,時間與時間戳之間的轉換
阿新 • • 發佈:2019-01-10
獲取當前時間
<input name="timesj" value="" type="text" id="timesj" class="intxt">
<script type="text/javascript">
var nowDate = new Date().getTime();
document.getElementById("timesj").value = new Date().getTime();
</script>
時間與時間戳之間的轉換
<script type="text/javascript"> // 獲取當前時間戳(以s為單位) var timestamp = Date.parse(new Date()); timestamp = timestamp / 1000; //當前時間戳為:1403149534 console.log("當前時間戳為:" + timestamp); // 獲取某個時間格式的時間戳 var stringTime = "2014-07-10 10:21:12"; var timestamp2 = Date.parse(new Date(stringTime)); timestamp2 = timestamp2 / 1000; //2014-07-10 10:21:12的時間戳為:1404958872 console.log(stringTime + "的時間戳為:" + timestamp2); // 將當前時間換成時間格式字串 var timestamp3 = 1403058804; var newDate = new Date(); newDate.setTime(timestamp3 * 1000); // Wed Jun 18 2014 console.log(newDate.toDateString()); // Wed, 18 Jun 2014 02:33:24 GMT console.log(newDate.toGMTString()); // 2014-06-18T02:33:24.000Z console.log(newDate.toISOString()); // 2014-06-18T02:33:24.000Z console.log(newDate.toJSON()); // 2014年6月18日 console.log(newDate.toLocaleDateString()); // 2014年6月18日 上午10:33:24 console.log(newDate.toLocaleString()); // 上午10:33:24 console.log(newDate.toLocaleTimeString()); // Wed Jun 18 2014 10:33:24 GMT+0800 (中國標準時間) console.log(newDate.toString()); // 10:33:24 GMT+0800 (中國標準時間) console.log(newDate.toTimeString()); // Wed, 18 Jun 2014 02:33:24 GMT console.log(newDate.toUTCString()); Date.prototype.format = function(format) { var date = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), "q+": Math.floor((this.getMonth() + 3) / 3), "S+": this.getMilliseconds() }; if (/(y+)/i.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); } for (var k in date) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length)); } } return format; } console.log(newDate.format('yyyy-MM-dd h:m:s')); </script>
將時間戳轉換成日期格式
// 簡單的一句程式碼
var date = new Date(時間戳); //獲取一個時間物件
/**
1. 下面是獲取時間日期的方法,需要什麼樣的格式自己拼接起來就好了
2. 更多好用的方法可以在這查到 -> http://www.w3school.com.cn/jsref/jsref_obj_date.asp
*/
date.getFullYear(); // 獲取完整的年份(4位,1970)
date.getMonth(); // 獲取月份(0-11,0代表1月,用的時候記得加上1)
date.getDate(); // 獲取日(1-31)
date.getTime(); // 獲取時間(從1970.1.1開始的毫秒數)
date.getHours(); // 獲取小時數(0-23)
date.getMinutes(); // 獲取分鐘數(0-59)
date.getSeconds(); // 獲取秒數(0-59)
// 比如需要這樣的格式 yyyy-MM-dd hh:mm:ss
var date = new Date(1398250549490);
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() + ' ';
h = date.getHours() + ':' ;
m = date.getMinutes() + ':';
s = date.getSeconds();
console.log(Y+M+D+h+m+s);
// 輸出結果:2014-04-23 18:55:49
將日期格式轉換成時間戳
// 也很簡單
var strtime = '2014-04-23 18:55:49:123';
var date = new Date(strtime); //傳入一個時間格式,如果不傳入就是獲取現在的時間了,這樣做不相容火狐。
// 可以這樣做
var date = new Date(strtime.replace(/-/g, '/'));
// 有三種方式獲取,在後面會講到三種方式的區別
time1 = date.getTime();
time2 = date.valueOf();
time3 = Date.parse(date);
/*
三種獲取的區別:
第一、第二種:會精確到毫秒
第三種:只能精確到秒,毫秒將用0來代替
比如上面程式碼輸出的結果(一眼就能看出區別):
1398250549123
1398250549123
1398250549000
*/