Iphone obj-C,为什么这会给我昨天的约会?

Iphone obj-C,为什么这会给我昨天的约会?,iphone,objective-c,cocoa-touch,nsdate,Iphone,Objective C,Cocoa Touch,Nsdate,我一直在努力使用函数返回今天的日期,尽可能接近零秒、零分、零小时。因此,我能够在各种交易中重复使用同一日期 然而,我现在发现我的函数返回昨天的日期 + (NSDate *)makeAbsoluteNSDate:(NSDate*)datSource { NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; [calendar s

我一直在努力使用函数返回今天的日期,尽可能接近零秒、零分、零小时。因此,我能够在各种交易中重复使用同一日期

然而,我现在发现我的函数返回昨天的日期

+ (NSDate *)makeAbsoluteNSDate:(NSDate*)datSource {

    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;
}

您所需要的只是当前日期和时间的
[NSDate date]

使用以下功能获取昨天的日期:

- (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;
 }

要在方法调用中获取昨天的日期传递日期=[NSDate date]、日期=-1、小时=0和分钟=0作为参数。

如果您试图在时区中获取今天或昨天的午夜,您应该获取
NSDate
,它指向GMT中的足够时间。在我的情况下-2小时,因为我的时区是+0200,所以对于2011-08-26的午夜,我将得到2011-08-25 22:00

您必须确保为
NSCalendar
设置了正确的时区,即
[NSTimeZone systemTimeZone]

在我的时区里,我的午夜惯例是:

+ (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];
}
因此,在评论中经过长时间讨论后,这里有一个中午函数:

+ (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];
}

第一个明显的问题是你们是否检查过你们电脑的时区和日期???请验证两者是否相同…请检查您设备的日期或系统的日期..我正在使用模拟器,我的Mac日期是正确的。是的,但我想去掉秒、分钟和小时。我想要今天,但我要昨天好的,我尝试了你的代码,目前在我的机器上是'2011-08-26 07:16:05+0000',这是datSource值在调试中返回的值。函数返回'2011-08-25 23:00:00+0000',我想这就是你说的。我真正想从这个函数中得到的是‘2011-08-26随便什么’?谢谢你的帮助。你的时间是从哪里来的?如果是从NSDate开始的,那就不完全正确。NSDate不保存时区信息-打印其内容总是返回+0000。从我的函数结果中,我看到你的时区是+0100,所以2011-08-25 23:00是正确的。如果你真的想在格林尼治时间午夜,我想你需要设置
[NSTimeZone timeZoneForSecondsFromGMT:0]
,而不是
[NSTimeZone systemTimeZone]
,应该可以。我正在传递'date'NSDate*dateToday=[General makeAbsoluteNSDate:[NSDate date]];我需要它根据设备当前日期时间在全球范围内工作。
[NSDate date]
返回从系统时区中的时间创建的
NSDate
。如果您的时间是09:36+0100,您将在
NSDate
中获得2011-08-26 08:36+0000。始终使用GMT是非常重要的。本地时区仅用于用户输出。那么,在函数中将时区设置为GMT有帮助吗?