Tec*_*ain 1 timezone objective-c ios
我想在iOS中获得时间/日期格式,比如几小时前,几天前.但是我得到了错误的数据.例如,如果时间是:
2015-10-21 03:43:20
Run Code Online (Sandbox Code Playgroud)
然后根据下面的代码,它显示我6小时前.我在php中使用Now()函数来保存时间/日期,那么我如何才能显示不同国家的确切时间?
+(NSString*)HourCalculation:(NSString*)PostDate
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormat setTimeZone:gmt];
NSDate *ExpDate = [dateFormat dateFromString:PostDate];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSDayCalendarUnit|NSWeekCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:ExpDate toDate:[NSDate date] options:0];
NSString *time;
if(components.year!=0)
{
if(components.year==1)
{
time=[NSString stringWithFormat:@"%ld year",(long)components.year];
}
else
{
time=[NSString stringWithFormat:@"%ld years",(long)components.year];
}
}
else if(components.month!=0)
{
if(components.month==1)
{
time=[NSString stringWithFormat:@"%ld month",(long)components.month];
}
else
{
time=[NSString stringWithFormat:@"%ld months",(long)components.month];
}
}
else if(components.week!=0)
{
if(components.week==1)
{
time=[NSString stringWithFormat:@"%ld week",(long)components.week];
}
else
{
time=[NSString stringWithFormat:@"%ld weeks",(long)components.week];
}
}
else if(components.day!=0)
{
if(components.day==1)
{
time=[NSString stringWithFormat:@"%ld day",(long)components.day];
}
else
{
time=[NSString stringWithFormat:@"%ld days",(long)components.day];
}
}
else if(components.hour!=0)
{
if(components.hour==1)
{
time=[NSString stringWithFormat:@"%ld hour",(long)components.hour];
}
else
{
time=[NSString stringWithFormat:@"%ld hours",(long)components.hour];
}
}
else if(components.minute!=0)
{
if(components.minute==1)
{
time=[NSString stringWithFormat:@"%ld min",(long)components.minute];
}
else
{
time=[NSString stringWithFormat:@"%ld mins",(long)components.minute];
}
}
else if(components.second>=0)
{
if(components.second==0)
{
time=[NSString stringWithFormat:@"1 sec"];
}
else
{
time=[NSString stringWithFormat:@"%ld secs",(long)components.second];
}
}
return [NSString stringWithFormat:@"%@ ago",time];
}
Run Code Online (Sandbox Code Playgroud)
一些观察:
首先,如果您使用NSDateComponentsFormatter,例如,您可以大大简化此代码
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleFull;
formatter.allowedUnits = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSString *elapsed = [formatter stringFromDate:someDate toDate:[NSDate date]];
Run Code Online (Sandbox Code Playgroud)
这会生成一个表示两个NSDate对象之间时间的字符串,如下所示:
5年,9个月,20天,3小时,48分钟,56秒
或者您可以指定最大单位数,它只显示n个最大单位数.例如:
formatter.maximumUnitCount = 2;
Run Code Online (Sandbox Code Playgroud)
这将产生:
5年10个月
注意使用常量如NSCalendarUnitYearnot NSYearCalendarUnit.
如果已用时间已关闭,则最常见的情况是时间字符串中的时区不正确.避免这些问题的最简单方法是确保从服务器收到的字符串处于已知时区(例如GMT)或时间字符串包含时区.通常我们会在Web服务中使用ISO 8601/RFC 3339日期格式并相应地调整我们的日期格式化程序(请参阅Apple Technical Q&A 1480,其中讨论了一些微妙的问题).
| 归档时间: |
|
| 查看次数: |
2055 次 |
| 最近记录: |