近期项目有类似QQ空间展示动态的UI,模仿了QQ空间的时间显示。在此记录,以备查阅。

这是QQ空间的ui:

时间显示为:

1.今天-->今天 xx:xx(今天 15:39)

2.昨天-->昨天 xx:xx(昨天 06:00)

3.前天-->前天 xx:xx(前天 19:00)

4.同一年, 比如:同一年的一月三号-->01-03 xx:xx(01-03 12:29)

5.不在同一年 --> xxxx-xx-xx(2014-12-12)

程序执行效果例如以下。下面为简单的測试。没有ui界面:


以下的代码是在网上已有代码的基础上更改而来,原代码点此查看
/**
* 仿QQ空间时间显示
* @param string eg:2015年5月24日 02时21分30秒
*/
- (NSString *)format:(NSString *)string{
NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] ];
[inputFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
NSDate*inputDate = [inputFormatter dateFromString:string];
//NSLog(@"startDate= %@", inputDate); NSDateFormatter *outputFormatter= [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[NSLocale currentLocale]];
[outputFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//get date str
NSString *str= [outputFormatter stringFromDate:inputDate];
//str to nsdate
NSDate *strDate = [outputFormatter dateFromString:str];
//修正8小时的差时
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: strDate];
NSDate *endDate = [strDate dateByAddingTimeInterval: interval];
//NSLog(@"endDate:%@",endDate);
NSString *lastTime = [self compareDate:endDate];
NSLog(@"lastTime = %@",lastTime);
return str;
} -(NSString *)compareDate:(NSDate *)date{ NSTimeInterval secondsPerDay = 24 * 60 * 60; //修正8小时之差
NSDate *date1 = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date1];
NSDate *localeDate = [date1 dateByAddingTimeInterval: interval]; //NSLog(@"nowdate=%@\nolddate = %@",localeDate,date);
NSDate *today = localeDate;
NSDate *yesterday,*beforeOfYesterday;
//今年
NSString *toYears; toYears = [[today description] substringToIndex:4]; yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
beforeOfYesterday = [yesterday dateByAddingTimeInterval: -secondsPerDay]; // 10 first characters of description is the calendar date:
NSString *todayString = [[today description] substringToIndex:10];
NSString *yesterdayString = [[yesterday description] substringToIndex:10];
NSString *beforeOfYesterdayString = [[beforeOfYesterday description] substringToIndex:10]; NSString *dateString = [[date description] substringToIndex:10];
NSString *dateYears = [[date description] substringToIndex:4]; NSString *dateContent;
if ([dateYears isEqualToString:toYears]) {//同一年
//今 昨 前天的时间
NSString *time = [[date description] substringWithRange:(NSRange){11,5}];
//其它时间
NSString *time2 = [[date description] substringWithRange:(NSRange){5,11}];
if ([dateString isEqualToString:todayString]){
dateContent = [NSString stringWithFormat:@"今天 %@",time];
return dateContent;
} else if ([dateString isEqualToString:yesterdayString]){
dateContent = [NSString stringWithFormat:@"昨天 %@",time];
return dateContent;
}else if ([dateString isEqualToString:beforeOfYesterdayString]){
dateContent = [NSString stringWithFormat:@"前天 %@",time];
return dateContent;
}else{
return time2;
}
}else{
return dateString;
}
}

