NSDate returns wrong date on the first of month

后端 未结 2 783
迷失自我
迷失自我 2021-01-06 01:35

My app used [NSDate date ] function to get current date. Its work fine other days except 1st of every month during AM. i.e Follow Following steps :

2条回答
  •  半阙折子戏
    2021-01-06 01:53

    Are you running iOS version 4.2? If so, there is some kind of issue with the date method, it does not seem to respect your local time zone. For example, if I run your code in the iOS 4.0 simulator, I get this output:

    Current Date :: 2011-07-06 10:43:38 -0400

    But if I run it in the iOS 4.2 simulator, I get this:

    Current Date :: 2011-07-06 14:44:18 +0000

    Here is another SO post that describes this in more detail:

    NSDate et al woes on iOS 4.2

    If you are just looking for the current date and time in an NSString, I use code that looks like this to accomplish that task:

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [formatter setDateStyle:NSDateFormatterShortStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    NSString *result = [formatter stringFromDate:[NSDate date]];
    [formatter release];
    NSLog(@"Current Date v2 :: %@",result);
    

提交回复
热议问题