React实战之将数据库返回的时间转换为几分钟前、几小时前、几天前的形式。
React实战之将数据库返回的时间转换为几分钟前、几小时前、几天前的形式。
不知道大家的时间格式是什么样子的,我先展示下我这里数据库返回的时间格式
‘2019-05-05T15:52:19Z’
是这个样子的,然后上代码:
在utils文件下创建文件time.js文件
time.js
export default function time(UTCtiem) {
var T_pos = UTCtiem.indexOf('T');
var Z_pos = UTCtiem.indexOf('Z');
var year_month_day = UTCtiem.substr(0, T_pos);
var hour_minute_second = UTCtiem.substr(T_pos + 1, Z_pos - T_pos - 1);
var new_datetime = year_month_day + " " + hour_minute_second;
var dateTime = new Date(new_datetime);
var no1new = dateTime.valueOf();
var year = dateTime.getFullYear();
var month = dateTime.getMonth() + 1;
var day = dateTime.getDate();
var hour = dateTime.getHours();
var minute = dateTime.getMinutes();
var second = dateTime.getSeconds();
var now = new Date();
var now_new = now.valueOf(); //typescript转换写法
var milliseconds = 0;
var timeSpanStr;
milliseconds = now_new - no1new;
if (milliseconds <= 1000 * 60 * 1) {
timeSpanStr = '刚刚';
}
else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
}
else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
}
else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
}
else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == now.getFullYear()) {
timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute;
} else {
timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
}
return timeSpanStr;
}
用法:
先需要import,具体看各位的路径
import time from '../utils/time'; //这里请注意自己的路径
let times = time('2019-06-05T15:32:19Z');
console.log('times',times);
代码就是这样!!!
React实战之将数据库返回的时间转换为几分钟前、几小时前、几天前的形式。的更多相关文章
- JS对象 返回/设置时间方法 get/setTime() 返回/设置时间,单位毫秒数 一小时为:60*60*1000
返回/设置时间方法 get/setTime() 返回/设置时间,单位毫秒数,计算从 1970 年 1 月 1 日零时到日期对象所指的日期的毫秒数. 如果将目前日期对象的时间推迟1小时,代码如下: &l ...
- MySQL 查询大于“时间字段”15分钟、1小时、1天的数据
以下代码中times为时间字段,类型为datetime 1.查询大于times十五分钟的数据 //大于号后面都是获取times十五分钟后的时间select*from table where now() ...
- 【视频合集】极客时间 react实战进阶45讲 【更新中】
https://up2.v.sharedaka.com/video/ochvq0AVfpa71A24bmugS5EewhFM1553702519936.mp4 01 React出现的历史背景及特性介绍 ...
- react实战 系列 —— React 的数据流和生命周期
其他章节请看: react实战 系列 数据流和生命周期 如何处理 React 中的数据,组件之间如何通信,数据在 React 中如何流动? 常用的 React 生命周期方法以及开源项目 spug 中使 ...
- react实战系列 —— React 中的表单和路由的原理
其他章节请看: react实战 系列 React 中的表单和路由的原理 React 中的表单是否简单好用,受控组件和非受控是指什么? React 中的路由原理是什么,如何更好的理解 React 应用的 ...
- 往MySQL数据库datetime类型字段中插入数据库的当前时间
代码: StringBuilder sb = new StringBuilder(); sb.append(" insert into uosdetailfile ("); sb. ...
- React实战之Ant Design—Upload上传_附件上传
React实战之Ant Design—Upload上传_附件上传 Upload组件大家都在官方文档中看过了,但写的时候还是会遇到许多问题,一些新手看了文档后感觉无从下手,本文过多的简绍就不说了,直接看 ...
- 如何诊断rac环境sysdate 返回的时间错误
最近处理了若干rac环境访问sysdate错误的时间返回.而这个问题通常是一个数据库链接是由现在Listener创建的情况下.并且.大部分情况下都是和时区设置相关的.在这篇文章中我们会针对怎样诊断这样 ...
- 从零开始react实战:云书签-1 react环境搭建
总览篇:react 实战之云书签 本篇是实战系列的第一篇,主要是搭建 react 开发环境,在create-react-app的基础上加上如下功能: antd 组件库按需引入 ,支持主题定制 支持 l ...
随机推荐
- CodeForces 596C Wilbur and Points
先对n个点分类,然后按题意要求构造,构造的时候判断这个点的右上方之前是否有点,判断可以用线段树来操作. #include<cstdio> #include<cstring> # ...
- spring mvc 选中多文件同时上传(利用input元素的multiple属性)
原文:http://m.blog.csdn.net/article/details?id=51351388 <!DOCTYPE html> <html> <head> ...
- Shell 脚本小试牛刀(5) -- 超便捷脚本之高速ssh 登录其它主机
假设你也是以Linux 为工作环境的童鞋,那么此文真是捷报!由于我的学习/工作中(特别是近期玩耍树莓派)常常会使用到ssh 登录其它主机,而每次使用ssh 登录都须要输入老长一大串让我非常烦.所以我写 ...
- 弄技术要弄通-公司reis的pub/sub怎么使用的呢?
Pub/Sub in Redis using PHP Posted on November 14, 2011by xmeng I would like to put an example togeth ...
- VB6 如何连接MYSQL数据库
1 从官网下载MYSQL的ODBC,选择与自己操作系统对应的版本(前提是你安装了MYSQL) http://dev.mysql.com/downloads/connector/odbc/ 2 安装 ...
- 在DIV中自己主动换行
word-break:break-all和word-wrap:break-word都是能使其容器如DIV的内容自己主动换行. 它们的差别就在于: 1,word-break:break-all 比如di ...
- 【教程】怎样申请Chrome应用商店(Web Store)开发人员
首先你须要一张信用卡,假设你没有的话.能够借用父母或他人的(多见于学生党) 假设你有信用卡.你还得看看信用卡正面是否有注明"VISA"."MasterCard" ...
- Eclipse导入项目: No projects are found to import
Eclipse导入项目: No projects are found to import 如果发导入工程import的时候,出现”No projects are found to import” 的 ...
- for循环console输出结果的问题
我想定时打印出一串数字,写好了如下代码 for (var i = 0; i < 5; i++) { setTimeout(function () { console.log(i); ...
- FMDB中常用SQL使用
大家工作中,最常用到的无非是 增.删.查.改... 在SQL中对应的语句为:INSERT DELETE SELECT UPDATE 首先,你可以使用一款叫做“sqlite database brows ...