Iphone NSDate和NSDate格式和unix时间戳

Iphone NSDate和NSDate格式和unix时间戳,iphone,objective-c,ios,ipad,Iphone,Objective C,Ios,Ipad,因此,我有以下代码: NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"MM/dd/yyyy hh:mma"]; NSString *dateString = [dateFormat stringFromDate:self.firstUsed]; NSLog(@"FIRST USED %@ %f", dateString, [[NSDate dat

因此,我有以下代码:

 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"MM/dd/yyyy hh:mma"];
    NSString *dateString = [dateFormat stringFromDate:self.firstUsed];
 NSLog(@"FIRST USED %@ %f", dateString, [[NSDate date] timeIntervalSinceDate:self.firstUsed]);

FIRST USED 06/15/2012 10:42PM 716.087895

让我困惑的是,当我输入173.755023时,它并没有转换回2012年6月15日10:42。这怎么可能?我想正确的号码应该是133971440,请写下以下代码:

NSDateFormatter *dtFormatAppointment = [[NSDateFormatter alloc] init];

[dtFormatAppointment setDateFormat:@"MM/dd/yyyy hh:mm a"];

NSString *dateString = [dateFormat stringFromDate:self.firstUsed];

NSTimeInterval time = [dateString timeIntervalSince1970];

NSLog(@"FIRST USED %@ %.f", dateString, time);
此代码适用于您。

请尝试以下操作:

NSDate* referenceDate = [NSDate dateWithTimeIntervalSince1970: 0];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
int offset = [timeZone secondsFromGMTForDate: referenceDate];
int unix_timestamp =  [curdate timeIntervalSince1970];
int Timestamp = unix_timestamp - offset;

请告诉我你到底想做什么。您需要从时间戳或从时间戳到日期的日期“2012年6月15日10:42”。你给的时间戳的日期是错误的。这正是问题所在,为什么它是错误的?基本上日期是正确的,但时间戳不是