函数内容

// 时间转为时间戳
function date2timestamp(datetime) {
var timestamp = new Date(Date.parse(datetime));
timestamp = timestamp.getTime();
timestamp = timestamp / 1000;
return timestamp;
} // 时间戳转时间
function timestamp2date(timestamp, mode) {
var tt = new Date(parseInt(timestamp) * 1000).toLocaleString().replace(/:\d{1,2}$/, ' ').replace(/年|月/g, "-").replace(/日/g, " ").replace(/上午/g, "").replace(/下午/g, "");
var date_arr = tt.split(" "); if (mode == 3) {
var minute = 60;
var hour = minute * 60;
var day = hour * 24;
var halfamonth = day * 15;
var month = day * 30;
var current_timestamp = parseInt(Date.parse(new Date()) / 1000);
var diffValue = current_timestamp - timestamp;
var monthC = diffValue / month;
var weekC = diffValue / (7 * day);
var dayC = diffValue / day;
var hourC = diffValue / hour;
var minC = diffValue / minute;
if (monthC >= 1) {
result = parseInt(monthC) + "月前";
}
else if (weekC >= 1) {
result = parseInt(weekC) + "周前";
}
else if (dayC >= 1) {
result = parseInt(dayC) + "天前";
}
else if (hourC >= 1) {
result = parseInt(hourC) + "小时前";
}
else if (minC >= 1) {
result = parseInt(minC) + "分钟前";
} else
result = "刚刚";
return result;
} if (mode == 2) {
var current_timestamp = parseInt(Date.parse(new Date()) / 1000);
if ((current_timestamp - timestamp) > 7 * 24 * 60 * 60) {
// 一周之前,显示日期
return date_arr[0];
} else {
var d = new Date();
var date = d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate();
var b_date = date2timestamp(date + " 00:00:00");
var e_date = date2timestamp(date + " 23:59:59"); if (parseInt(timestamp) > parseInt(b_date) && parseInt(timestamp) < parseInt(e_date)) {
// 今天,只显示时间
return date_arr[1];
} if (parseInt(timestamp) > parseInt(b_date - 24 * 60 * 60) && parseInt(timestamp) < parseInt(e_date - 24 * 60 * 60)) {
// 昨天,显示昨天
return "昨天";
} // 显示周几
var days = new Array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
var day = new Date(date_arr[0]).getDay();
return days[day];
}
} if (mode == 1) {
// 如果是当天,就不显示日期
var d = new Date();
var date = d.getFullYear() + "/" + (d.getMonth() + 1) + "/" + d.getDate();
var b_date = date2timestamp(date + " 00:00:00");
var e_date = date2timestamp(date + " 23:59:59"); if (parseInt(timestamp) > parseInt(b_date) && parseInt(timestamp) < parseInt(e_date)) {
return date_arr[1];
}
return tt;
} return tt;
} // 1498806947 2017.6.30 15:15 // 1498720547 2017.6.29 15:15 // 1498634147 2017.6.28 15:15 // 1497942947 2017.6.20 15:15 console.log(timestamp2date('1498806947', 3));
console.log(timestamp2date('1498720547', 3));
console.log(timestamp2date('1498634147', 3));
console.log(timestamp2date('1497942947', 3)); console.log("-------------------------------"); console.log(timestamp2date('1498806947', 2));
console.log(timestamp2date('1498720547', 2));
console.log(timestamp2date('1498634147', 2));
console.log(timestamp2date('1497942947', 2)); console.log("-------------------------------"); console.log(timestamp2date('1498806947', 1));
console.log(timestamp2date('1498720547', 1));
console.log(timestamp2date('1498634147', 1));
console.log(timestamp2date('1497942947', 1)); console.log("-------------------------------"); console.log(timestamp2date('1498806947'));
console.log(timestamp2date('1498720547'));
console.log(timestamp2date('1498634147'));
console.log(timestamp2date('1497942947'));

执行结果

1小时前
1天前
2天前
1周前
-------------------------------
15:15
昨天
星期三
2017-06-20
-------------------------------
15:15
2017-06-29 15:15
2017-06-28 15:15
2017-06-20 15:15
-------------------------------
2017-06-30 15:15
2017-06-29 15:15
2017-06-28 15:15
2017-06-20 15:15

小结

JS对于时间戳处理不太便捷。需要自己计算处理。根据需要,显示不同的时间模式。

