Monday, 3 June 2013

Get current date, month and year: cocos2d/Xcode

To get current date, month and year, you can use the following code:

    NSDate *now = [NSDate date];

    NSLog(@"now: %@", now); // now: 2011-02-28 09:57:49 +0000

    NSString *strDate = [[NSString alloc] initWithFormat:@"%@",now];
    NSArray *arr = [strDate componentsSeparatedByString:@" "];
    NSString *str;
    str = [arr objectAtIndex:0];
    NSLog(@"strdate: %@",str); // strdate: 2011-02-28

    NSArray *arr_my = [str componentsSeparatedByString:@"-"];

    NSInteger date = [[arr_my objectAtIndex:2] intValue];
    NSInteger month = [[arr_my objectAtIndex:1] intValue];
    NSInteger year = [[arr_my objectAtIndex:0] intValue];

    NSLog(@"year = %d", year); // year = 2011
    NSLog(@"month = %d", month); // month = 2
    NSLog(@"date = %d", date); // date = 2

No comments:

Post a Comment