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))}年前`
}
}

RN 时间戳的更多相关文章

  1. C# DateTime与时间戳转换

    C# DateTime与时间戳的相互转换,包括JavaScript时间戳和Unix的时间戳. 1. 什么是时间戳 首先要清楚JavaScript与Unix的时间戳的区别: JavaScript时间戳: ...

  2. nodejs中获取时间戳、时间差

    Nodejs中获取时间戳的方法有很多种,例如: new Date().getTime() Date.now() process.uptime() process.hrtime() 平时想获取一个时间戳 ...

  3. 基于RN开发的一款视频配音APP(开源)

    在如今React.ng.vue三分天下的格局下,不得不让自己加快学习的脚步.虽然经常会陷入各种迷茫,学得越多会发现不会的东西也被无限放大,不过能用新的技术作出一些小项目小Demo还是会给自己些许自信与 ...

  4. EF里Guid类型数据的自增长、时间戳和复杂类型的用法

    通过前两章Lodging和Destination类的演示,大家肯定基本了解Code First是怎么玩的了,本章继续演示一些很实用的东西.文章的开头提示下:提供的demo为了后面演示效果,前面代码有些 ...

  5. fmt标签把时间戳格式化日期

    jsp页面标签格式化日期 <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f" %> ...

  6. MySQL对时间戳的转换处理

    开发中很多时候在数据库里都会存储Long类型的时间戳,而时间戳做比对会相对麻烦 我的绝决方案: SELECT FROM_UNIXTIME(LEFT(create_time,10), '%Y-%m-%d ...

  7. Kafka消息时间戳(kafka message timestamp)

    最近碰到了消息时间戳的问题,于是花了一些功夫研究了一下,特此记录一下.   Kafka消息的时间戳 在消息中增加了一个时间戳字段和时间戳类型.目前支持的时间戳类型有两种: CreateTime 和 L ...

  8. Python时间戳和日期的相互转换

    Python时间戳和日期的相互转换 (2014-03-17 11:24:35) 转载▼   分类: Python 当前时间戳:time.time() 当前日期:time.ctime() 1.Pytho ...

  9. 时间戳TimeStamp处理

     我获得这个时间戳是得想除以1000再处理的,看看你们的需要先除多少再处理 //时间戳处理 NSInteger time = timeStamp / 1000; NSNumber *timer = [ ...

随机推荐

  1. c++ 集合的增删改查,与两集合的合并 缺陷(空间大小不灵活)

    #if 1 #include <iostream> #include <stdlib.h> using namespace std; class List { public: ...

  2. 利用js 生成不同li标签的点击事件

    <ul> <li>click me</li> <li>你好啊2</li> <li>你好啊3</li> <li& ...

  3. nginx 配置 HTTPS 及http 强制跳转https.

    #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  4. 鼠标跟随效果 vue或者js通用

    this.$refs.tooltip.getBoundingClientRect() => 用于获取某个元素相对于视窗的位置集合.集合中有top, right, bottom, left等属性. ...

  5. I/O 模型与 Java

    本文主要记录 Java 中 I/O 模型及相关知识点 .另,由于自身知识水平有限,如有不正之处,望各位能够谅解,欢迎批评指正! 一.I/O 基础 由于 Java 中 I/O 相关的 API ,无论是 ...

  6. js常用随手记

    1. 判断是否是空对象 let myObject={} Object.keys(myObject).length 2. void 0 代替 undefined undefined 不是保留字,在局部作 ...

  7. 往redis中存储数据是利用pipeline方法

    在redis中保存数据时,保存和设置有效时间是分开写的话,如果中间出现的异常,这会导致数据永久有效,因此就可以采用pipeline方法. # 创建redis管道对象,可以一次执行多个语句 pipeli ...

  8. Hello2实例的分析

    首先: java EE 上的hello2项目是一个部署在glass fish上的开发源码的java web项目,在终端通过命令行使用maven进行打包成.war文件,最后部署到相关的glass fis ...

  9. 随笔:关于Class.getSimpleName()

    最近学习过程中,遇到了Class.getSimpleName()这个方法,就搜索了一些资料: API定义: Class.getName():以String的形式,返回Class对象的"实体& ...

  10. WMware Vsphere取消某虚机的漂移

    由于一些业务特性,有一些虚机不应该完全受集群DSR控制.下面记录一下如何更改某一虚机的漂移属性. 1.环境 VMware Vsphere web client 6.5 2.点击需要配置虚机所在的集群, ...