JS时间转时间戳,时间戳转时间。时间显示模式。的更多相关文章

  1. js 时间转成时间戳对比;My97DatePicker日历控件时间格式;Date.parse Firefox火狐浏览器返回Nan的解决办法

    有个情况,我在显示时间的时候是需要显示为  2013年8月15日 14时28分15秒 但是假如我用js去获取到这个时间,并且想进行时间对比的时候,这个时间2013年8月15日 14时28分15秒根本就 ...

  2. js实现new Date(),时间对象和时间戳操作

    1.js中实现时间date对象 var myDate = new Date();//获取系统当前时间,结果:Wed Aug 09 2017 00:00:00 GMT+0800 (中国标准时间) 2.获 ...

  3. 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换

    var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...

  4. JS时间格式和时间戳的互转

    //时间格式转为时间戳 function sjc(){ var date = new Date(); //时间对象 var str = date.getTime(); //转换成时间戳 } //时间戳 ...

  5. js时间格式化工具,时间戳格式化,字符串转时间戳

    在开发中经常会用到时间格式化,有时候在网上搜索一大堆但不是自己想要的,自己总结一下,写一个时间格式化工具方便以后直接使用,欢迎大家来吐槽…… 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  6. js对时间戳的处理 获取时间,昨天,今天,明天,时间不同格式

    1.获取昨天,今天,明天的时间 //昨天的时间 var day1 = new Date(); day1.setTime(day1.getTime()-24*60*60*1000); var s1 = ...

  7. vue ele 日期时间格式限制不能早于当天,时间转换成时间戳 进行比较

    <el-date-picker             value-format="yyyy-MM-dd HH:mm:ss"             v-model=&quo ...

  8. mysql时间属性之时间戳和datetime之间的转换

    一.datetime转换为时间戳     方案一:强制转换字段类型 use`nec`; ; ) NOT NULL COMMENT '注册时间' , ) NULL DEFAULT NULL COMMEN ...

  9. Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象

    1. 时间字符串 --> 时间戳 1) time 模块 timestring = '2016-12-21 10:22:56' print time.mktime(time.strptime(ti ...

  10. 时间:UTC时间、GMT时间、本地时间、Unix时间戳

    转自:http://blog.csdn.net/u012102306/article/details/51538574 1.UTC时间 与 GMT时间 我们可以认为格林威治时间就是时间协调时间(GMT ...

随机推荐

  1. AQS源码分析

    AQS全程为AbstractQueuedSynchronizer,其定义了一套多线程访问共享资源的同步框架,大部分的同步类的实现都依赖于他,比如ReentrantLock,ReentrantReadW ...

  2. python Django Nginx+ uWSGI 安装配置

    环境: CentOS7.python-3.5.3.Nignx 1.10.3 .Django 1.10.6.uWSGI 2.0.14 django项目目录:/var/webRoot/p1 项目结构: 基 ...

  3. 洛谷2530(codevs2098)化工厂装箱员

    题目:https://www.luogu.org/problemnew/show/P2530 dp或搜索. dp做法就是 当前值+1 转移到 当前某一维为0.位置前进了c位 的地方.但没写. 写了搜索 ...

  4. GitHub + circleCI 自动构建/自动部署 应用

    GitHub + circleCI 自动构建/自动部署, 这里略过了单元测试,以部署 laravel 应用为例子 比起 gitlab + ansible + genkins 操作起来节省了很多硬件资源 ...

  5. ML(3): 贝叶斯方法

    对于分类问题,我们每个人每天都在执行分类操作,只是我们没有意识到罢了.例如,当你看到一个陌生人,你的脑子下意识判断TA是男是女:你可能经常会走在路上对身旁的朋友说“这个人一看就很有钱.那边有个非主流” ...

  6. Asp.Net中使用Newtonsoft.Json转换,读取,写入

    using Newtonsoft.Json;using Newtonsoft.Json.Converters; //把Json字符串反序列化为对象目标对象 = JsonConvert.Deserial ...

  7. DataTable 分批处理,每批处理4行

    ZZ -- /// <summary> /// 分批处理. /// </summary> public void PartialProc() { ;//每个datatable行 ...

  8. 2013-8-6 ubuntu基本操作

    1,apt-get下载文件默认安装路径 apt-get 下载后,软件所在路径是什么?? /var/cache/apt/archives ubuntu 默认的PATH为 PATH=/home/brigh ...

  9. 关于office2016桌面新建不显示execl问题

    在百度,google找了很多方法都没有文档可以解决此问题,office2016安装完在新建是由ececl的,应该是我用了清理注册表工具,对execl项进行清理,造成没有execl,所以贴出原版xls, ...

  10. java study2

    Intellj小技巧 数组 1.java数组元素类型是唯一的,即一个数组只能存储一种数据类型的数据,而不能存储多种数据类型的数据. 2.java数组的长度,一旦初始化完成,控件就被固定,即数组的长度将 ...