关于NSDate时间的基础使用

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取本地时间
    NSDate *date = [NSDate date];
    NSLog(@"当前时间%@",date);
    //格式化 时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat : @"yyyy-MM-dd HH:mm:ss"];
    NSString *string;
    //转换成字符串
    string = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
    NSLog(@"%@",string);
    NSString *str;
    //距离1970多少201323323333毫秒之后的时间
    str = [NSString stringWithFormat:@"%@",[formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:201323323333]]];
    NSLog(@"%@",str);
    //获取当前时间距离1970的毫秒数
    UInt64 recordTime = [[NSDate date] timeIntervalSince1970]*1000;
    NSLog(@"%llu",recordTime);
    
    
    
}
// 转换UTC时间格式 传入date UTC格式
- (NSString *)formatDateWithUTC:(NSDate *)date
{
    
    NSString *str = [NSString stringWithFormat:@"%@",date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //输入格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    NSDate *dateFormatted = [dateFormatter dateFromString:str];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *locationTimeString=[dateFormatter stringFromDate:dateFormatted];
    return locationTimeString;
}
//北京时间  格式转换
- (NSDate *)beijingdate:(NSString *)createDate{
    NSDate *date;
    if ([createDate hasSuffix:@"Z"]) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //输入格式
        [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SZ"];
        NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
        [dateFormatter setTimeZone:localTimeZone];
        
        date  = [dateFormatter dateFromString:createDate];
        
    }
    return date;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end