JS时间转时间戳,时间戳转时间。时间显示模式。
函数内容
// 时间转为时间戳
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时间转时间戳,时间戳转时间。时间显示模式。的更多相关文章
- js 时间转成时间戳对比;My97DatePicker日历控件时间格式;Date.parse Firefox火狐浏览器返回Nan的解决办法
有个情况,我在显示时间的时候是需要显示为 2013年8月15日 14时28分15秒 但是假如我用js去获取到这个时间,并且想进行时间对比的时候,这个时间2013年8月15日 14时28分15秒根本就 ...
- js实现new Date(),时间对象和时间戳操作
1.js中实现时间date对象 var myDate = new Date();//获取系统当前时间,结果:Wed Aug 09 2017 00:00:00 GMT+0800 (中国标准时间) 2.获 ...
- 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换
var dateDiff = function (timestamp) { // 补全为13位 var arrTimestamp = (timestamp + '').split(''); for ( ...
- JS时间格式和时间戳的互转
//时间格式转为时间戳 function sjc(){ var date = new Date(); //时间对象 var str = date.getTime(); //转换成时间戳 } //时间戳 ...
- js时间格式化工具,时间戳格式化,字符串转时间戳
在开发中经常会用到时间格式化,有时候在网上搜索一大堆但不是自己想要的,自己总结一下,写一个时间格式化工具方便以后直接使用,欢迎大家来吐槽…… 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- js对时间戳的处理 获取时间,昨天,今天,明天,时间不同格式
1.获取昨天,今天,明天的时间 //昨天的时间 var day1 = new Date(); day1.setTime(day1.getTime()-24*60*60*1000); var s1 = ...
- vue ele 日期时间格式限制不能早于当天,时间转换成时间戳 进行比较
<el-date-picker value-format="yyyy-MM-dd HH:mm:ss" v-model=&quo ...
- mysql时间属性之时间戳和datetime之间的转换
一.datetime转换为时间戳 方案一:强制转换字段类型 use`nec`; ; ) NOT NULL COMMENT '注册时间' , ) NULL DEFAULT NULL COMMEN ...
- Python 之 时间字符串、时间戳、时间差、任意时间字符串转换时间对象
1. 时间字符串 --> 时间戳 1) time 模块 timestring = '2016-12-21 10:22:56' print time.mktime(time.strptime(ti ...
- 时间:UTC时间、GMT时间、本地时间、Unix时间戳
转自:http://blog.csdn.net/u012102306/article/details/51538574 1.UTC时间 与 GMT时间 我们可以认为格林威治时间就是时间协调时间(GMT ...
随机推荐
- GOOGLE高级搜索的秘籍
一.摘要 本文内容来源自互联网,全面的介绍Google搜索的各种功能和技巧. 二.GOOGLE简介 Google(http://www.google.com/)是一个搜索引擎,由两个斯坦福大学博士生L ...
- JS实现表单多文件上传样式美化支持选中文件后删除相关项
http://www.youdaili.net/javascript/5903.html
- 多线程安全问题之Lock显示锁
package com.hls.juc; import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.Reentr ...
- weex 知识点
使用 weex init [project_name] 创建的项目,执行 npm run dev 后,在 public/dist 文件夹里面就生成了两个对应的js,一个是index.web.js, 一 ...
- Queque 方法对比和分类
添加类:方法 public boolean add(E e) 增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常 bo ...
- postman环境变量的设置
相同的api接口因为部署环境不同,分为test和fromal 不仅重复,还容易出错 下面来介绍一下Postman的一个小技巧来解决这种问题: 设置环境变量 Tips: 不是在OS中设置环境变量哦 ...
- postman获取请求响应值
获取所有请求响应代码 var data = JSON.parse(responseBody); 把data的data值设置到token中 postman.setEnvironmentVari ...
- ORACLE删除某用户下所有对象
ORACLE删除某用户下所有对象 2013-10-26 15:50 4996人阅读 评论(1) 收藏 举报 --.sql脚本 --唯一注意的是下面的D:\dropobj.sql 为操作的.sql; ...
- C/C++基础----函数
用实参初始化形参时会忽略掉顶层const. 尽量使用常量引用,普通引用会限制所能接受的实参类型,也会给调用者误导. 管理数组实参的3种方法: 数组本身包含一个结束标记 传递指向数组首尾元素的指针 定义 ...
- 操作系统-移动操作系统-百科: iOS(苹果公司的移动操作系统)
ylbtech-操作系统-移动操作系统-百科: iOS(苹果公司的移动操作系统) iOS是由苹果公司开发的移动操作系统.苹果公司最早于2007年1月9日的Macworld大会上公布这个系统,最初是设计 ...