[f]聊天的时间格式化
代码如下:
参数:
t: 时间戳, type:返回格式(1:IM界面,其他:会话列表)
function formartTime(t, type) {
var oldtime = new Date(t);
var date = new Date();
var today = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime(); //今天凌晨
var yestday = new Date(today - 24 * 3600 * 1000).getTime();
var beforeYestday = new Date(today - 24 * 3600 * 1000 * 2).getTime();
var beforeWeek = new Date(today - 24 * 3600 * 1000 * 7).getTime();
var Y = oldtime.getFullYear(); //年份
var M = oldtime.getMonth() + 1; //月份
var d = oldtime.getDate(); //日
var h = oldtime.getHours() % 12 == 0 ? 12 : oldtime.getHours() % 12; //12小时
var H = oldtime.getHours(); //24小时
var m = oldtime.getMinutes(); //分
var w = toWeek(oldtime.getUTCDay()); //星期
var timesolt = toTimeSolt(oldtime.getHours()); //时间段
timeStr = '';
//当天
if (oldtime.getTime() > yestday) {
timeStr = H + ':' + m;
}
//昨天
if (oldtime.getTime() < today && yestday <= oldtime.getTime()) {
timeStr = '昨天 ' + (type == 1 ? H + ':' + m : '');
}
// 一周内
if (oldtime.getTime() < yestday && beforeWeek <= oldtime.getTime()) {
timeStr = w + (type == 1 ? ' ' + H + ':' + m : '');
}
// 一周前
if (oldtime.getTime() < beforeWeek) {
timeStr = type == 1 ? Y + '年' + M + '月' + d + '日 ' + timesolt + ' ' + H + ':' + m : Y + '/' + M + '/' + d;
}
// 比当前时间晚
if (oldtime.getTime() > date.getTime()) {
timeStr = '手动修改';
}
// 格式化时间段
function toTimeSolt(h) {
var bt = '';
if (0 <= h && h <= 3)
bt = '凌晨';
if (4 <= h && h <= 8)
bt = '早上';
if (9 <= h && h <= 11)
bt = '上午';
if (12 == h)
bt = '中午';
if (13 <= h && h <= 17)
bt = '下午';
if (18 <= h && h <= 23)
bt = '晚上';
return bt;
}
// 格式星期
function toWeek(w) {
var week = '';
switch (w) {
case 0:
week = '星期日';
break;
case 1:
week = '星期一';
break;
case 2:
week = '星期二';
break;
case 3:
week = '星期三';
break;
case 4:
week = '星期四';
break;
case 5:
week = '星期五';
break;
case 6:
week = '星期六';
break;
}
return week;
}
return timeStr;
}
使用:
[f]聊天的时间格式化的更多相关文章
- strftime 日期时间格式化
strftime() 函数根据区域设置格式化本地时间/日期,函数的功能将时间格式化,或者说格式化一个时间字符串. size_t strftime(char *strDest,size_t maxsiz ...
- PHPCMS V9调用时间标签 |日期时间格式化
PHPCMS V9 如何调用时间标签,下面分享常见的调用时间标签 |日期时间格式化 1.日期时间格式化显示: a标准型:{date('Y-m-d H:i:s', $rs['inputtime'])} ...
- Python学习笔记 (2) :字符串输出、操作、格式化和日期、时间格式化
一.字符串输出及运算 1.常用输出格式及方法 ')#单引号 ")#双引号 """)#三个引号 1234567890 1234567890 1234567890 ...
- C# 学习笔记(二) 时间格式化字符串
1. 以下4种时间格式化符号输出的固定时间格式在各个区域设置中都应是相同的: 标准格式字符串 由 DateTimeFormatInfo.InvariantInfo 属性定义 自定义格式字符串 “O”或 ...
- golang 时间戳 时间格式化 获取当前时间 timestamp 计算时间差
获取当前时间 func Now func Now() Time 1 Now returns the current local time. func (Time) UTC func (t Time) ...
- Java 学习 时间格式化(SimpleDateFormat)与历法类(Calendar)用法详解
基于Android一些时间创建的基本概念 获取当前时间 方式一: Date date = new Date(); Log.e(TAG, "当前时间="+date); 结果: E/T ...
- mysql format时间格式化说明
原文地址为:mysql format时间格式化说明 date_format(datetime,formatting)可以格式日期和时间(例如 YYYY-MM-DD HH:MM:SS)和(HH:MM:S ...
- javascript 时间格式化
添加扩展 //时间格式化扩展Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1 ...
- js时间格式化
const formatDate = timestamp => { const date = new Date(timestamp); const m = date.getMonth() + 1 ...
随机推荐
- Nagios监控
1.Nagios监控软件 Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态,交换机路由器等网络设置,打印机等.在系统或服务状态异常时发出邮件或短信报警 ...
- 知识点---animate()动画滞后执行的解决方案
jQuery动画: animate 容易出现连续触发.滞后反复执行的现象: 针对 jQuery 中 slideUp.slideDown.animate 等动画运用时出现的滞后反复执行等问题的解决方法有 ...
- mui-H5获取当前手机通讯录
mui.plusReady(function() { // 扩展API加载完毕,现在可以正常调用扩展API plus.contacts.getAddressBook(plus.contacts.ADD ...
- Java框架spring 学习笔记(二):Bean的作用域
Spring 框架Bean支持以下五个作用域: 下面介绍两种作用域,singleton和protoype singleton作用域 singleton作用域为默认作用域,在同一个ioc容器内getBe ...
- HTTP 错误码
HTTP 400 – 请求无效 HTTP 401.1 – 未授权:登录失败 HTTP 401.2 – 未授权:服务器配置问题导致登录失败 HTTP 401.3 – ACL 禁止访问资源 HTTP 40 ...
- html转化为图片下载
业务需求:按照客户要求把排课表转化为图片下载到本地.一个月到排课有很多.所以图片会很大 <!DOCTYPE html> <html lang="en"> & ...
- 微信小程序的开发
https://www.cnblogs.com/jackson-zhangjiang/p/9843696.html
- winform使用PrintDocument控件打印以及oledb驱动
代码,需要加入的控件:PrintDocument.PageSetupDialog.PrintDialog.PrintPreviewDialog.BackgroundWorker,控件的Document ...
- LCMapString/LCMapStringEx实现简体字、繁体字的转换。
c#环境下想要最小程度不使用第三方库.程序性能,于是选择了这个Windows API. 转载自https://coolong124220.nidbox.com/diary/read/8045380 对 ...
- 十六、IDEA创建一个maven工程
1.点击Create new Project 2.选择maven,使用的jdk,点击next; 3.填写GroupId和工程名,点击next; 4.默认不做其他修改,点击Finish; 5.创建成功