将NSDate转换为NSString

如何将NSDate转换为NSString以便只将@ yyyy格式的年份输出到string?

怎么样…

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy"]; //Optionally for time zone conversions [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]]; NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance]; //unless ARC is active [formatter release]; 

我不知道我们怎么都错过了这个: localizedStringFromDate:dateStyle:timeStyle:

 NSString *dateString = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterFullStyle]; NSLog(@"%@",dateString); 

输出'13/06/12 00:22:39 GMT + 03:00'

希望通过提供包括年,月,日在内的正常格式化来增加更多的价值。 你可以使用这个格式化程序不止一年

 [dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss zzz"]; 

希望这有助于更多的人

干杯

networking上有一些NSDate帮手,我倾向于使用:

https://github.com/billymeltdown/nsdate-helper/

自述文件摘录如下:

  NSString *displayString = [NSDate stringForDisplayFromDate:date]; 

这产生以下种类的输出:

 '3:42 AM' – if the date is after midnight today 'Tuesday' – if the date is within the last seven days 'Mar 1' – if the date is within the current calendar year 'Mar 1, 2008' – else ;-) 

在Swift中:

 var formatter = NSDateFormatter() formatter.dateFormat = "yyyy" var dateString = formatter.stringFromDate(YourNSDateInstanceHERE) 
  NSDateFormatter *dateformate=[[NSDateFormatter alloc]init]; [dateformate setDateFormat:@"yyyy"]; // Date formater NSString *date = [dateformate stringFromDate:[NSDate date]]; // Convert date to string NSLog(@"date :%@",date); 

如果你没有NSDate -descriptionWithCalendarFormat:timeZone:locale: available(我不相信iPhone / Cocoa Touch包含这个),你可能需要使用strftime和猴子一些C风格的string。 您可以使用NSDate -timeIntervalSince1970NSDate获取UNIX时间戳。

 +(NSString*)date2str:(NSDate*)myNSDateInstance onlyDate:(BOOL)onlyDate{ NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; if (onlyDate) { [formatter setDateFormat:@"yyyy-MM-dd"]; }else{ [formatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; } //Optionally for time zone conversions // [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"..."]]; NSString *stringFromDate = [formatter stringFromDate:myNSDateInstance]; return stringFromDate; } +(NSDate*)str2date:(NSString*)dateStr{ if ([dateStr isKindOfClass:[NSDate class]]) { return (NSDate*)dateStr; } NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd"]; NSDate *date = [dateFormat dateFromString:dateStr]; return date; } 

如果你在Mac OS X上,你可以写:

 NSString* s = [[NSDate date] descriptionWithCalendarFormat:@"%Y_%m_%d_%H_%M_%S" timeZone:nil locale:nil]; 

但是这在iOS上不可用。

只需添加这个扩展名:

 extension NSDate { var stringValue: String { let formatter = NSDateFormatter() formatter.dateFormat = "yourDateFormat" return formatter.stringFromDate(self) } } 
 NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:myNSDateInstance]; NSInteger year = [components year]; // NSInteger month = [components month]; NSString *yearStr = [NSString stringWithFormat:@"%ld", year]; 

定义你自己的工具来格式化你的date所需的date格式为例如。

 NSString * stringFromDate(NSDate *date) { NSDateFormatter *formatter [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MM ∕ dd ∕ yyyy, hh꞉mm a"]; return [formatter stringFromDate:date]; }