标签:
//1.显示当前时间
NSDate *date = [NSDate date];//获取当前的时间
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd a HH:mm:ss EEEE"];
NSLog(@"date = %@",[formatter stringFromDate:date]);
//输出结果是 date = 2015-10-09 下午 20:34:05 星期五
//得到昨天此时的时间
NSDate *dateOfYesterday = [NSDate dateWithTimeInterval:- 24 *3600 sinceDate:date];
NSLog(@"yesterday = %@",[formatter stringFromDate:dateOfYesterday]);
//输出 yesterday = 2015-10-08 下午 20:55:28 星期四
//2.将秒数常用时间格式显示
NSDate *dateS = [NSDate dateWithTimeIntervalSince1970:100];
NSDateFormatter *formatterS = [[NSDateFormatter alloc]init];
[formatterS setDateFormat:@"mm:ss"];
NSLog(@"100s = %@",[formatterS stringFromDate:dateS]);
//输出结果是 100s = 01:40
//3.自定义区域语言
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];// 天朝的是 zh_CN
NSLog(@"data = %@",[formatter stringFromDate:date]);
//输出 data = 2015-10-09 PM 20:43:11 Friday
//4. 将字符串转化位NSDate类型
NSString *dateString = @"2015-07-16 上午 11:52:49";
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
[formatter setDateFormat:@"yyyy-M-dd aHH:mm:ss"];
NSDate *dateFromString =[formatter dateFromString:dateString];
标签:
原文地址:http://www.cnblogs.com/YM872668663/p/4865138.html