根据年月日时间换算到精确时分秒和周几
来源:互联网 发布:ipad怎么上淘宝卖家版 编辑:程序博客网 时间:2024/05/18 01:56
闲来无事看到微信列表右侧有显示时间,可以清晰的看出来具体什么时间收到的消息
现在我根据后台返回的时间年月日时分秒来转换时间demo地址:https://github.com/tuwanli/ShowTimeDemo
demo:http://download.csdn.net/detail/tuwanli125/9470326
后台请求的数据:
该建表的建表,该创建cell的创建cell,然后就是将数据显示在表里
主要转换的代码如下
+ (NSString*)weekDayStr:(NSDate *)format{ NSString *weekDayStr = nil; NSDateComponents *comps = [[NSDateComponents alloc] init]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSInteger unitFlags =NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitHour |NSCalendarUnitMinute | NSCalendarUnitSecond; comps = [calendar components:unitFlags fromDate:format]; NSString *str = [self description]; if (str.length >= 10) { NSString *nowString = [str substringToIndex:10]; NSArray *array = [nowString componentsSeparatedByString:@"-"]; if (array.count == 0) { array = [nowString componentsSeparatedByString:@"/"]; } if (array.count >= 3) { NSInteger year = [[array objectAtIndex:0] integerValue]; NSInteger month = [[array objectAtIndex:1] integerValue]; NSInteger day = [[array objectAtIndex:2] integerValue]; [comps setYear:year]; [comps setMonth:month]; [comps setDay:day]; } } NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDate *_date = [gregorian dateFromComponents:comps]; NSDateComponents *weekdayComponents = [gregorian components:NSCalendarUnitWeekday fromDate:_date]; NSInteger week = [weekdayComponents weekday]; week ++; switch (week) { case 1: weekDayStr = @"星期日"; break; case 2: weekDayStr = @"星期一"; break; case 3: weekDayStr = @"星期二"; break; case 4: weekDayStr = @"星期三"; break; case 5: weekDayStr = @"星期四"; break; case 6: weekDayStr = @"星期五"; break; case 7: weekDayStr = @"星期六"; break; default: weekDayStr = @""; break; } return weekDayStr; }+ (NSString *)remindlistTime:(NSString *)timeStr{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; NSDate* date = [formatter dateFromString:timeStr]; NSTimeInterval secondsPerDay = 24 * 60 * 60; NSDate *today = [[NSDate alloc] init]; NSDate *yesterday,*weekday; weekday = [today dateByAddingTimeInterval: -(secondsPerDay*7)]; yesterday = [today dateByAddingTimeInterval: -secondsPerDay]; // 10 first characters of description is the calendar date: NSString * todayString = [[today description] substringToIndex:10]; NSString * yesterdayString = [[yesterday description] substringToIndex:10]; NSString * dateString = [[date description] substringToIndex:10]; if ([dateString isEqualToString:todayString]) { return @"今天"; } else if ([dateString isEqualToString:yesterdayString]) { return @"昨天"; }else if ([weekday timeIntervalSince1970]>[date timeIntervalSince1970]){ NSArray *timeArr = [[[timeStr componentsSeparatedByString:@" "] objectAtIndex:0] componentsSeparatedByString:@"-"]; NSString *dayTime; NSString *monthlyTime; if ([[timeArr[2] substringToIndex:1] isEqualToString:@"0"]) { dayTime = [timeArr[2] substringFromIndex:1]; }else{ dayTime = timeArr[2]; } if ([[timeArr[1] substringToIndex:1] isEqualToString:@"0"]) { monthlyTime = [timeArr[1] substringFromIndex:1]; }else{ monthlyTime = timeArr[1]; } return [NSString stringWithFormat:@"%@/%@/%@",[(NSString *)timeArr[0] substringFromIndex:2],monthlyTime,dayTime]; } else { return [Tool weekDayStr:date]; } }
效果:
如果你发现你看不见我单元格的线,告诉你一个秘密,因为我用的iOS9的模拟器,像素太高,所以看不见,command+1就能看见了,哈哈
1 0
- 根据年月日时间换算到精确时分秒和周几
- solaris/unix/linux 获取系统时间的方法--精确到年月日时分秒
- 根据时间或时间戳获得时分秒年月日
- 使用正则表达式校验时间,精确到秒,包括年月日时分秒,14位格式精确校验,正则表达式精确校验时间
- ios开发 获取系统时间 取得当前的年月日,当前的时分秒获得,周几和星期几获得
- 年月日时分秒转化为秒 和获取当前时间
- Unix时间和年月日时分秒时间格式转换
- EasyUI时间插件----年月日,年月日时分秒
- html页面显示年月日时分秒和星期几
- js显示年月日时分秒以及周几
- js 中时间大小比较和合法性校验check(时分校验,年月日时分秒校验,年月日时分校验)
- 时间控件---带年月日时分秒
- js显示动态时间年月日时分秒
- 带有年月日时分秒的时间控件
- 时间格式 年月日时分秒毫秒
- 取时间的年月日时分秒
- FLex 获取当前时间 年月日 时分秒
- Delphi 获取时间的年月日时分秒
- NGUI相关注意事项
- TP框架微信支付目录
- 虚拟机和主机ping不通解决办法
- NSTimer 和runloop的问题,将timer加入到runloop----同时注意在viewwilldisappear里面设置为空和无效
- css中伪元素:before :after的作用
- 根据年月日时间换算到精确时分秒和周几
- 指针数组和数组指针的区别
- 自定义注解Demo
- iOS多线程简介
- Redis常用命令
- 学习JAVA之路(二、入门练习)
- MongoDB简单查询与复杂查询性能对比
- 【杭电oj】3790 - 最短路径问题(dijkstra)
- Android仿网易上下双导航简单实现