函数内容

// 时间转为时间戳
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. VisualSVN安装配置与使用

    VisualSVN安装配置与使用 1.  所选服务器安装包:VisualSVN-Server-2.1.3.msi. 2.  客户端安装包:TortoiseSVN-1.6.2.16344-win32-s ...

  2. MongoDB注册Windows服务启动

    下载MongoDB安装到:E:\Work_App\MongoDB 这个目录 安装:E:\Work_App\MongoDB (安装在专门的目录中) 配置: 1.在E:\Work_App\MongoDB\ ...

  3. 单变量微积分笔记20——三角替换1(sin和cos)

    sin和cos的常用公式 基本公式: 半角公式: 微分公式: 积分公式: 三角替换 示例1 根据微分公式,cosxdx = dsinx 示例2 示例3 半角公式 示例1 示例2 解法1: 解法2: 综 ...

  4. 【转】每天一个linux命令(19):find 命令概览

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/13/2767374.html Linux下find命令在目录结构中搜索文件,并执行指定的操作.Li ...

  5. java 多线程之:join() 方法

    join()介绍 join() 定义在java.lang.Thread中. join() 的作用:让"主线程"等待"子线程"结束之后才能继续运行.

  6. linux 下git使用教程

    #添加所有新增文件 git add . #提交所有修改,包括删除,添加,修改 git add -A git add --all #查看状态 git status #添加一个文件 git add rea ...

  7. POJ1191棋盘分割

    题目:http://poj.org/problem?id=1191 1.分析式子!!! 发现xba是定值,σ的大小仅和∑ xi^2 有关.故dp条件是平方和最小. 2.分出一块就像割掉一条,只需枚举从 ...

  8. UI设计心得

    旁观型ui.追求一种无所不在,同时低调退隐的,奢华的存在感.内容由用户自己去搜索,浏览,构建,召唤,或是随着信息世界的某种外界趋势自然产生,ui作为始终凌驾于用户之上的高高在上的守护神,随时起到中承, ...

  9. 异步FIFO跨时钟域亚稳态如何解决?

    跨时钟域的问题:前一篇已经提到要通过比较读写指针来判断产生读空和写满信号,但是读指针是属于读时钟域的,写指针是属于写时钟域的,而异步FIFO的读写时钟域不同,是异步的,要是将读时钟域的读指针与写时钟域 ...

  10. C++进阶--const和函数(const and functions)

    // const和函数一起使用的情况 class Dog { int age; string name; public: Dog() { age = 3; name = "dummy&quo ...