IOS学习笔记之十七 (NSDate、NSDateFormatter、NSCalendar、NSDateComponents、NSTimer)
生活随笔
收集整理的這篇文章主要介紹了
IOS学习笔记之十七 (NSDate、NSDateFormatter、NSCalendar、NSDateComponents、NSTimer)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、NSDate
時間與日期的初始化,主要有
[NSDate date];?
2、NSDateFormatter
?
主要用于NSString和Date之間相互轉化 //NSDate轉NSStringstringFromDate:dt//NSString轉NSDatedateFromString?
?
?
?
?
?
3、NSCalendar和NSDateComponents
Date打印出具體時間的年月日和把年月日轉化為Date?
?
4、NSTimer
定時器
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
?
?
?
5、測試Demo
main.m文件
int main(int argc, char * argv[]) {@autoreleasepool {//NSDate常用方法NSDate *date = [NSDate date];NSLog(@"%@", date);NSDate *date1 = [[NSDate alloc] initWithTimeIntervalSinceNow:3600 * 24];NSLog(@"date1 is %@", date1);NSDate *date2 = [[NSDate alloc] initWithTimeIntervalSinceNow:-3 * 3600 * 24];NSLog(@"date1 is %@", date2);NSLocale *cn = [NSLocale currentLocale];NSLog(@"%@", [date1 descriptionWithLocale:cn]);NSLog(@"%@", [date1 earlierDate:date2]);//NSDate轉NSStringNSDate *dt = [NSDate dateWithTimeIntervalSince1970:3600 * 24 * 366 * 20];NSDate *dt1 = [NSDate dateWithTimeIntervalSince1970:0];NSLog(@"%@", dt1);NSDateFormatter *df = [[NSDateFormatter alloc] init];[df setDateFormat:@"公元yyyy年MM月DD日 HH時mm分"];NSLog(@"%@", [df stringFromDate:dt]);//NSString轉NSDateNSString *str = @"2018-03-02";NSDateFormatter *df1 = [[NSDateFormatter alloc] init];[df1 setDateFormat:@"yyyy-MM-dd"];NSLog(@"%@", [df1 dateFromString:str]);//NSCalendar和NSDateComponents使用NSCalendar *car = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];NSDate *date3 = [NSDate date];unsigned unit = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit;NSDateComponents *comp = [car components:unit fromDate:date3];NSLog(@"現在是%ld年", comp.year);NSLog(@"現在是%ld月", comp.month);NSLog(@"現在是%ld日", comp.day);NSLog(@"現在是%ld時", comp.hour);NSLog(@"現在是%ld分", comp.minute);NSLog(@"現在是%ld秒", comp.second);NSLog(@"現在是星期%ld", comp.weekday);NSDateComponents *comp1 = [[NSDateComponents alloc] init];comp1.year = 2015;comp1.month = 4;comp1.day = 5;comp1.hour = 18;comp1.minute = 34;NSDate *date4 = [car dateFromComponents:comp1];NSLog(@"date is %@:", date4);return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));} }?
?
?
?
?
ViewControl.m文件(這里實現的定時器)
@implementation ViewController- (void)viewDidLoad {[super viewDidLoad]; // Control *control = [Control new]; // control.delegate = self; // [control willShowAlert];NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(Timered:) userInfo:nil repeats:YES]; }- (void)Timered:(NSTimer*)timer {NSLog(@"hello chenyu"); } @end?
?
?
?
?
6、運行的結果
2018-07-12 23:03:31.634870+0800 cyTest[37918:6787840] hello chenyu
2018-07-12 23:03:32.634915+0800 cyTest[37918:6787840] hello chenyu
2018-07-12 23:03:33.634881+0800 cyTest[37918:6787840] hello chenyu
2018-07-12 23:03:34.634938+0800 cyTest[37918:6787840] hello chenyu
2018-07-12 23:03:35.634998+0800 cyTest[37918:6787840] hello chenyu
2018-07-12 23:03:36.635095+0800 cyTest[37918:6787840] hello chenyu
2018-07-12 23:03:37.635086+0800 cyTest[37918:6787840] hello chenyu2018-07-12 23:14:23.789640+0800 cyTest[38218:6796894] Thu Jul 12 23:14:23 2018
2018-07-12 23:14:23.790844+0800 cyTest[38218:6796894] date1 is Fri Jul 13 23:14:23 2018
2018-07-12 23:14:23.791492+0800 cyTest[38218:6796894] date1 is Mon Jul 9 23:14:23 2018
2018-07-12 23:14:23.804598+0800 cyTest[38218:6796894] Friday, July 13, 2018 at 11:14:23 PM China Standard Time
2018-07-12 23:14:23.804853+0800 cyTest[38218:6796894] Mon Jul 9 23:14:23 2018
2018-07-12 23:14:23.805346+0800 cyTest[38218:6796894] Thu Jan 1 08:00:00 1970
2018-07-12 23:14:23.805927+0800 cyTest[38218:6796894] 公元1990年01月16日 08時00分
2018-07-12 23:14:23.808330+0800 cyTest[38218:6796894] Fri Mar 2 00:00:00 2018
2018-07-12 23:14:23.808982+0800 cyTest[38218:6796894] 現在是2018年
2018-07-12 23:14:23.809150+0800 cyTest[38218:6796894] 現在是7月
2018-07-12 23:14:23.809354+0800 cyTest[38218:6796894] 現在是12日
2018-07-12 23:14:23.809558+0800 cyTest[38218:6796894] 現在是23時
2018-07-12 23:14:23.809684+0800 cyTest[38218:6796894] 現在是14分
2018-07-12 23:14:23.809834+0800 cyTest[38218:6796894] 現在是23秒
2018-07-12 23:14:23.810407+0800 cyTest[38218:6796894] 現在是星期5
2018-07-12 23:14:23.811211+0800 cyTest[38218:6796894] date is Sun Apr 5 18:34:00 2015:
2018-07-12 23:14:27.764633+0800 cyTest[38218:6796894] hello chenyu
2018-07-12 23:14:28.764758+0800 cyTest[38218:6796894] hello chenyu
2018-07-12 23:14:29.765776+0800 cyTest[38218:6796894] hello chenyu
2018-07-12 23:14:30.765792+0800 cyTest[38218:6796894] hello chenyu
2018-07-12 23:14:31.765426+0800 cyTest[38218:6796894] hello chenyu
?
?
?
?
總結
以上是生活随笔為你收集整理的IOS学习笔记之十七 (NSDate、NSDateFormatter、NSCalendar、NSDateComponents、NSTimer)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux平台之如何查看svn账号
- 下一篇: IOS学习笔记十八(copy、mutab