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. python学习笔记3-函数

    一.函数高级特性 1)列表生成式,列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. eg: >>> list(r ...

  2. 团队-student_blog-最终程序

    托管平台地址:https://github.com/gengwenhao/student_blog 小组名称:逛逛踹电脑 程序运行方法: 其他附加内容:demo版本:http://blog.gengw ...

  3. 电脑小知识-win10

    设置开机自启 一直想设置几个软件开机自启,尤其是文件搜索类和截图软件,不然每次打开电脑都要双击好几个工具,下面就记录一下如何设置开机自启. 1.先打开咱们的任务管理器,看看都有什么是开机自启的: 可以 ...

  4. c++单链表冒泡排序(交换结点),链表增删改查,运算符重载

    #include <iostream> #include <stdlib.h> #include <time.h> #include <fstream> ...

  5. MS SQL 全局临时表的删除

    本来已经搜索到怎么删除了 如下: IF OBJECT_ID( 'tempdb..##TEMP_COPTD') IS NOT NULL Begin DROP TABLE ##TEMP_COPTD End ...

  6. WEB学习笔记5-标准的HTML页面结构

    完整的文档包含一下 <html> <head> </head> <body> </body> </html> 在HTML5规范中 ...

  7. input 和 button元素 作为提交、重置、按钮功用的区别。

    首先,input元素和button元素 都是可以作为普通按钮.提交按钮.重置按钮的. <input type="button" value="button" ...

  8. scott 本月报将收录移动Web加速技术的主要进展,欢迎读者一起完善,投稿邮箱:openweb@baidu.com

    _trackPageview 功能说明 用于发送某个URL的PV统计请求,适用于统计AJAX.异步加载页面,友情链接,下载链接的流量. 代码语法 _czc.push(["_trackPage ...

  9. 工具提高效率 - iterm2

    快捷键 command + d, command + shift +d 显示所有记录 ITERM默认设置了终端能保留的历史输出行数,在进行调试时特别不方便,一旦输出过多就无法看到完整的历史记录. 在P ...

  10. 异常详细信息: System.BadImageFormatException: 未能加载文件或程序集“Maticsoft.Common”或它的某一个依赖项。试图加载格式不正确的程序。

    异常详细信息: System.BadImageFormatException: 未能加载文件或程序集“Maticsoft.Common”或它的某一个依赖项.试图加载格式不正确的程序. 解决方法: 点击 ...