NSDate,NSDateFormatter,NSLocale

1.static NSString* const KDateFormat=@"MM-dd-yyyy";

   NSString* dateStr=@"8-1-2000";

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

   dateFormat.dateFormat=KDateFormat;

    NSDate* date=[dateFormat dateFromString:dateStr];
    NSTimeInterval interval=[date timeIntervalSince1970]//timeIntervalSince1970返回的是单位是秒

    static NSString* const KTimeFormat=@"HH:mm:ss"; 小时成大写HH是24小时制,小写hh是12小时制。


2. NSLocale 本地化

    NSLog(@"NSLocaleCountryCode:%@",[[NSLocale currentLocale] objectForKey:NSLocaleCountryCode]);//US,ZH
    NSLog(@"NSLocaleLanguageCode:%@",[[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode]);//en,cn

   结果不受国际化中的语言设置而改变。它受通用/多语言环境/区域格式中的设置影响。它的意思可能是NSLocaleCountryCode国家,使用NSLocaleLanguageCode语言。


   取国际化中的语言设置

   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *languages = [defaults objectForKey:@"AppleLanguages"];
    NSString *currentLanguage = [languages objectAtIndex:0];
    NSLog(@"currentLanguage:%@",currentLanguage);//es,en,zh-Hans


3.时间范围
        iPickerView = [[UIDatePicker alloc] initWithFrame:CGRectZero];
        iPickerView.datePickerMode=UIDatePickerModeDate;
        double timeInterval=-60*60*24*365*50;
        NSDate* minimumDate=[NSDate dateWithTimeIntervalSinceNow:timeInterval];
        minimumDate=[NSDate dateWithTimeInterval:timeInterval sinceDate:minimumDate];//多次使用dateWithTimeInterval
        iPickerView.minimumDate=minimumDate;//100年前
        iPickerView.maximumDate=[NSDate dateWithTimeIntervalSinceNow:0];//现在


4.格式化日期
    《iPhone开发秘籍(第2版)》11.25格式化日期,表11-1 NSDateFormatter类的默认格式代码。


浮点数格式化四舍五入问题

id str=[NSString stringWithFormat:@"%.2f",1.999];  结果str为2.00

使用NSNumberFormatter可设置更多如何对数字格式化。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值