从时间戳间隔获取缺少的月份 [英] Get missing month from timestamp Interval

查看:196
本文介绍了从时间戳间隔获取缺少的月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从服务器接收几秒钟的时间,然后使用以下代码将tjose秒转换为月份:

I am receiving time in seconds from server and then i am converting tjose seconds into month using this code:

NSDateFormatter * dateFormatter=[[[NSDateFormatter alloc]init]autorelease];
dateFormatter setDateFormat:@"MMM"];
[dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeStamp]];

问题是我从服务器每个月都不会收到,就像我第一次再次获得Jan我从服务器上获得了4月份,但是我需要在iPhone屏幕上显示feb和Mar(缺少月份)(服务器人员不能更改代码,所以我必须在我的最后)。

任何人建议我怎么可能失踪月份。

同样的问题有时候我会从服务器获得11月份,然后是2月份,但是我也必须显示12月和1月。

任何帮助将

The problem is I dont get every month from the server like if I am getting Jan first then second time I'm getting Apr from server but I need to show feb and Mar (missing month) on my iPhone screen (Server people cannot change their code so I have to do it at my end).
Can anyone suggest me how can I get missing months.
The same problem is sometimes I get Nov and then Feb from server, but I also have to show Dec and Jan.
Any help will be appreciated!

推荐答案

您可以将两个日期之间的差异作为时间戳比较

You can compare for the difference between two dates as timestamps

NSDateFormatter * dateFormatter=[[[NSDateFormatter alloc]init]autorelease];
dateFormatter setDateFormat:@"MMM"];
NSString* monthName = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:newTimestamp]]

NSTimeInterval month = 60.0 * 60.0 * 24.0 * 31.0; //seconds * minutes * hours * days ~ month
NSTimeInterval tempTimestamp = oldTimestamp;
while(tempTimestamp - newTimestamp) > month){
    //more than a month difference -> add a month
    NSString* tempMonthName = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:tempTimestamp]];
    if(![monthName isEqualTo:newMonthName]){
        //do whatever you need with the additional month name...
    }
    tempTimestamp += month;
}

希望会有所帮助。

干杯...

这篇关于从时间戳间隔获取缺少的月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