iOS中日期格式转换

1.NSString * -> NSDate *

- (nullableNSDate*)dateFromString:(NSString*)string;

例:

NSString *dataString = @"2017年11月12日";

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    dateFormatter.dateFormat = @"yyyy年MM月dd日";

    NSDate *destDate= [dateFormatter dateFromString:dataString];

    NSLog(@"destDate - %@",destDate);


2.NSDate * -> NSString *

- (NSString*)stringFromDate:(NSDate*)date;

例:

NSDate*date = [NSDatedate];

NSDateFormatter*fmt = [[NSDateFormatteralloc] init];

fmt.dateFormat =@"yyyy年MM月dd号 HH:mm:ss";

NSString*string = [fmt stringFromDate:date];


3.获取系统当前时间:

   NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    formatter.dateFormat = @"YYYY年MM月dd日";

    NSDate *nowDate = [NSDate date];

    NSString *currentTimeString = [formatter stringFromDate:nowDate];

你可能感兴趣的:(iOS中日期格式转换)