zoukankan      html  css  js  c++  java
  • UIDatePicker 时间选择器

    1. NSDate *currentTime = [NSDate date];
    2. datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 100, 320, 216)];
    3. // [datePicker setTimeZone:[NSTimeZone defaultTimeZone]];
    4. // [datePicker setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+8"]];
    5. // 设置当前显示
    6. [datePicker setDate:currentTime animated:YES];
    7. // 设置显示最大时间(
    8. //[datePicker setMaximumDate:currentTime];
    9. // 显示模式
    10. [datePicker setDatePickerMode:UIDatePickerModeDateAndTime];
    11. // 回调的方法由于UIDatePicker 是UIControl的子类 ,可以在UIControl类的通知结构中挂接一个委托 
    12. [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; 
    13. [self.view addSubview:datePicker]; -(void)datePickerValueChanged:(id)sender
    14.   NSDate *selected = [datePicker date]; NSLog(@"date: %@", selected); 
    15. }
    复制代码


    日期范围属性中任何一个未被设置,则默认行为将会允许用户选择过去或未来的任意日期。这在某些情况下很有用处,比如,当选择生日时,可以是过去的任意日期,但终止与当前日期。

    选取器的高度始终是216像素

    关于NSDate:来源 http://hi.baidu.com/douxinchun/item/86c9732bc603349db73263ae
    NSDate的常用用法

    1. 创建或初始化可用以下方法
        
        用于创建NSDate实例的类方法有
        + (id)date;
        返回当前时间

        + (id)dateWithTimeIntervalSinceNowNSTimeInterval)secs;   
        返回以当前时间为基准,然后过了secs秒的时间

        + (id)dateWithTimeIntervalSinceReferenceDateNSTimeInterval)secs;
        返回以2001/01/01 GMT为基准,然后过了secs秒的时间

        + (id)dateWithTimeIntervalSince1970NSTimeInterval)secs;
        返回以1970/01/01 GMT为基准,然后过了secs秒的时间

        + (id)distantFuture;
        返回很多年以后的未来的某一天。
        比如你需要一个比现在(Now)晚(大)很长时间的时间值,则可以调用该方法。测试返回了4000/12/31 16:00:00

        + (id)distantPast;
        返回很多年以前的某一天。
        比如你需要一个比现在(Now)早(小)大很长时间的时间值,则可以调用该方法。测试返回了公元前0001/12/31 17:00:00

        用于创建NSDate实例的实例方法有
        - (id)addTimeIntervalNSTimeInterval)secs;
        返回以目前的实例中保存的时间为基准,然后过了secs秒的时间

        用于初始化NSDate实例的实例方法有
        - (id)init;
        初始化为当前时间。类似date方法

        - (id)initWithTimeIntervalSinceReferenceDateNSTimeInterval)secs;
        初始化为以2001/01/01 GMT为基准,然后过了secs秒的时间。类似dateWithTimeIntervalSinceReferenceDate:方法

        - (id)initWithTimeIntervalNSTimeInterval)secs sinceDateNSDate *)refDate;
        初始化为以refDate为基准,然后过了secs秒的时间

        - (id)initWithTimeIntervalSinceNowNSTimeInterval)secs;
        初始化为以当前时间为基准,然后过了secs秒的时间


    2. 日期之间比较可用以下方法
       
        - (BOOL)isEqualToDateNSDate *)otherDate;
        与otherDate比较,相同返回YES

        - (NSDate *)earlierDateNSDate *)anotherDate;
        与anotherDate比较,返回较早的那个日期

        - (NSDate *)laterDateNSDate *)anotherDate;
        与anotherDate比较,返回较晚的那个日期

        - (NSComparisonResult)compareNSDate *)other;
        该方法用于排序时调用:
          . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
          . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
          . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending

    3. 取回时间间隔可用以下方法

        - (NSTimeInterval)timeIntervalSinceDateNSDate *)refDate;
        以refDate为基准时间,返回实例保存的时间与refDate的时间间隔

        - (NSTimeInterval)timeIntervalSinceNow;
        以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔

        - (NSTimeInterval)timeIntervalSince1970;
        以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔

        - (NSTimeInterval)timeIntervalSinceReferenceDate;
        以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔


        + (NSTimeInterval)timeIntervalSinceReferenceDate;
        以2001/01/01 GMT为基准时间,返回当前时间(Now)与2001/01/01 GMT的时间间隔

    4. 将时间表示成字符串

        - (NSString *)description;
        以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示时间。
        其中 "±HHMM" 表示与GMT的存在多少小时多少分钟的时区差异。比如,若时区设置在北京,则 "±HHMM" 显示为 "+0800"


    1. NSTimeZone* timename = [ [ NSTimeZone alloc] initWithName:names ];
    2. NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
    3. NSDate* nowDate = [ NSDate date ];
    4. for( NSString* names in timeZoneNames )
    5. {
    6. NSTimeZone* timename = [ [ NSTimeZone alloc] initWithName:names ];
    7. NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init ];
    8. [outputFormatter setDateFormat:@"dd/MM/YYYY HH:mm:ss "];
    9. [outputFormatter setTimeZone:timename ];
    10. NSString *newDateString = [outputFormatter stringFromDate:nowDate];
    11. NSLog( @"/nZone: %@,%@",[ timename name ] ,newDateString );
    12. //[ timename name ],[ nowDate descriptionWithCalendarFormat:@"%d/%m/%y, %H:%M:%S %z" 
    13. // timeZone:timename 
    14. // locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]] );
    15. [ outputFormatter release ];
    16. [ timename release ];
    17. }
    复制代码


    1. NSDateFormatter *formatter= [[NSDateFormatter alloc] init];
    2. [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    3. NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    4. [formatter setTimeZone:timeZone];
    5. NSString *loctime = [formatter stringFromDate:date];
    6. [formatter release];
    复制代码

    现在loctime就是指定时区的时间字符串了

    无论用户设置的是12小时制还是24小时制,如何获得24小时制的时间

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

    [formattersetDateFormat"yyyy-MM-dd HH:mm:ss"];

    NSString *loctime = [formatter stringFromDate:date];

    [formatter release]

    这里要注意的是formatter的格式,如果是小写的"hh",那么时间将会跟着系统设置变成12小时或者24小时制。大写的"HH",则强制为24小时制。

    原文链接:http://www.cnblogs.com/zhidao-chen/archive/2013/05/17/3083488.html

     
     
     
     
    让明天,不后悔今天的所作所为
  • 相关阅读:
    鲍尔默称微软将投入数十亿美元打造数据中心业务(转)
    程序流程的辅助控制
    获取程序所在目录
    越界赋值
    长字串与宽字串
    长字串与短字串
    指针的运算
    Windows API 的数据类型与 Delphi 数据类型对照表
    给动态数组添加一个元素
    Delphi 与 C/C++ 数据类型对照表
  • 原文地址:https://www.cnblogs.com/-yun/p/5048960.html
Copyright © 2011-2022 走看看