Tim*_*Tim 21 iphone nsdateformatter
我有一个日期时间格式的字符串:"YYYY-MM-DD HH:MM:SS".
我在我的源代码中使用它:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"%e. %B %Y"];
NSString *test = [formatter stringFromDate:@"2010-01-10 13:55:15"];
Run Code Online (Sandbox Code Playgroud)
我想从"2010-01-10 13:55:15"转换为"2010年1月10日".但我的实施不起作用.
这有什么不对?
非常感谢提前和最诚挚的问候.
更新的源代码:
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"%Y-%m-%d %H:%M:%S"];
NSString *test1 = [formatter stringFromDate:@"2010-01-10 13:55:15"];
NSDateFormatter *formatter1 = [[[NSDateFormatter alloc] init] autorelease];
[formatter1 setDateFormat:@"%d. %M4 %Y"];
NSString *test2 = [formatter1 stringFromDate:test1];
Run Code Online (Sandbox Code Playgroud)
Nie*_*tle 33
日期格式化程序一次只能处理一种格式.你需要采取这种方法:
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [f dateFromString:@"2010-01-10 13:55:15"];
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:@"d. MMMM YYYY"];
NSString *s = [f2 stringFromDate:date];
Run Code Online (Sandbox Code Playgroud)
s现在将是"2010年1月10日"
以下是使用我的代码中的数据格式化程序的一些示例.您应该可以使用这些功能中的任何一个并根据您的格式进行调整.
用法
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [Constants getTitleDateFormatter];
NSString *dateString = [dateFormatter stringFromDate:today];
[dateFormatter release];
Run Code Online (Sandbox Code Playgroud)
功能
+ (NSDateFormatter *) getDateFormatterWithTimeZone {
//Returns the following information in the format of the locale:
//YYYY-MM-dd HH:mm:ss z (Z is time zone)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
return dateFormatter;
Run Code Online (Sandbox Code Playgroud)
}
+ (NSDateFormatter *)dateFormatterWithoutYear {
NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *format = [dateFormatter dateFormat];
format = [format stringByReplacingOccurrencesOfString:@"/yy" withString:@""];
NSRange secondSpace;
secondSpace.location = format.length-2;
secondSpace.length = 1;
format = [format stringByReplacingCharactersInRange:secondSpace withString:@""];
[dateFormatter setDateFormat:format];
return dateFormatter;
}
+ (NSDateFormatter *) dateFormatterMonthDayOnly {
NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *format = [dateFormatter dateFormat];
format = [format stringByReplacingOccurrencesOfString:@"/yy" withString:@""];
NSRange range;
range.location = 0;
range.length = 3;
format = [format substringWithRange:range];
[dateFormatter setDateFormat:format];
return dateFormatter;
}
+ (NSDateFormatter *) getTitleDateFormatter {
//Returns the following information in the format of the locale:
//MM-dd-yyyy hh:mm:ssa
NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *format = [dateFormatter dateFormat];
NSRange secondSpace;
secondSpace.location = format.length-2;
secondSpace.length = 1;
format = [format stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
format = [format stringByReplacingCharactersInRange:secondSpace withString:@""];
[dateFormatter setDateFormat:format];
return dateFormatter;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
32482 次 |
| 最近记录: |