最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
IOS 时间和时间戳之间转化示例
时间:2022-06-25 23:31:06 编辑:袖梨 来源:一聚教程网
以毫秒为整数值的时间戳转换
时间戳转化为时间NSDate
| 代码如下 | 复制代码 |
- (NSString *)timeWithTimeIntervalString:(NSString *)timeString { // 格式化时间 NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; formatter.timeZone = [NSTimeZone timeZoneWithName:@"shanghai"]; [formatter setDateStyle:NSDateFormatterMediumStyle]; [formatter setTimeStyle:NSDateFormatterShortStyle]; [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm"];
// 毫秒值转化为秒 NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0]; NSString* dateString = [formatter stringFromDate:date]; returndateString; } | |
时间转化为时间戳
| 代码如下 | 复制代码 |
// 当前时间 NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0]; NSTimeInterval a=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒,不乘就是精确到秒 NSString *timeString = [NSString stringWithFormat:@"%.0f", a];//转为字符型 | |
通过比较时间与当前时间返回年月日的方法
| 代码如下 | 复制代码 |
- (void)getBabyDetailAge:(NSString *)date { // 获得日期对象 NSDateFormatter *formatter_ = [[NSDateFormatter alloc] init]; formatter_.dateFormat = @"yyyy-MM-dd HH:mm:ss"; NSDate *createDate = [formatter_ dateFromString:date];
NSCalendar *gregorian = [[ NSCalendar alloc ] initWithCalendarIdentifier : NSCalendarIdentifierGregorian]; NSUInteger unitFlags = NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear; NSDateComponents *components = [gregorian components:unitFlags fromDate:createDate toDate:[NSDate date] options: 0 ];
NSInteger years = [components year]; NSInteger months = [components month ]; NSInteger days = [components day ]; } | |
相关文章
- TikTok国际版官网下载入口-安全访问全球版TikTok 12-15
- 新三国志曹操传马超无双试炼四通关攻略 12-15
- 俄罗斯YANDEX引擎官网入口-免登录直达Yandex首页 12-15
- 漫蛙ManWa在线免费阅读入口_漫蛙ManWa官方网页版一键直达 12-15
- 逃离鸭科夫仓库区地窖在哪 仓库区地窖入口位置详情 12-15
- 光与影33号远征队熙艾尔bd玩法:熙艾尔毕业装备与加点思路介绍 12-15