我想做的是今天,昨天,本周,本周,本周,本月,上个月变量准备比较要在UITableView的titleForHeaderInSection上添加标题的NSDate
我想要的是在下面的代码中手动完成日期2009-12-11
NSDate *today = [NSDate dateWithString:@"2009-12-11 00:00:00 +0000"];
NSDate *yesterday = [NSDate dateWithString:@"2009-12-10 00:00:00 +0000"];
NSDate *thisWeek = [NSDate dateWithString:@"2009-12-06 00:00:00 +0000"];
NSDate *lastWeek = [NSDate dateWithString:@"2009-11-30 00:00:00 +0000"];
NSDate *thisMonth = [NSDate dateWithString:@"2009-12-01 00:00:00 +0000"];
NSDate *lastMonth = [NSDate dateWithString:@"2009-11-01 00:00:00 +0000"];
Run Code Online (Sandbox Code Playgroud) 有没有人知道是否有办法在NSCalendar上设置一周的第一天,或者是否有一个日历已经将星期一作为一周的第一天而不是星期日.我目前正在开发一款基于一周工作量的应用程序,它需要在星期一而不是星期天开始.我很可能会做一些工作来解决这个问题,但会有很多极端情况.我更喜欢平台为我做这件事.
提前致谢
这是我正在使用的一些代码.现在是星期六,所以我希望工作日是6而不是7,这意味着星期日将是7而不是0到0
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setFirstWeekday:0];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit;
NSDateComponents *todaysDate = [gregorian components:unitFlags fromDate:[NSDate date]];
int dayOfWeek = todaysDate.weekday;
Run Code Online (Sandbox Code Playgroud) 我有一个时间戳作为字符串,如:
星期四,2009年5月21日19:10:09 -0700
而且我想将它转换为像'20分钟前'或'3天前'的相对时间戳.
使用Objective-C为iPhone做到这一点的最佳方法是什么?
有没有人知道一个库或什么会将NSDate转换成字符串,如下面的例子?
1 hour ago
yesterday
last Thursday
2 days ago
last month
6 months ago
last year
Run Code Online (Sandbox Code Playgroud) 我需要向用户显示我的应用中的帖子日期,现在我以这种格式显示:"5月25日星期五".如何格式化NSDate以读取"2小时前"的内容?使其更加用户友好.
我需要一种方法来将NSTimeInterval
(以秒为单位的时间跨度)格式化为字符串,以产生类似"大约10分钟前","1小时,20分钟"或"小于1分钟"的内容.
-(NSString*) formattedTimeSpan:(NSTimeInterval)interval;
Run Code Online (Sandbox Code Playgroud)
目标平台是iOS.欢迎使用示例代码.
iphone ×4
cocoa ×3
nsdate ×3
time ×3
objective-c ×2
calendar ×1
comparison ×1
date ×1
date-format ×1
datetime ×1
formatting ×1
ios ×1
nscalendar ×1
nsstring ×1
weekday ×1