RN 时间戳
let curTime = Date.now(); //獲取到當前時間
curTime: 1555120690696 //是指從1970.1.1到現在的毫秒(ms)數
時間與時間戳之間的轉換
// 獲取當前時間戳
var timestamp = Date.parse(new Date());
console.log(timestamp);
// 獲取某個時間格式的時間戳
var stringTime = "2019-06-10 11:37:00";
var timestamp2 = Date.parse(new Date(stringTime));
// 將當前時間換成時間格式字符串
var newDate = new Date();
newDate.setTime(timestamp);
// Mon Jun 10 2019
console.log(newDate.toDateString());
// Mon, 10 Jun 2019 03:37:42 GMT
console.log(newDate.toGMTString());
// 2019-06-10T03:37:42.000Z
console.log(newDate.toISOString());
// 2019-06-10T03:37:42.000Z
console.log(newDate.toJSON());
// 2019/6 /10
console.log(newDate.toLocaleDateString());
// 2019/6/10 上午11:37:42
console.log(newDate.toLocaleString());
// 上午11:37:42
console.log(newDate.toLocaleTimeString());
// Mon Jun 10 2019 11:37:42 GMT +0800(中國標準時間)
console.log(newDate.toString());
// 11:37:42 GMT + 0800(中國標準時間)
console.log(newDate.toTimeString());
// Mon, 10 Jun 2019 03:37:42 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;
}
// 2019-06-10 11:37:42
console.log(newDate.format('yyyy-MM-dd h:m:s'));
將時間戳轉換成日期格式
更多方法可以在這查到 -> http://www.w3school.com.cn/jsref/jsref_obj_date.asp
var date = new Date(時間戳); //獲取一個時間對象 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); // 2019-06-10 11:45:39
將日期格式轉換成時間戳
var strtime = '2014-04-23 18:55:49:123';
time1 = new Date(strtime).getTime(); //傳入一個時間格式,如果不傳入就是獲取現在的時間了,這樣做不兼容火狐。
time2 = new Date(strtime).valueOf();
計算時間差
cxk() {
//之前時間
let preTime = 1535710147654;
//當前時間
let nowTime = Date.now();
//間隔
let intervalTime = nowTime - preTime;
// 10min 毫秒數
let tenMinites = 60 * 10 * 1000;
let hour = tenMinites * 6;
let day = 24 * hour;
if (preTime > nowTime) {
return '當前時間傳遞有誤,請檢查!!!'
}
// 剛剛 (10min之內為剛剛)
if (intervalTime < tenMinites || intervalTime === tenMinites) {
return '剛剛'
}
// 幾分鐘 (10-60min為幾分鐘)
if (tenMinites < intervalTime && intervalTime <= hour) {
return `${Math.ceil((intervalTime) / tenMinites * .1)}分鐘前`
}
// 幾小時 (1-24h為幾小時)
if (hour < intervalTime && intervalTime <= day) {
return `${Math.ceil((intervalTime) / (6 * tenMinites))}小時前`
}
// (小于7d幾天前)
if (day < intervalTime && intervalTime <= day * 7) {
return `${Math.ceil((intervalTime) / (6 * tenMinites * 24))}天前`
}
// (大于7d為幾周前)
if (day * 7 < intervalTime && intervalTime <= day * 35) {
return `${Math.ceil((intervalTime) / (6 * tenMinites * 24 * 7))}周前`
}
// (大于四周為幾月前)
if (day * 7 * 5 < intervalTime && intervalTime <= day * 7 * 5 * 11) {
return `${Math.ceil((intervalTime) / (6 * tenMinites * 24 * 30))}月前`
}
// 幾年 (其余顯示幾年前)
if (day * 7 * 5 * 11 < intervalTime) {
return `${Math.ceil((intervalTime) / (6 * tenMinites * 24 * 365))}年前`
}
}
總結
- 上一篇: 转义字符的完整诠释
- 下一篇: 用SecureCRT实现真机跟虚拟机的文