日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

RN 时间戳

發(fā)布時間:2023/12/15 综合教程 34 生活家
生活随笔 收集整理的這篇文章主要介紹了 RN 时间戳 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

let curTime = Date.now(); //獲取到當(dāng)前時間

curTime: 1555120690696 //是指從1970.1.1到現(xiàn)在的毫秒(ms)數(shù)

時間與時間戳之間的轉(zhuǎn)換

// 獲取當(dāng)前時間戳
var timestamp = Date.parse(new Date());
console.log(timestamp);

// 獲取某個時間格式的時間戳
var stringTime = "2019-06-10 11:37:00";
var timestamp2 = Date.parse(new Date(stringTime));

// 將當(dāng)前時間換成時間格式字符串
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'));

將時間戳轉(zhuǎn)換成日期格式

更多方法可以在這查到 -> 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開始的毫秒數(shù))
date.getHours();  // 獲取小時數(shù)(0-23)
date.getMinutes();  // 獲取分鐘數(shù)(0-59)
date.getSeconds();  // 獲取秒數(shù)(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

將日期格式轉(zhuǎn)換成時間戳


var strtime = '2014-04-23 18:55:49:123';

time1 = new Date(strtime).getTime(); //傳入一個時間格式,如果不傳入就是獲取現(xiàn)在的時間了,這樣做不兼容火狐。
time2 = new Date(strtime).valueOf();

計算時間差

cxk() {
    //之前時間
    let preTime = 1535710147654;
    //當(dāng)前時間
    let nowTime = Date.now();
    //間隔
    let intervalTime = nowTime - preTime;
    // 10min 毫秒數(shù)
    let tenMinites = 60 * 10 * 1000;
    let hour = tenMinites * 6;
    let day = 24 * hour;

    if (preTime > nowTime) {
        return '當(dāng)前時間傳遞有誤,請檢查!!!'
    }
    // 剛剛 (10min之內(nèi)為剛剛)
    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))}年前`
    }
}

總結(jié)

以上是生活随笔為你收集整理的RN 时间戳的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。