iOS NSDate等時間類的使用,iosnsdate使用

來源:互聯網
上載者:User

iOS NSDate等時間類的使用,iosnsdate使用
一、NSDate

  

    //iOS時間    //目前時間、預設0時區    NSDate *date = [NSDate date];    NSLog(@"目前時間date%@",date);        //NSDateFormatter是用來設定NSDate的格式    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    //設定為系統時區    formatter.timeZone = [NSTimeZone systemTimeZone];    //用來設定NSDate的輸出格式    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";    //用來講本地時間轉換成字串的形式輸出    NSString *timeStr = [formatter stringFromDate:[NSDate date]];    NSLog(@"本地時間 NSDate的輸出格式 %@",timeStr);

 

 除此之外,日期之間比較可用以下方法:

    //與otherDate比較,相同返回YES    - (BOOL)isEqualToDate:(NSDate *)otherDate;        //與anotherDate比較,返回較早的那個日期    - (NSDate *)earlierDate:(NSDate *)anotherDate;        //與anotherDate比較,返回較晚的那個日期    - (NSDate *)laterDate:(NSDate *)anotherDate;

 

 

二、NSDateFormatter

 

    //將字串轉換為日期對象.    NSString *dateStrs = @"2020-01-01-18:10:00";    NSDateFormatter *formatterTime = [NSDateFormatter new];    formatterTime.dateFormat = @"yyyy-MM-dd-HH:mm:ss";    formatterTime.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];    NSDate *dateTime = [formatterTime dateFromString:dateStrs];    NSLog(@"將字串轉換為日期對象date %@",dateTime);

 

   

  NSDateFormatter 格式化參數如下:    G: 公元時代,例如AD公元    yy: 年的後2位    yyyy: 完整年    MM: 月,顯示為1-12    MMM: 月,顯示為英文月份簡寫,如 Jan    MMMM: 月,顯示為英文月份全稱,如 Janualy    dd: 日,2位元表示,如02    d: 日,1-2位顯示,如 2    EEE: 簡寫星期幾,如Sun    EEEE: 全寫星期幾,如Sunday    aa: 上下午,AM/PM    H: 時,24小時制,0-23    K:時,12小時制,0-11    m: 分,1-2位    mm: 分,2位    s: 秒,1-2位    ss: 秒,2位    S: 毫秒    常用日期結構:    yyyy-MM-dd HH:mm:ss.SSS    yyyy-MM-dd HH:mm:ss    yyyy-MM-dd    MM dd yyyy

 

三、NSDate和時間戳記的相互轉換
    // NSDate 轉換成時間戳記    NSString *timeSp = [NSString stringWithFormat:@"%ld",(long)[[NSDate date] timeIntervalSince1970]];        NSLog(@"timeSp : %@", timeSp);        // 時間戳記轉換成NSDate    NSDate *currentTime = [NSDate dateWithTimeIntervalSince1970:[timeSp intValue]];        NSLog(@"currentTime : %@", currentTime);

 

四、NSCalendar
    //取到NSDate對象的各個部分  年 月 日 周    //1. 建立1個當前的日曆對象.    //   作用: 可以取到1個日期對象的各個部分.    NSCalendar *calendar = [NSCalendar currentCalendar];    //2. 指定日曆對象,要去取日期對象的那些部分.    NSDateComponents *comp =  [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:date];            //3. 通過NSDateComponents取到指定的日期的各個部分.    NSLog(@"%lu",comp.year);    NSLog(@"%lu",comp.month);    NSLog(@"%lu",comp.day);    NSLog(@"%lu",comp.weekday);

 

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.