简体   繁体   English

将NSString转换为NSDate - 格式错误

[英]Converting NSString to NSDate - wrong format

Hi I am trying to convert a string into NSDate format - but it returns value with wrong format and also in GMT time. 您好我正在尝试将字符串转换为NSDate格式 - 但它返回错误格式的值以及GMT时间。

 NSString *myString = @"10:30 AM";
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 NSTimeZone *local = [NSTimeZone systemTimeZone];
 dateFormatter.dateFormat = @"hh:mm a";
[dateFormatter setTimeZone:local];
 NSDate *myDate = [dateFormatter dateFromString:myString];



NSLog(@"myDate---%@",myDate);

But this returns 但这会回来

  myDate---2000-01-01 05:30:00 +0000

Where I am making the mistake, what to do to get the date as 10:30 AM, Please help me to fix this. 在我犯错误的地方,如何将日期定义为上午10:30,请帮我解决这个问题。 Thanks in advance 提前致谢

Don't use NSLog() to display the date as that will format it using the UTC/GMT timezone and its own format; 不要使用NSLog()来显示日期,因为它将使用UTC / GMT时区及其自己的格式对其进行格式化; instead use the date formatter to display it: 而是使用日期格式化程序来显示它:

NSLog(@"myDate---%@", [dateFormatter stringFromDate:myDate]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM