繁体   English   中英

比较两个日期以获取iOS游戏中的每日奖励

[英]Comparing two dates for daily reward in iOS game

我正在实施“每日奖励”方法,以便每天在游戏中提供一些硬币。

我有一个包含实际日期和时间的json。

问题是我不知道如何比较日期。

这是我的代码-

@implementation ItemRewardManager

+(NSDate *)dateFromUTCTimeStamp:(NSString *)dateString{

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *myDate = [df dateFromString: dateString];
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [df setTimeZone:gmt];

    [[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@"lastDatePlayed"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

-(void)getData
{
    NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:dateUrl]];
    id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
    NSArray *keys = [jsonObjects allKeys];

    // values in foreach loop
    for (NSString *key in keys) {
        NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]);
    }
}

+(void)dailyReward{
    NSLog(@"llega al daily");
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"HH:mm:ss zzz"];

    NSDate *now = [NSDate date];
    NSString *dateString = [dateFormat stringFromDate:now];

    if([[[NSUserDefaults standardUserDefaults] objectForKey:@"lastDatePlayed"] isEqualToString: dateString])
    {
        NSLog(@"Actual date is the same as json date");
        UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:nil
                                                         message:@"You have win 5 coins! Come back tomorrow for more."
                                                        delegate:nil
                                               cancelButtonTitle:@"Ok"
                                               otherButtonTitles: nil] autorelease];
        [alert show];
        return;
    }
}
@end

因此,当我打开我的应用程序并调用此方法时,我的应用程序由于某种原因而崩溃。

我认为问题可能出在两个日期的比较上。

有人可以告诉我有什么问题吗?

使用以下内容。

if ([date1 compare:date2]==NSOrderedSame)

date1和date2是NSdate对象。

NSDate具有比较日期的方法。 他们来了:

isEqualToDate:返回一个布尔值,该布尔值指示给定对象是否为NSDate对象并且与接收方完全相等。

- (BOOL)isEqualToDate:(NSDate *)anotherDate

lateDate:返回接收者的较晚日期和另一个给定的日期。

- (NSDate *)laterDate:(NSDate *)anotherDate

earlyDate:返回接收者的较早日期和另一个给定的日期。

- (NSDate *)earlierDate:(NSDate *)anotherDate

尝试这样。 可能对你有帮助

NSDate *now = [NSDate date]; // current date
NSDate *newDate = [dateFormat dateFromString:[[NSUserDefaults standardUserDefaults] objectForKey:@"lastDatePlayed"]]; // server date 

NSComparisonResult result; 

result = [now compare:newDate]; // comparing two dates

if(result == NSOrderedAscending)
    NSLog(@"now is less");
else if(result == NSOrderedDescending)
    NSLog(@"newDate is less");
else
    NSLog(@"Both dates are same");

使用此代码。

 NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:@"MM/dd/yyyy"];
NSDate *ServerDate = [dateFormatter1 dateFromString:@"03/01/2013"];
[dateFormatter1 release];
NSTimeInterval interval = [date timeIntervalSinceDate:ServerDate];
int diff=interval/1440;

这是实现的代码。

暂无
暂无

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

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