我正在开发一个全球应用程序,其功能是我需要将发布的时间分别显示为“仅现在”,“ 1分钟前”,“ 1小时前”。我在创建帖子时发送日期和时间并发送到Webservice。并且我为每个帖子检索相同的日期时间,我需要在每个帖子上显示相对的时间(分别是现在,1小时,1周)。从Web服务获取实际日期为
“ created_date” =“ 2015-01-29 06:33:02”; 但是,当我解析为Nsdate格式时,它返回的日期为“ 2015-01-29 01:03:02 +0000”。这是向其发送服务器的日期和时间的代码。
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"DateFormatter%@",[DateFormatter stringFromDate:[NSDate date]]);
下面的代码我用来格式化字符串的日期。
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:[value valueForKey:@"created_date"]];
NSLog(@"Output :%@",date);
如何找到分享帖子的实际时间。我对目标C还是陌生的。我不确定自己做错了什么,请有人帮我解决。
看来这里有两个问题。
第一个是关于自然语言日期格式的,我想您可能会发现它与此相关。
第二个问题似乎是您对时区有些困惑。NSDate将假设GMT没有任何偏移量或ICU时区规范的指示来解析您的日期。然后,当您重新打印出来时,很可能使用机器的当前语言环境来格式化输出日期。您应该阅读NSDate和NSDateFormatter对时区的处理。
在将日期作为字符串发送到服务器时,应采用以下格式:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// Special, agnostic locale
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:locale];
// GMT
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[dateFormatter setTimeZone:timeZone];
// ISO 8601
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSLog(@"DateFormatter%@",[dateFormatter stringFromDate:[NSDate date]]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句