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;

    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM