时间戳 得到(几分钟前,几小时前,几天前)
时间戳 得到(几分钟前,几小时前,几天前) (注意:是秒级 还是毫秒级)
//方式一 后台给的格式为yyyy-MM-dd HH:mm:ss.SSS
- (NSString *) compareCurrentTime:(NSString *)str
{
//把字符串转为NSdate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *timeDate = [dateFormatter dateFromString:str];
NSDate *currentDate = [NSDate date];
//得到两个时间差
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:timeDate];
long temp = 0;
NSString *result;
if (timeInterval/60 < 1)
{
result = [NSString stringWithFormat:@"刚刚"];
}
else if((temp = timeInterval/60) <60){
result = [NSString stringWithFormat:@"%ld分钟前",temp];
}
else if((temp = temp/60) <24){
result = [NSString stringWithFormat:@"%ld小时前",temp];
}
else if((temp = temp/24) <30){
result = [NSString stringWithFormat:@"%ld天前",temp];
}
else if((temp = temp/30) <12){
result = [NSString stringWithFormat:@"%ld月前",temp];
} else{
temp = temp/12;
result = [NSString stringWithFormat:@"%ld年前",temp];
}
return result;
}
//方式二 后台给的格式为 纯数字1352170595000(13位) 毫秒级
- (NSString *)updateTimeForRow:(NSString *)str {
// 获取当前时时间戳 1466386762.345715 十位整数 6位小数
NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970]; 秒级
// 创建歌曲时间戳(后台返回的时间 一般是13位数字)
NSTimeInterval createTime =[str floatValue]/1000; 得到秒级
// 时间差
NSTimeInterval time = currentTime - createTime;
//秒转分钟
NSInteger small = time / 60;
if (small == 0) {
return [NSString stringWithFormat:@"刚刚"];
}
if (small < 60) {
return [NSString stringWithFormat:@"%ld分钟前",small];
}
// 秒转小时
NSInteger hours = time/3600;
if (hours<24) {
return [NSString stringWithFormat:@"%ld小时前",hours];
}
//秒转天数
NSInteger days = time/3600/24;
if (days < 30) {
return [NSString stringWithFormat:@"%ld天前",days];
}
//秒转月
NSInteger months = time/3600/24/30;
if (months < 12) {
return [NSString stringWithFormat:@"%ld月前",months];
}
//秒转年
NSInteger years = time/3600/24/30/12;
return [NSString stringWithFormat:@"%ld年前",years];
}
时间戳 得到(几分钟前,几小时前,几天前)相关推荐
- php 小时时间戳,PHP时间戳函数(几分钟、几小时前、几天前等)
时间戳是我们在时间日期对比时常用到一个小功能,下面我先来给各位介绍strtotime时间戳转换的一些方法与利用它来做一个日期格式化的几分钟.几小时前.几天前的一个实例. 1.PHP时间戳函数将日期转化 ...
- 根据时间戳显示刚刚/几分钟前/几小时前/今天等等
规则是: 一分钟内显示刚刚 一小时内显示几分钟前 24小时内显示今天几点几分 24 - 48小时内显示昨天几点几分 一年内显示几月几日 一年前显示 年-月-日 function timestampFo ...
- flutter 几秒前, 几分钟前, 几小时前, 几天前...
Show me the code!!! class RelativeDateFormat {static final num ONE_MINUTE = 60000;static final num O ...
- 微信小程序-将时间转换成几秒前 几分钟前 几小时前 几天前等时间格式
描述: 显示消息时间为 几天前 几小时前 几分钟前:同时一年以上的日期直接显示YYY:MM:DD形式 效果: 方法实现: 可以在utils目录下建一个存放公共方法的文件,将getDateDiff方法放 ...
- PHP人性化时间显示,实现多少秒前,多少分钟前,多少小时前
现在很多网站的时间显示都很人性化,不再是单纯的年月日时分秒,而是根据数据更新的时间与当前时间进行比较,实现多少秒前,多少分钟前,多少小时前! //人性化时间显示 function formatTime ...
- python获取时间————前一天后一天前一小时后一小时前一分钟后一分钟前一秒后一秒
SSRQ='2022-04-08' SSRQ_datetime= datetime.datetime.strptime(SSRQ,'%Y-%m-%d') 字符串转换为时间类型 导入包 import d ...
- 使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
效果图 代码 utils.ts 通过引入utils.ts import dayjs from 'dayjs'; import 'dayjs/locale/zh-cn'; import relative ...
- 毫秒转换友好的显示格式【刚刚、几秒前,几小时,几天前(3天内) 时间格式化】
毫秒转换友好的显示格式[刚刚.几秒前,几小时,几天前(3天内) 时间格式化] /*毫秒转换友好的显示格式输出格式:21小时前@param {[type]} time [description]@ret ...
- 《21世纪机器人》——货物搬运工晨明号:重启前1小时
货物搬运工晨明号:重启前1小时 "看那些小家伙跑起来了." 当晨明号从第5晶圆厂慢慢推出的时候兰德尔说.这是因为余甜给清洗系统下发了命令.小机器人们一拥而上,开始打扫庞大的码头,清 ...
- 《21世纪机器人》一一2.7 货物搬运工晨明号:重启前1小时
本节书摘来自华章计算机<21世纪机器人>一书中的第2章,第2.7节,作者:[美]布莱恩·戴维·约翰逊(Brian David Johnson) 更多章节内容可以访问云栖社区"华章 ...
最新文章
- Java实现拼音转汉字
- Java修炼之路——基础篇——值传递
- JavaFX技巧5:可观察
- 上证50基金有哪些_定投基金(易方达上证50指数A)
- oracle:用户购买平台案例分析与优化
- SpringBoot 工程目录 整合mybatis-mysql(xml类型)
- 和大家分享2015年我逐步形成的六个管理认识
- 3.1 Zabbix企业级分布式监控系统 --- zabbix 安装
- 站在巨人的肩膀上——Linux信号量操作
- java 关机命令_(windows )定时关机命令
- python连接cx_oracle时报错DatabaseError: DPI-1047: 64-bit Oracle Client library cannot be loaded:
- 健康驿站房间线上预约系统网站分析
- Word文档,英文字母间隔非常大,解决方法
- 【2013】【论文笔记】利用graphene产生THz——
- win10 mysql 入站规则_WIn10防火墙入站规则设置无效
- 开源的Jshop微信小程序商城源码
- 3、浙江移动网页Post登陆分析RSA算法【Post/Js逆向笔记】
- Linux环境下脚本加密软件SHC的解密方法
- SQL 04 计算时间差(天数)
- 腾讯地图实现站点标记,及已标记的点回显在地图上