ios開發中UIDatePicker 的一些用法

來源:互聯網
上載者:User

例子

 代碼如下 複製代碼


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小時制。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.