字符串转时间
iOS 中 NSDate 是时间管理类,这里获取到的时间是 UTC 时间,可以根据需要转换成任意地区的时间。
NSFormatter 类是时间格式化类,可以根据指定的格式、地区将指定的时间转换为 NSDate 对象
NSString* string = @"";
NSDateFormatter *inputFormatter = [[[NSDateFormatter alloc] init] autorelease];
[inputFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
[inputFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSDate* inputDate = [inputFormatter dateFromString:string];
时间格式转换
NSString *dateStr = @"2015-11-25 00:00";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSDate *date = [formatter dateFromString:dateStr]; formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy年MM月dd日"];
NSString *result = [formatter stringFromDate:date];
获取本地时间
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
// [formatter setLocale:[NSLocale currentLocale]];
NSString *result = [formatter stringFromDate:date];
获取年月日
NSDate*date = [NSDate date];
NSCalendar*calendar = [NSCalendar currentCalendar];
NSDateComponents*comps; // 年月日获得
comps =[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit) fromDate:date]; NSIntegeryear = [comps year];
NSIntegermonth = [comps month];
NSIntegerday = [comps day];
时间格式
yy: 年的后2位
yyyy: 完整年
MM: 月,显示为1-12
MMM: 月,显示为英文月份简写,如 Jan
MMMM: 月,显示为英文月份全称,如 Janualy
dd: 日,2位数表示,如02
d: 日,1-2位显示,如 2
EEE: 简写星期几,如Sun
EEEE: 全写星期几,如Sunday
aa: 上下午,AM/PM
H: 时,24小时制,0-23
K:时,12小时制,0-11
m: 分,1-2位
mm: 分,2位
s: 秒,1-2位
ss: 秒,2位
S: 毫秒
地区(local identifier)
地区码 | 地区 |
---|---|
eu | |
en_US | 美国 |
zh_Hant_HK | 中国香港 |
zh_Hans_CN | * |
zh_Hant_TW | 中国* |
IOS NSDate日期操作总结 // 当前时间创建NSDate NSDate *myDate = [NSDate date]; NSLog(@"myDate = %@",myDate); //从现在开始的24小时 NSTimeInterval secondsPerDay = **; NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay]; NSLog(@"myDate = %@",tomorrow); //根据已有日期创建日期 NSTimeInterval secondsPerDay1 = **; NSDate *now = [NSDate date]; NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1]; NSLog(@"yesterDay = %@",yesterDay); //比较日期 BOOL sameDate = [now isEqualToDate:yesterDay]; NSLog(@"sameDate = %lu",sameDate); 4.1//获取较早的日期 NSDate *earlierDate = [yesterDay earlierDate:now]; NSLog(@"earlierDate = %@",earlierDate); 4.2//较晚的日期 NSDate *laterDate = [yesterDay laterDate:now]; NSLog(@"laterDate = %@",laterDate); //两个日期之间相隔多少秒 NSTimeInterval secondsBetweenDates= [yesterDay timeIntervalSinceDate:now]; NSLog(@"secondsBetweenDates= %lf",secondsBetweenDates); //通过NSCALENDAR类来创建日期 NSDateComponents *comp = [[NSDateComponentsalloc]init]; [comp setMonth:]; [comp setDay:]; [comp setYear:]; NSCalendar *myCal = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *myDate1 = [myCal dateFromComponents:comp]; NSLog(@"myDate1 = %@",myDate1); //从已有日期获取日期 unsigned units = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit; NSDateComponents *comp1 = [myCal components:units fromDate:now]; NSInteger month = [comp1 month]; NSInteger year = [comp1 year]; NSInteger day = [comp1 day]; //NSDateFormatter实现日期的输出 NSDateFormatter *formatter = [[NSDateFormatteralloc]init]; [formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码 //或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"]; NSString *string = [formatter stringFromDate:now]; NSLog(@"string = %@",string); NSLog(@"formater = %@",formatter); //获取日期格式对象 - (NSDateFormatter *)dateFormatter { if (dateFormatter == nil) { dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; [dateFormatter setTimeStyle:NSDateFormatterNoStyle]; } return dateFormatter; } NSDate与NSDateFormatter的相关用法 .NSDateFormatter配合NSDate与NSString之间的转化
NSDateFormatter有下面2个方法:
- (NSString *)stringFromDate:(NSDate *)date;//NSDate转NSString
- (NSDate *)dateFromString:(NSString *)string;//NSString转NSDate e.g.
NSString *dateString=@"1900-01-01";
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date=[dateFormatter dateFromString:dateString];
[dateFormatter release]; NSString转NSDate与上面类似
NSString *dateString=[dateFormatter dateFromString:[NSDate date]]; .NSDateFormatter 的一些格式介绍
[dateFormatter setDateFormat:@"yyyy年MM月dd日#EEEE"];EEEE为星期几,EEE为周几
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setDateFormat:@"yyyy年MMMMd日"];//MMMM 为xx月,一个d可以省去01日前的0 .NSString转NSDate少一天的解决办法
用下面这种格式化方式
.[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; .
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];
NSDate *todayDate = [calendar dateFromComponents:components];