RN 时间戳

摘要:
'0'+:date.getMonth()+1)+'-';D=date.getDate()+'';h=date.getHours()+':';m=date.getMinutes()+':';s=date.getSeconds();console.log;//2019-06-1011:45:39将日期格式转换成时间戳varstrtime='2014-04-2318:55:49:123';time1=newDate.getTime();//传入一个时间格式,如果不传入就是获取现在的时间了,这样做不兼容火狐。'}//刚刚if{return'刚刚'}//几分钟if{return`${Math.ceil}分钟前`}//几小时if{return`${Math.ceil}小时前`}//if{return`${Math.ceil}天前`}//if(day*7˂intervalTime&&inte
let curTime = Date.now(); //获取到当前时间

curTime: 1555120690696 //是指从1970.1.1到现在的毫秒(ms)数

时间与时间戳之间的转换

// 获取当前时间戳
var timestamp = Date.parse(newDate());
console.log(timestamp);

// 获取某个时间格式的时间戳
var stringTime = "2019-06-10 11:37:00";
var timestamp2 = Date.parse(newDate(stringTime));

// 将当前时间换成时间格式字符串
var newDate = newDate();
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 indate) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? date[k] : ("00" + date[k]).substr(("" +date[k]).length));
        }
    }
    returnformat;
}
// 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))}年前`
    }
}

免责声明:文章转载自《RN 时间戳》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇visual studio 2010 c++ 打印 Hello worldUnity3D–Texture图片空间和内存占用分析下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

pandas的基础使用

从 CSV 文件中读取数据 这个数据集是一个列表,蒙特利尔的 7 个不同的自行车道上每天有多少人。 broken_df = pd.read_csv('../data/bikes.csv') # 查看前三行 broken_df[:3] 你可以看到这完全损坏了,说明读取方式不合适。 read_csv 拥有一堆选项能够让我们修复它,在这里我们: 将列分隔符改...

Oracle 计算工时除去节假日(返回小时数)

--前提条件:DIM_oa_TIME 包含每一天,并且is_work=1 工作日 =0 非工作日 --详见:https://www.cnblogs.com/xiaobaidejiucuoben/p/14630923.html create or replace function getworktime(begindate in date,enddate...

mysql8中窗口函数

引用自: https://blog.csdn.net/yeshang_lady/article/details/102728513 在以前的MySQL版本中是没有窗口函数的,直到MySQL8.0才引入了窗口函数。窗口函数是对查询中的每一条记录执行一个计算,并且这个计算结果是用与该条记录相关的多条记录得到的。 1.窗口函数与聚合函数 窗口函数与聚合函数很像...

mysql时间表示和计算

计算:天数 SELECTDATEDIFF('2015-06-29','2015-06-12') ASDiffDate   表示: DATE_FORMAT(date,format)  根据format字符串格式化date值。下列修饰符可以被用在format字符串中: %M 月名字(January……December)  %W 星期名字(Sunday…...

dbms_scheduler介绍

dbms_scheduler介绍 10g引入的这个dbms_scheduler包,替代了之前的dbms_job包,该包功能更强大,可以将job需要的各种资源分开再进行组合。1.program1.1 create_program参数说明:  program_name——程序名称  program_type——程序类型(STORED_PROCEDURE,PL...

HBase 学习(一) Python操作Hbase

一,前言 二,包安装 三,表操作DDL 四,数据操作DML 正文 一,前言   上节讲到我们可以用JavaAPI进行Hbase的操作,但是很明显,Java的API很底层,用起来会很不方便,如果你们学习过Python,可以用Python来对Hbase进行操作。   happybase使用:https://happybase.readthedocs.io/e...