【问题标题】:nslocale issue in convert string to date将字符串转换为日期时出现 nslocale 问题
【发布时间】:2013-11-21 12:08:12
【问题描述】:

我是 xcode 新手,请看下面的代码和链接 enter link description here

{
NSString * TempDate = [NSString stringWithFormat:@"%@ %@ %@",Datelbl.text,yearString,TimeLbl.text]; //Thursday, November 21 2013 5:35 PM

[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat :@"EEEE',' MMMM d yyyy hh:mm a"];
AppointmentDate = [dateFormatter dateFromString:TempDate];    //NSDate 
Appointment = [dateFormatter stringFromDate:AppointmentDate]; //NSString 
}

这里,约会日期返回这个值 -> 2013-11-21 12:05:00 +0000, 约会返回此值-> 2013 年 11 月 21 日星期四下午 5:35。

我需要得到 2013-11-21 05.35 PM。那么,如何管理这个 GMT 问题。

【问题讨论】:

  • TempDate 是什么样的?
  • @ramdy:请将您之前的问题与此链接,这将对您有所帮助。

标签: ios objective-c nsdate nsdateformatter


【解决方案1】:

试试这个:

NSString *date = @"Thursday, November 21 2013 3:05 PM";

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat : @"EEEE, MMMM d yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
NSDate *AppointmentDate = [dateFormatter dateFromString:date];

NSLog(@"DATE--> %@",AppointmentDate);

输出:

DAT--> 2013-11-21 15:05:00 +0000

注意:NSDate 将采用 24 小时格式。您需要将其转换为字符串是您想要的 03:05...

【讨论】:

  • 但是这样你创建了一个“坏”的 NSDate 对象。时区应设置为输入的时区(或创建字符串时的输出),以便 NSDate 本身为 UTC。
【解决方案2】:

编辑:

  [dateFormatter setDateFormat : @"EEEE, MMMM d yyyy hh:mm a"];

String value not converting to date format

【讨论】:

  • 您的格式与输入格式不匹配:Thursday, November 21 2013 5:35 PM。问题也不在于格式,而在于时区
猜你喜欢
  • 2014-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-15
  • 2020-10-05
  • 1970-01-01
  • 2016-03-13
相关资源
最近更新 更多