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 ...
随机推荐
- 根据IP获得城市信息(百度API的运用)
/** * 根据IP获取城市 * @param string $ip ip地址 * @return array * http://api.map.baidu.c ...
- sdut2165 Crack Mathmen (山东省第二届ACM省赛)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/svitter/article/details/24270265 本文出自:http://blog.c ...
- JUC集合之 ConcurrentSkipListSet
ConcurrentSkipListSet介绍 ConcurrentSkipListSet是线程安全的有序的集合,适用于高并发的场景. ConcurrentSkipListSet和TreeSet,它们 ...
- php curl上传文件$_FILES为空问题
php使用curl上传文件,代码如下: 发送的代码(完全是官方的示例) <?php /* http://localhost/upload.php:print_r($_POST);print_r( ...
- Microsoft Dynamics CRM 4.0 如何添加自定义按钮
一.通过导入导出ISV.Config(ISV配置),具体如下图: 先设置—>打开导出自定义项—>选择ISV配置—>选择导出所选自定义项 点击确定 保存到桌面,解压,用VS打开cust ...
- css-深入理解margin和padding
最近一阶段从新学习了css,发现真的有很多很多的地方都是空白的,今天我们来总结一下margin和padding的一些不为人知的秘密! 一利用float和margin实现布局 我们首先来实现一个两列示布 ...
- js中replace的用法(两种常用举例,还有好多用法不一一列举)
1.替换特定字符 <html><body> <script type="text/javascript"> var str="Visi ...
- [C#][MVC]DropDownListFor 默认值无法选中的 BUG
本文来自:https://www.cnblogs.com/craze/p/6124575.html 关于mvc中@Html.DropDownListFor和@Html.DropDownList默认值无 ...
- shell_sctipts: 删除mysql备份到最后7日
目前,mysql的备份文件,经过一周左右清理,手动清理会比较费事,所以写了一个简单脚本来实现. 前提介绍: mysql备份文件放在/usr/bak/sql里面,sql文件的备份名称格式为: mysql ...
- sed简单用法
参数说明: -e 以指定的script来处理输入的文本文件 -f 以指定的script文件来处理输入的文本文件 -h 显示帮助 -n 仅显示script处理后的结果 -V 显示版本信息 -v 反选 动 ...