zoukankan      html  css  js  c++  java
  • 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))}年前`
        }
    }
  • 相关阅读:
    (IDEA) VCS-->Import Into Version Control没有Share Project(Subversion)这个选项。
    Maven学习笔记(二)—— 整合SSH框架
    Maven学习笔记(一)—— Maven基础
    使用IDEA完成maven整合SSH框架时抛出Hibernate : Mapping (RESOURCE) not found
    mysql性能的检查和优化方法
    每个php程序员都应该知道的15个最佳PHP库
    linux oracle 11g 漏洞补丁升级
    linux 启动MongoDB
    linux 7 查看oracle 11g版本号
    linux 清除缓存命令
  • 原文地址:https://www.cnblogs.com/gemeiyi/p/10700959.html
Copyright © 2011-2022 走看看