开发者

obj-C, why does this give me yesterdays date?

I've been struggling with my function to return todays date, at as close to zero seconds, minutes and hours as possible. So I'm able to re-use the same date with various transactions.

However, I've now discovered that my function returns yesterdays date?

+ (NSDate *)makeAbsoluteNSDate:(NSDate*)datSour开发者_运维问答ce {

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:
           NSGregorianCalendar];
    [calendar setTimeZone:[NSTimeZone localTimeZone]];

    NSDateComponents *dateComponents = [calendar components:NSYearCalendarUnit | 
            NSMonthCalendarUnit | NSDayCalendarUnit
            fromDate:datSource];
    [dateComponents setHour:0];
    [dateComponents setMinute:0];
    [dateComponents setSecond:0];

    NSDate *today = [calendar dateFromComponents:dateComponents];
    [calendar release];

    return today;
}


If you're trying to get today's or yesterday's midnight in your time zone, you should get NSDate that points to adequate time in GMT. In my case -2 hours as my timezone is +0200, so for midnight of 2011-08-26 I'll get 2011-08-25 22:00.

You have to make sure that you're setting the right timezone for the NSCalendar, which is [NSTimeZone systemTimeZone].

My routine for getting the midnight in my time zone is:

+ (NSDate *)midnightOfDate:(NSDate *)date {
    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setTimeZone:[NSTimeZone systemTimeZone]];

    NSDateComponents *components = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];
    [components setTimeZone:[cal timeZone]];

    [components setHour:0];
    [components setMinute:0];
    [components setSecond:0];

    return [cal dateFromComponents:components];
}

So, after a long discussion in comments, here's a midday function:

+ (NSDate *)middayOfDate:(NSDate *)date {
    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setTimeZone:[NSTimeZone systemTimeZone]];

    NSDateComponents *components = [cal components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:date];
    [components setTimeZone:[cal timeZone]];

    [components setHour:12];
    [components setMinute:0];
    [components setSecond:0];

    return [cal dateFromComponents:components];
}


Use below function to get date for yesterday :

- (NSDate *)getDate:(NSDate *)date days:(NSInteger)days hours:(NSInteger)hours mins:(NSInteger)mins seconds:(NSInteger)seconds 
{
     NSCalendar *cal = [NSCalendar currentCalendar];
     NSDateComponents *components = [cal components:( NSHourCalendarUnit |   NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];

     days = (days*24) + hours; 
     [components setHour:days];
     [components setMinute:mins];
     [components setSecond:seconds];

     NSDate *returnDate = [cal dateByAddingComponents:components toDate:date options:0];
     return returnDate;
 }

To get yesterday date pass date = [NSDate date], day = -1, hours = 0 and mins = 0 as parameters in method calling.


All you need is [NSDate date] for the current date & time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