2009-02-23 108 views

回答

448

......怎么

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]; 
+23

这将产生内存泄漏,因为格式化程序从未被释放。 – mana 2010-06-22 08:46:22

+5

不要在`NSDateFormatter`中使用`init`。它[在iOS 3.2之后被移除](http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/DeprecationAppendix/AppendixADeprecatedAPI.html%23//apple_ref/occ/instm/NSDateFormatter/INIT)。(如果你使用类方法,它会自动释放,你也不会有泄漏问题。) – zekel 2010-11-08 05:20:13

+2

永远不要忘记在END的[格式化程序发布]! – CastroAPZ 2010-03-15 10:19:58

4

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

82

希望通过提供正常的格式包括年,月,日与时间增添更多的价值。 您可以使用此格式的不仅仅是一年

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

希望这有助于更多的人

干杯 铝

23

有一些在网络上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 ;-) 
2

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

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

然而,这不适用于iOS。

267

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

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

输出'13/6月12日零时22分39秒GMT + 03:00'

6
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init]; 
    [dateformate setDateFormat:@"yyyy"]; // Date formater 
    NSString *date = [dateformate stringFromDate:[NSDate date]]; // Convert date to string 
    NSLog(@"date :%@",date); 
4
+(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; 
} 
11

在斯威夫特:

var formatter = NSDateFormatter() 
formatter.dateFormat = "yyyy" 
var dateString = formatter.stringFromDate(YourNSDateInstanceHERE) 
2

只需添加此扩展名:

extension NSDate { 
    var stringValue: String { 
     let formatter = NSDateFormatter() 
     formatter.dateFormat = "yourDateFormat" 
     return formatter.stringFromDate(self) 
    } 
} 
0
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]; 
0

定义自己的效用格式的日期所需的日期格式 为如。

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