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))}年前`
    }
}
posted @ 2019-04-13 14:07  GGGG6666  阅读(1023)  评论(0编辑  收藏  举报