IOS Date, NSDateFormatter, compare, dateFromString:mydatestr


小结:

1)日期格式化:NSDateFormatter,NSLocale,  "yyyy-MM-dd HH:mm:ss"

2)字符转换为日期;NSDate *mydate=[df dateFromString:mydatestr];

3)日期比较:compare,NSOrderedSame,NSOrderedAscending,NSOrderedDescending

 

NSDate *dateDay=[[NSDate alloc] init];

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

    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

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

    [df setLocale:locale];

   

    NSLog(@"dateDay =%@",dateDay);

    

    NSString *mydatestr=@"2012-09-10 18:20:00";

    NSDate *mydate=[df dateFromString:mydatestr];

    

      NSLog(@"mydate =%@",mydate);

    switch ([dateDay compare:mydate]) {

        case NSOrderedSame:

            NSLog(@"same");

            break;

         case NSOrderedAscending

            NSLog(@"dateday is earlier than mydate");

            break;

         case NSOrderedDescending:

            NSLog(@"dateday is later than mydate");

            break;

        default:

            NSLog(@"Bad date");

            break;

    }

 

posted @ 2012-07-16 15:47  大树2  阅读(3167)  评论(0编辑  收藏  举报