stringFromDate总是NIL

编程入门 行业动态 更新时间:2024-10-26 03:31:55
本文介绍了stringFromDate总是NIL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道这是一个重复的问题,但在stackoverflow和google上搜索了许多类似的问题之后,没有一个解决方案适用于我。

I know this is a repetitive question, but after searching for many similar questions on stackoverflow and google, none of the solutions worked for me.

我想要将我从DB收到的日期转换为字符串格式以在iPhone应用程序中显示。

I am tring to convert date which I receive from DB to string format to display in iPhone app.

我将以下列方式将日期转换为字符串,但[dateFormat stringFromDate:beginDate]始终返回nil。

I am converting date to string in following manner, but [dateFormat stringFromDate:beginDate] always return nil.

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"MMM dd hh:mm a"]; NSString *dateString = [dateFormat stringFromDate:beginDate]; NSLog(@"date: %@", dateString); [dateFormat release];

收到的服务器NSDate格式如下:

The server NSDate received is of following format:

2012-05-23 13:06:51.394+1000

我希望它

May 25 13:06 PM

请帮忙。

提前致谢。

推荐答案

您需要先将beginDate字符串转换为实际的NSDate,然后执行此操作:

You need to convert your beginDate string to an actual NSDate first, and then do it:

NSString *beginDateString = [arr_data objectAtIndex:1]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSSZ"]; NSDate *beginDate = [formatter dateFromString:beginDateString]; [formatter setDateFormat:@"MMM dd hh:mm a"]; NSString *dateString = [formatter stringFromDate:beginDate]; NSLog(@"date: %@", dateString);

请注意,您在问题中说明要输出 5月25日13:06 PM 但它实际上会输出 5月25日01:06 PM 。 (13:06将使用24小时时间而不是12小时时间,并且不需要上午/下午。)

Note that you say in your question that you want it to output May 25 13:06 PM but it will actually output May 25 01:06 PM. (13:06 would be using 24 hour time instead of 12 hour time and wouldn't need the am/pm.)

更多推荐

stringFromDate总是NIL

本文发布于:2023-10-19 19:22:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508521.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:stringFromDate   NIL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!