码迷,mamicode.com
首页 > 移动开发 > 详细

IOS--时间 NSDate,NSTimeInterval的一些转换

时间:2017-09-27 17:32:14      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:转换   test   nsstring   date   div   att   amp   sage   class   

 1 //1.时间格式
 2 NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init];
 3 [_dateFormatter setDateFormat:@"YYYY年MM月dd天hh小时mm分ss秒"];
 4 
 5 //2.获取系统时间
 6 NSString *date = [_dateFormatter stringFromDate:[NSDate date]];
 7 
 8 //3.NSDate转化为时间戳
 9 NSTimeInterval a=[date timeIntervalSince1970];
10 
11 //4.时间戳转化为NSDate
12 NSDate *stampDate = [NSDate dateWithTimeIntervalSince1970:timeInter];
13 
14 //4.NSDate转化为NSString
15 NSString* dateString = [_dateFormatter stringFromDate:date];
16 
17 //5.传入时间差,重置格式
18 //传入时间差,转换格式
19 - (NSString *)timeWithMessageString:(NSTimeInterval)timeInter
20 {
21     
22     int month = timeInter / (3600 * 24 * 30);
23     int day = timeInter / (3600 * 24);
24     int hour = timeInter / 3600;
25     int minute = timeInter / 60;
26     
27     int day_process = day - month * 30;
28     int hour_process = hour - day *24;
29     int minute_process = minute - hour *60;
30     int miao_process = timeInter - minute*60;
31     
32     NSString *timedate = nil;
33     if (day == 0) {
34         timedate = [NSString stringWithFormat:@"%d小时%d分%d秒",hour_process,minute_process,miao_process];
35         if (hour == 0) {
36             timedate = [NSString stringWithFormat:@"%d分%d秒",minute_process,miao_process];
37             if (hour == 0) {
38                 timedate = [NSString stringWithFormat:@"%d秒",miao_process];
39             }
40         }
41     }else{
42         timedate = [NSString stringWithFormat:@"%d天%d小时%d分%d秒",day_process,hour_process,minute_process,miao_process];
43     }
44     
45     
46     timedate = [NSString stringWithFormat:@"限时优惠: 还剩%@",timedate];
47     return timedate;
48 }

 

IOS--时间 NSDate,NSTimeInterval的一些转换

标签:转换   test   nsstring   date   div   att   amp   sage   class   

原文地址:http://www.cnblogs.com/qiyiyifan/p/7602639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!