0

我正在使用以下代码从字符串中获取日期,但它没有显示正确的日期

NSDateFormatter *saveddf = [[NSDateFormatter alloc] init];

[saveddf setDateFormat:@"EEEE_MMMM dd_YYYY hh:mm a"];
[saveddf setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *savedtdate = [[NSDate alloc] init];
savedtdate = [saveddf dateFromString:datestring];



Saturday_June 22_2013 11:44 AM  -- INPUT (datestring).

2013-01-05 06:14:00 +0000 This is output i am getting.(savedtdate).

请告诉我的代码有什么问题。

4

2 回答 2

1

试试这段代码,它应该可以工作:

NSString *yourStringDate = @"Saturday_June 22_2013 11:44 AM";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"EEE_MMM dd_yyyy hh:mm a"];

NSDate *date = [dateFormatter dateFromString: yourStringDate];

  //Set the required date format
[dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm"];

NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(@"Converted String : %@",convertedString);
于 2013-06-22T09:24:15.343 回答
0

尝试使用此代码

NSDateFormatter *formatter;
NSString        *dateString;

formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MM-yyyy HH:mm"];
dateString = [formatter stringFromDate:[NSDate date]];
NSLog(@"%@",dateString);
于 2013-06-22T09:07:48.300 回答