IOS学习笔记之十七 (NSDate、NSDateFormatter、NSCalendar、NSDateComponents、NSTimer)

1、NSDate

时间与日期的初始化,主要有

[NSDate date];

 
2、NSDateFormatter

 
主要用于NSString和Date之间相互转化

    //NSDate转NSString
     
    stringFromDate:dt
     
    //NSString转NSDate
     
    dateFromString

 
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转NSString
            NSDate *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转NSDate
            
            NSString *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);

更多请见:http://www.mark-to-win.com/tutorial/52001.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值