IOS --- 日期时间格式 转换

1、如何如何将一个字符串如“ 20110826134106”装化为任意的日期时间格式,下面列举两种类型:

    NSString* string =@"20110826134106";
        NSDateFormatter*inputFormatter = [[[NSDateFormatter alloc] init]autorelease];

        [inputFormattersetLocale:[[[NSLocale alloc] initWithLocaleIdentifier :@"en_US"]autorelease]];
        [inputFormattersetDateFormat:@"yyyyMMddHHmmss"];
        NSDate*inputDate = [inputFormatter dateFromString:string];
        NSLog(@"date= %@", inputDate);
       
        NSDateFormatter*outputFormatter = [[[NSDateFormatter alloc] init]autorelease]; 
        [outputFormattersetLocale:[NSLocale currentLocale]];
        [outputFormattersetDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
        NSString*str = [outputFormatter stringFromDate:inputDate];
        NSLog(@"testDate:%@",str);
两次打印的结果为:
 
      date= 2011-08-26 05:41:06 +0000
 
      testDate:2011年08月26日13时41分06秒

2、iOS-NSDateFormatter 格式说明:
G: 公元时代,例如AD公元
        yy:年的后2位
        yyyy:完整年
        MM:月,显示为1-12
        MMM:月,显示为英文月份简写,如 Jan
        MMMM:月,显示为英文月份全称,如 Janualy
        dd:日,2位数表示,如02
        d:日,1-2位显示,如 2
        EEE:简写星期几,如Sun
        EEEE:全写星期几,如Sunday
        aa:上下午,AM/PM
        H:时,24小时制,0-23
        K:时,12小时制,0-11
        m:分,1-2位
        mm:分,2位
        s:秒,1-2位
        ss:秒,2位
        S:毫秒

常用日期结构:
yyyy-MM-dd HH:mm:ss.SSS
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
MM dd yyyy

3:新浪微博接口中,取得日期字符串 的转换

NSString*dateStr = @"Wed May 222:27:08+0800 2012";

     NSDateFormatter* formater =[[NSDateFormatter alloc] init];

   [formater setDateFormat:@"EEE MMM d HH:mm:sszzzz yyyy"];
    NSDate* date = [formaterdateFromString:dateStr];
    NSLog(@"%@",date);
   
    //NSDate* now = [NSDatenow];
    double inter = fabs([datetimeIntervalSinceNow]);
    if( inter <60)
       NSLog(@"1 mins ago!");
    else if(inter< 60*60)
       NSLog(@"1 hours ago!");
    else if(inter< 60*60*24)
       NSLog(@"1 days ago!");
       NSLog(@"interval is %f min", inter/60);

  上面代码在真机上运行后,发现取得date为NULL,模拟器正常显示,上网搜索后发现需要设置local, 果然设置后,真机正常

   NSLocale* local =[[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"] autorelease];

   [formatter setLocale: local];

4、自定义显示的 星期 格式

   使用NSDateFormatter转换日期时,得到的英文字母的星期几只能是这样,如Sun, Mon, etc.

   如果想得到大写字母的星期几,可以这样:


    NSArray*weekdayAry = [NSArray arrayWithObjects:@"SUN", @"MON", @"TUE",@"WED", @"THU", @"FRI", @"SAT", nil];


   dateFormatter = [[NSDateFormatter alloc] init];


   [dateFormatter setDateFormat:NSLocalizedString(@"YYYY.MM.dd.eee",nil)];


    //此处更改显示的大写字母的星期几
 
  [dateFormattersetShortWeekdaySymbols:weekdayAry];


   [dateFormatter setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"] ]];


    NString *str= [dateFormatter stringFromDate:[NSDate date]];

 

5计算距离某一天还有多少时间

 

NSDate* toDate   = [ [ NSDate alloc]initWithString:@"2012-9-29 0:0:00 +0600" ];  

NSDate*  startDate  = [ [ NSDatealloc] init ];  

NSCalendar* chineseClendar = [ [ NSCalendar alloc ]initWithCalendarIdentifier:NSGregorianCalendar ]; 

 

NSUInteger unitFlags =  

NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit |NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit; 

 

NSDateComponents *cps = [chineseClendar components:unitFlagsfromDate:startDate toDate:toDate  options:0];  

 

NSInteger diffHour = [cps hour];  

NSInteger diffMin    = [cpsminute];  

NSInteger diffSec   = [cps second]; 

NSInteger diffDay   = [cps day]; 

NSInteger diffMon  = [cps month]; 

NSInteger diffYear = [cps year];  

 

NSLog(  @" From Now to %@, diff: Years:%d  Months: %d, Days; %d, Hours: %d, Mins:%d,sec:%d",   

[toDate description], diffYear, diffMon, diffDay, diffHour,diffMin,diffSec );  

   
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值