NSDate

//    NSDate日期类
    NSDate *date = [NSDate date];
    NSLog(@"%@", date);//0时区
        //打印出来的格式 -- :: 时区
    
//    NSTimeInterval:时间间隔类,实质是double类型,单位是秒
    NSTimeInterval timeInterval = 60;
    NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:60];
    NSLog(@"%@", date1);
        //昨天的现在时刻
    
    NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:-24*60*60];
    NSLog(@"%@", date2);
        //明年的现在时刻
    NSDate *date3 = [NSDate dateWithTimeIntervalSinceNow:365*24*60*60];
    NSLog(@"%@", date3);
    
        //昨天的现在时刻  明年的现在时刻 的时间间隔是多少秒
    NSTimeInterval timeInterval2 = [date2 timeIntervalSinceDate:date3];
    NSLog(@"%lf", timeInterval2);
    
        //现在的时刻 197011日的时间间隔是多少秒
    NSTimeInterval timeInterval3 = [date timeIntervalSince1970];
     NSLog(@"%lf", timeInterval3);
    
        //时间戳:某一时刻与197011日的时间间隔
        //例如:服务器传过来的时间戳为1416882400
    
    NSDate *date4 = [NSDate dateWithTimeIntervalSince1970:1416882400];
    NSLog(@"%@", date4);
    
    NSDate *nowDate = [NSDate date];

//    NSDateFormatter , 日期格式类,

//yyyy:四位年份
//yy:后两位为年份

//MM:2位月份 :后两位为年份
//MM:位月份
2位月份

     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                    //设置日期格式
    [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];


        //设置时区
    NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:8 * 60 * 60];
    [formatter setTimeZone:timeZone];

    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"china"];//时区名字或地区名字
    [formatter setTimeZone:timeZone];
    
        //按照formatter规定的格式,把日期转成字符串
    NSString *timeString = [formatter stringFromDate:nowDate];
    NSLog(@"%@", timeString);
    
    
    将字符串"20140501 102318"  转换为日期格式
    NSString *time = @"20140501 102318";
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
    [formatter1 setDateFormat:@"yyyyMMdd hhmmss"];
    
    
    [formatter1 setTimeZone:[NSTimeZone timeZoneWithName:@"GTM"]];//GTM,格林尼治时区
    NSDate *date = [formatter1 dateFromString:time];
    NSLog(@"%@", date);

                                                     

[[NSDateFormatter alloc] init]; NSDateFormatter alloc] init];[[NSDateFormatter alloc] init]

 
原文地址:https://www.cnblogs.com/tian-sun/p/4309574.html