iOS開發--擷取時間

來源:互聯網
上載者:User

標籤:

取得當前的年月日,當前的時分秒獲得,周幾和星期幾獲得NSDate*date = [NSDate date];NSCalendar*calendar = [NSCalendar currentCalendar];NSDateComponents*comps; // 年月日獲得comps =[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit)                                      fromDate:date];NSIntegeryear = [comps year];NSIntegermonth = [comps month];NSIntegerday = [comps day];NSLog(@"year:%d month: %d, day: %d", year, month, day); //當前的時分秒獲得comps =[calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit)                                      fromDate:date];NSIntegerhour = [comps hour];NSIntegerminute = [comps minute];NSIntegersecond = [comps second];NSLog(@"hour:%d minute: %d second: %d", hour, minute, second); // 周幾和星期幾獲得comps =[calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit)                                      fromDate:date];NSInteger week = [comps week]; // 今年的第幾周NSIntegerweekday = [comps weekday]; // 星期幾(注意,周日是“1”,周一是“2”。。。。)NSIntegerweekdayOrdinal = [comps weekdayOrdinal]; // 這個月的第幾周NSLog(@"week:%d weekday: %d weekday ordinal: %d", week, weekday, weekdayOrdinal); NSDateFormatter*dateFormatter = [[NSDateFormatter alloc]init];       if(dateSwitch.on)       [dateFormattersetDateFormat:@"dd-MMM-yyy,hh:mm:ss"];       else       [dateFormatter setDateFormat:@"hh:mm:ss"];       labelTime.text = [dateFormatter stringFromDate:[NSDatedate]];       labelTime.font = [UIFontsystemFontOfSize:fontSlider.value];       [dateFormatter release];

  

 

 

//後台時間戳記轉化為yyyy年MM月dd日

  const long long dateTimeLong =[value longLongValue]/1000;

                

                NSDate *dateTime = [[NSDate alloc] initWithTimeIntervalSince1970:dateTimeLong];

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

                [formatter setTimeStyle:NSDateFormatterNoStyle];

                [formatter setDateStyle:NSDateFormatterMediumStyle];

                NSLocale *formatterLocal = [[NSLocale alloc] initWithLocaleIdentifier:@"en_us"];

                [formatter setLocale:formatterLocal];

                [formatter setDateFormat:@"yyyy年MM月dd日"];

                NSDate *new = [NSDate dateWithTimeInterval:0 sinceDate:dateTime];

                NSString *dateString = [formatter stringFromDate:new];

 

 

 

iOS開發--擷取時間

聯繫我們

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