Objective c 如何将本地化日期字符串转换为NSDate

Objective c 如何将本地化日期字符串转换为NSDate,objective-c,nsdate,date-conversion,localized,Objective C,Nsdate,Date Conversion,Localized,我有NSString*localized_Date=分钟从午夜到localized time(时间单位) 我在早上4:30,晚上10:00得到这个字符串 如何将此字符串转换为NSDate? 请让我知道。NSDateFormatter是您的朋友,请使用适合您的日期/时间的格式 NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; [df setDateFormat:[NSString stringWithForma

我有NSString*localized_Date=分钟从午夜到localized time(时间单位)

我在早上4:30,晚上10:00得到这个字符串

如何将此字符串转换为NSDate?
请让我知道。

NSDateFormatter是您的朋友,请使用适合您的日期/时间的格式

NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease]; 
[df setDateFormat:[NSString stringWithFormat:@"dd MMM yyyy HH:mm"]]; 
NSDate *_date = [df dateFromString:string]; 
约翰

试试下面的代码:

NSString *dateString = @"4:30 am";

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

[dateFormatter setDateFormat:@"hh:mm a"];

NSDate *date = [dateFormatter dateFromString:dateString];

希望这能对您有所帮助。

我知道代码有点长,但我想让它尽可能可读。 此代码使用minutess而不是本地化字符串

NSDate *currentDate = [NSDate date];
NSDateComponents *currentDateComponents = [calendar components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:currentDate];
NSDateComponents *currentDateToMidnight = [[NSDateComponents alloc] init];
[currentDateToMidnight setHour:-[currentDateComponents hour]];
[currentDateToMidnight setMinute:-[currentDateComponents minute]];
[currentDateToMidnight setSecond:-[currentDateComponents second]];
NSDate *midnight = [calendar dateByAddingComponents:currentDateToMidnight toDate:currentDate options:0];
NSDateComponents *midnightToDesiredDate = [[NSDateComponents alloc] init];
[midnightToDesiredDate setMinute:minutesSinceMidnight];
NSDate *desiredDate = [calendar dateByAddingComponents:midnightToDesiredDate toDate:midnight options:0];

那不是给我一个合适的日期。是的,那会给我一个合适的时间。如果要更改日期格式
[dateFormatter setDateFormat:@“EEE,dd MMMM yyy HH:mm:ss vvv”]。此格式将使用time.NSString*dateString=@“10:59 pm”给出正确的日期;NSLog(@“datestring%@”,datestring);NSDateFormatter*dateFormatter=[[NSDateFormatter alloc]init];//[dateFormatter setDateFormat:@“hh:mm a”];[dateFormatter setDateFormat:@“EEE,dd-MMMM-yyyy-HH:mm:ss-vvv”];NSDate*date=[dateFormatter dateFromString:dateString];NSLog(@“转换日期%@”,日期)<代码>代码
我将日期作为空对象获取。你知道吗?你只有时间,比如晚上10:59,那么有了这些输入,你怎么能得到正确的日期呢。您应该有合适的字符串作为
@“2013/05/11 12:34 pm”
,然后只有您将字符串转换为NSDate。如何从minutesSinceMidnight(即NSInteger)获取NSDate?嗨,rbkujawa,如果我的minutesSinceMidnight为120,我将获得比当前时间提前7小时的09:00:00+0000时间。您的设备/模拟器上设置了什么时区?您需要某个特定时区的minutesSinceMidnight日期还是仅当前设备的时区?我有太平洋夏令时。您需要某个特定时区的minutesSinceMidnight日期还是仅当前设备的时区的minutesSinceMidnight日期?我需要仅当前设备的时区的日期