- //
- // main.m
- // 日期函数
- //
- // Created by chenyf on 15/3/23.
- // Copyright (c) 2015年 chenyf. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- NSDate *now = [NSDate date];
- NSLog(@" the date is %@", now);
-
- double seconds = [now timeIntervalSince1970];
- NSLog(@"%f seconds has passed since 1970", seconds);
-
-
- NSDate *later = [now dateByAddingTimeInterval:100000];
- NSLog(@"In 100,000 seconds it will be %@", later);
-
- // 现在是一年中的第几个小时
- NSCalendar *cal = [NSCalendar currentCalendar];
-
- // NSHourCalendarUnit 和 NSYearCalendarUnit 已经废弃了
- // 请使用 NSCalendarUnitHour 和 NSCalendarUnitYear
- NSUInteger hour = [cal ordinalityOfUnit:NSCalendarUnitHour
- inUnit:NSCalendarUnitYear
- forDate:now];
- NSLog(@"今年已经过了 %lu 小时", (unsigned long)hour); // 也可以获取 一个月中的第几天
-
- }
- return 0;
- }
复制代码
|
|