我正在"2013-02-10 09:26:52 +0000"
使用[NSDate date]
.
我想要格式的日期,如"Feb 10"
.
任何人都可以建议正确dateFormat
吗?
我正在"2013-02-10 09:26:52 +0000"
使用[NSDate date]
.
我想要格式的日期,如"Feb 10"
.
任何人都可以建议正确dateFormat
吗?
它在我的情况下工作:
NSDateFormatter* dateFormatterBegin = [[NSDateFormatter alloc] init];
[dateFormatterBegin setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];
NSString *dateStringBegin = @"2013-02-10 09:26:52 +0000";
NSDate* datebegin = [dateFormatterBegin dateFromString:[dateStringBegin stringByReplacingOccurrencesOfString:@":" withString:@"" options:0 range:NSMakeRange(dateStringBegin.length-3, 1)]];
[dateFormatterBegin setDateFormat:@"MMM-dd"];
NSString *beginDate = [[NSString alloc] initWithFormat:@"%s%@","Start Date : ",[dateFormatterBegin stringFromDate:datebegin]];
NSLog(@"Your date is : %@",beginDate);
[dateFormatterBegin release];
[beginDate release];
使用 NSDateFormatter 类:
NSDateFormatter * myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:@"ddMMyy" options:0 locale:[NSLocale currentLocale]];
NSString * formattedDate = [myFormatter stringFromDate:[NSDate date]];
在这里,您可以选择 setDateFormat 的输出格式:https ://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
NSString *string = @"2013-02-10 09:26:52 +0000"; // The date in a string
//Convert the string to date with the same format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"]; // Set format
NSDate *date = [dateFormat dateFromString:string];
//Create the new date format using NSDateFormatter
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:@"MMM dd"]; // Set format
//Convert the date using dateFormat1
NSDate *formatDate= [dateFormat1 dateFromString:date]; // date with MMM dd
嗨,像这样设置日期格式化程序
[dateFormatter setDateFormat:@"MMM d, yyyy hh:mm:ss"];
希望它会工作