我該如何轉換,NSDate
到NSString
這樣只有在@「yyyy」格式的年份被輸出到字符串?轉換NSDate到NSString
235
A
回答
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];
4
如果你沒有NSDate
-descriptionWithCalendarFormat:timeZone:locale:
可用(我不相信iPhone/Cocoa Touch包含這個),你可能需要使用strftime和猴子一些C風格的字符串。您可以使用NSDate -timeIntervalSince1970
從NSDate
獲取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];
}
相關問題
- 1. 轉換:NSString到NSDate
- 2. 轉換NSString到NSDate
- 3. NSString到NSDate轉換
- 4. NSString到NSDate轉換錯誤
- 5. 轉換的NSDate到的NSString
- 6. NSString到NSDate轉換問題
- 7. 轉換NSString-> NSDate?
- 8. iPhone - NSDate之間的轉換 - NSString - NSDate
- 9. 將NSString轉換爲NSDate
- 10. 從NSString問題轉換NSDate
- 11. 轉換的NSString的NSDate
- 12. 轉換的NSString的NSDate
- 13. 轉換的NSString的NSDate
- 14. 將NSString轉換爲NSDate
- 15. 將NSString轉換爲NSDate
- 16. 的NSString的NSDate轉換
- 17. 錯誤的轉換NSString到NSdate
- 18. NSString到NSDate轉換不起作用
- 19. 轉換NSString到NSDate,添加時間
- 20. 轉換NSDate到NSString保存在NSMutableArray
- 21. 將NSString轉換爲NSDate並返回到不同的格式NSString
- 22. NSString datetime到NSDate
- 23. 錯誤的NSString轉換成NSDate的
- 24. 將NSString轉換爲NSDate格式?
- 25. 如何將NSDate轉換爲NSString?
- 26. 無法將NSString正確轉換爲NSDate
- 27. 轉換的NSString日期NSDate的
- 28. 如何將一個NSString轉換爲NSDate?
- 29. 轉換的NSString的NSDate給出零
- 30. 如何將NSString轉換爲NSDate?
這將產生內存泄漏,因爲格式化程序從未被釋放。 – mana 2010-06-22 08:46:22
不要在`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
永遠不要忘記在END的[格式化程序發佈]! – CastroAPZ 2010-03-15 10:19:58