IOS日期转为今天昨天形式的更多相关文章

  1. BIP_开发案例07_将原有Report Builer报表全部转为XML Publisher形式(案例)

    2014-05-31 Created By BaoXinjian

  2. R语言将5位数字日期转为正常日期

    R语言中用double表示日期,即从1970-1-1距离给定日期的天数,将5位数字日期转为正常日期格式的方法 as.Date(16543,origin='1970-1-1')

  3. placeholder的字体样式改变,滚动条的颜色改变,ios日期兼容

    placeholder:::-webkit-input-placeholder { color: rgba(153, 153, 153, 0.541);font-size:12px;}:-moz-pl ...

  4. 【C#】string格式的日期转为DateTime类型及时间格式化处理方法

    日期格式:yyyyMMdd HH:mm:ss(注意此字符串的字母大小写很严格) yyyy:代表年份 MM: 代表月份 dd: 代表天 HH: 代表小时(24小时制) mm: 代表分钟 ss: 代表秒 ...

  5. Java与IOS日期格式

    //JAVA日期格式 Date date = new Date(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM- ...

  6. PostgreSQL将日期转为当前年、月、日的函数date_trunc

    PostgreSQL将日期转为年.月.日的函数date_trunc: 当前年: select  date_trunc('year',now()) 当前月: select  date_trunc('mo ...

  7. IOS 日期的简洁格式展示

    首先我要解释一下标题的意义,日期的简洁格式展示,之所以简介,是因为让人一目了然,不需要思考是什么时候. 在详细一点就是我们在微信朋友圈中 所看到的时间格式. 例如:刚刚 -几分钟前-几小时前等等. 今 ...

  8. MySql中使用日期函数获取昨天的数据

    .body-classic{ color:#444; font-family:Georgia, Palatino, 'Palatino Linotype', Times, 'Times New Rom ...

  9. Swift iOS 日期操作:NSDate、NSDateFormatter

    1.日期(NSDate) // 1.初始化 // 初始化一个当前时刻对象 var now = NSDate() // 初始化一个明天当前时刻对象 var tomorrow = NSDate(timeI ...

随机推荐

  1. 62.nodejs中的ejs模板学习

    转自:https://blog.csdn.net/dongmelon/article/details/72403913 02.ejs.这是被include的文件 <script> cons ...

  2. vue2细节变化的用法

    1.v-el和v-ref合并为一个属性:ref 原来:v-el:my-element 现在: ref="myElement", v-ref:my-component 变成了这样:  ...

  3. 手机浏览器,微信浏览器对background-color不显示的问题

    PC上的浏览器可以正常显示,但是到了手机上就不显示了,古怪的问题花了我一晚上都没解决. 今天突然想到会不会是某些特立独行的了浏览器为了彰显个性,采用不同别人的解析方式呢? 我的原来CSS是这么写的: ...

  4. nginx配置静态文件服务器的一个特殊需求的探索和分享, nginx处理不同路径返回统一文件,nginx改写,跳转请求.

    最近在做一个前后端分离的个人博客,在做自己博客的时候有个想法,本来是打算用nginx作为静态文件服务器使用,django做后端程序. 我的前端页面用vue写的,结果用组件用嗨了,发现页面列表和 详情都 ...

  5. ZJU 2425 Inversion

    Inversion Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ID:  ...

  6. 调用中行接口针对返回报文(xml形式)做相关处理

    最近在对接中行银行接口,在获取返回报文的时候遇到一些问题,现在在这里做个总结 TIP: 在返回报文之前,要对前置机的URL请求,在这期间遇到一个坑,还是通过查看日志才发现问题 在填写转账信息的时候要求 ...

  7. [Unit testing] data-test attr FTW

    Most of time, we get used to use class name as a selector in the test. But one problem for this is c ...

  8. Irrlicht 3D Engine 笔记系列 之 教程5- User Interface

    作者:i_dovelemon 日期:2014 / 12 / 18 来源:CSDN 主题:GUI 引言 今天.博主学习了第五个教程. 这个教程解说了怎样使用Irrlicht内置的一个基础模块.GUI模块 ...

  9. 【LeetCode-面试算法经典-Java实现】【130-Surrounded Regions(围绕区域)】

    [130-Surrounded Regions(围绕区域)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a 2D board containing 'X ...

  10. js12---闭包,原型,继承

    <html> <body> <script type="text/javascript"> //闭包实现了函数层面多个子函数共享父类函数的属性. ...