NSDateFormatter dateFromString for 24hr date gives wrong time

我只是一个虾纸丫 提交于 2019-12-24 23:40:26

问题


I want to add event to native calender.I am having problem in getting date accurate time from string. I have set locale,24 hr format. but still getting the same bug.

EKEvent *event = [EKEvent eventWithEventStore:self.eventStore];

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

  NSLocale *locale =[[[NSLocalealloc]initWithLocaleIdentifier:@"en_US_POSIX"]autorelease];  

  [dateFormatter setLocale:locale];
  [dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];


  event.startDate=[dateFormatter dateFromString:@"10/02/2011 15:39"];

And event.startDate is getting set as : 2011-02-10 10:09:00 GMT

Can anyone help???


回答1:


I think your event.startDate is re initializing,

Its showing the current time, i think so.

Check whether any code there like

event.startDate=[NSDate date];

EDIT:

how you checking your event.startDate vale? If it is by using NSLog check this answer.




回答2:


Hi,

I tried with setting timezone as GMT and it worked properly. Please add the below code and it should work.

[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];


来源:https://stackoverflow.com/questions/4954155/nsdateformatter-datefromstring-for-24hr-date-gives-wrong-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!