ios开发中UIDatePicker 的一些用法

例子

 代码如下 复制代码


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

  NSDate *selected = [datePicker date]; NSLog(@"date: %@", selected); 
}

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

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

NSDate的常用用法

1. 创建或初始化可用以下方法

   用于创建NSDate实例的类方法有

+ (id)date;

   返回当前时间

+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;

 

   返回以当前时间为基准,然后过了secs秒的时间

+ (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;

   返回以2001/01/01 GMT为基准,然后过了secs秒的时间

+ (id)dateWithTimeIntervalSince1970:(NSTimeInterval)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)addTimeInterval:(NSTimeInterval)secs;

   返回以目前的实例中保存的时间为基准,然后过了secs秒的时间

   用于初始化NSDate实例的实例方法有

- (id)init;

   初始化为当前时间。类似date方法

- (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;

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

- (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate;

   初始化为以refDate为基准,然后过了secs秒的时间

- (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;

   初始化为以当前时间为基准,然后过了secs秒的时间

2. 日期之间比较可用以下方法

- (BOOL)isEqualToDate:(NSDate *)otherDate;

   与otherDate比较,相同返回YES

- (NSDate *)earlierDate:(NSDate *)anotherDate;

   与anotherDate比较,返回较早的那个日期

 - (NSDate *)laterDate:(NSDate *)anotherDate;

   与anotherDate比较,返回较晚的那个日期

- (NSComparisonResult)compare:(NSDate *)other;

   该方法用于排序时调用:

     . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame

     . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending

     . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending

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

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)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″

关于NSTimeZone

 代码如下 复制代码

NSTimeZone* timename = [ [ NSTimeZone alloc] initWithName:names ];
NSArray *timeZoneNames = [NSTimeZone knownTimeZoneNames];
NSDate* nowDate = [ NSDate date ];
for( NSString* names  in timeZoneNames )
{
   NSTimeZone* timename = [ [ NSTimeZone alloc] initWithName:names ];
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init ];
   [outputFormatter setDateFormat:@"dd/MM/YYYY HH:mm:ss "];
   [outputFormatter setTimeZone:timename ];
   NSString *newDateString = [outputFormatter stringFromDate:nowDate];
    NSLog( @"/nZone: %@,%@",[ timename name ] ,newDateString );
   //[ timename name ],[ nowDate descriptionWithCalendarFormat:@"%d/%m/%y, %H:%M:%S %z"
   //     timeZone:timename
   //     locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]  );
   [ outputFormatter release ];
   [ timename release ];
}

----------------------------------------

NSDateFormatter *formatter=  [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSString *loctime = [formatter stringFromDate:date];
[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小时制。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值