Iphone iOs中的NSDate转换问题

Iphone iOs中的NSDate转换问题,iphone,nsdate,nsdateformatter,nstimeinterval,Iphone,Nsdate,Nsdateformatter,Nstimeinterval,我有一个表示NSTimeInterval的双精度值。我想从双精度值创建正确的日期,这是我的代码。我得到了输出,但是实际值有一些变化 NSString *modiDate = [NSString stringWithFormat:@"1338229800"]; //example time interval value NSNumber *time = [NSNumber numberWithDouble:([modiDate doubleValue] - 3600)]; NSTimeInter

我有一个表示
NSTimeInterval
的双精度值。我想从双精度值创建正确的日期,这是我的代码。我得到了输出,但是实际值有一些变化

NSString *modiDate = [NSString stringWithFormat:@"1338229800"]; //example time interval value

NSNumber *time = [NSNumber numberWithDouble:([modiDate doubleValue] - 3600)];
NSTimeInterval interval = [time doubleValue];    
NSDate *online = [NSDate date];
online = [NSDate dateWithTimeIntervalSince1970:interval];    
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];

NSLog(@"result: %@", [dateFormatter stringFromDate:online]);
这里出了什么问题?有什么想法吗

编辑: 对于第一次测试,我将日期转换为
NSTimeInterval
,并尝试返回日期。 下面是代码和输出

NSString *modDate = [NSString stringWithFormat:@"5/30/2012 2:02:55 PM"];

NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];
NSDate *dateFromString1 = [[NSDate alloc] init];
dateFromString1 = [format dateFromString:modDate];
[format release];
NSTimeInterval timeInterval1 = [dateFromString1 timeIntervalSince1970];

NSLog(@"timeinterval : %f",timeInterval1);

NSDate *online = [NSDate date];
online = [NSDate dateWithTimeIntervalSince1970:timeInterval1];    
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];

NSLog(@"result: %@", [dateFormatter stringFromDate:online]);
输出为:

 result: 05/30/2012 12:02:55 PM actual date:5/30/2012 2:02:55 PM

首先,NSTimeInterval是一个浮点数,因此不需要使用NSString或NSNumber(NSNumber用于在字典或数组中存储数字)

您可以将代码简化为:

NSTimeInterval interval = 1338229800;
interval -= 3600;
NSDate *online = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];
NSLog(@"result: %@", [dateFormatter stringFromDate:online]);
这给我:
05/28/2012 19:30:00 PM

它对应于初始值:1338229800减去一小时(-3600秒)

编辑:

在编辑中,您可以编写:

NSString *modDate = [NSString stringWithFormat:@"5/30/2012 2:02:55 PM"];
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];
但您应该检查:

2:02:55 PM
对应于
@“h:mm:ss a”
而不是
“HH:mm:ss aaa”
首先,NSTimeInterval是一个浮点数,因此不需要使用NSString或NSNumber(NSNumber用于在字典或数组中存储数字)

您可以将代码简化为:

NSTimeInterval interval = 1338229800;
interval -= 3600;
NSDate *online = [NSDate dateWithTimeIntervalSince1970:interval];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];
NSLog(@"result: %@", [dateFormatter stringFromDate:online]);
这给我:
05/28/2012 19:30:00 PM

它对应于初始值:1338229800减去一小时(-3600秒)

编辑:

在编辑中,您可以编写:

NSString *modDate = [NSString stringWithFormat:@"5/30/2012 2:02:55 PM"];
NSDateFormatter *format = [[NSDateFormatter alloc]init];
[format setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];
但您应该检查:

2:02:55 PM
对应于
@“h:mm:ss a”
而不是
“HH:mm:ss aaa”
此代码有很多错误:

NSDate *dateFromString1 = [[NSDate alloc] init];
dateFromString1 = [format dateFromString:modDate];
为什么要创建所有这些对象:

NSString *modiDate = [NSString stringWithFormat:@"1338229800"]; //example time interval value
NSNumber *time = [NSNumber numberWithDouble:([modiDate doubleValue] - 3600)];
NSTimeInterval interval = [time doubleValue];   
如果这是相同的:

 NSString *modiDate = [NSString stringWithFormat:@"1338229800"];
 NSTimeInterval interval = [modiDate doubleValue]  - 3600;

在此,您可以将新日期分配到联机:

NSDate *online = [NSDate date];
仅在此处将其他日期设置为联机:

online = [NSDate dateWithTimeIntervalSince1970:interval];  
你应该这样做:

NSDate *online = [NSDate dateWithTimeIntervalSince1970:interval];  

我知道你对这段代码有什么问题,但它工作正常。 您可能希望设置区域设置,但实际上不需要,但可能会对本地化产生一些影响:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.locale = [NSLocale systemLocale];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];

NSLog(@"result: %@", [dateFormatter stringFromDate:online]);

从编辑中添加新代码

在编辑中,您添加了以下代码:

NSDate *dateFromString1 = [[NSDate alloc] init];
dateFromString1 = [format dateFromString:modDate];
您正在泄漏一个
NSDate
对象,只需执行以下操作:

NSDate *dateFromString1 = [format dateFromString:modDate];

问题答案中的状态是时间格式字符串。

此代码有很多错误:

NSDate *dateFromString1 = [[NSDate alloc] init];
dateFromString1 = [format dateFromString:modDate];
为什么要创建所有这些对象:

NSString *modiDate = [NSString stringWithFormat:@"1338229800"]; //example time interval value
NSNumber *time = [NSNumber numberWithDouble:([modiDate doubleValue] - 3600)];
NSTimeInterval interval = [time doubleValue];   
如果这是相同的:

 NSString *modiDate = [NSString stringWithFormat:@"1338229800"];
 NSTimeInterval interval = [modiDate doubleValue]  - 3600;

在此,您可以将新日期分配到联机:

NSDate *online = [NSDate date];
仅在此处将其他日期设置为联机:

online = [NSDate dateWithTimeIntervalSince1970:interval];  
你应该这样做:

NSDate *online = [NSDate dateWithTimeIntervalSince1970:interval];  

我知道你对这段代码有什么问题,但它工作正常。 您可能希望设置区域设置,但实际上不需要,但可能会对本地化产生一些影响:

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
dateFormatter.locale = [NSLocale systemLocale];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];

NSLog(@"result: %@", [dateFormatter stringFromDate:online]);

从编辑中添加新代码

在编辑中,您添加了以下代码:

NSDate *dateFromString1 = [[NSDate alloc] init];
dateFromString1 = [format dateFromString:modDate];
您正在泄漏一个
NSDate
对象,只需执行以下操作:

NSDate *dateFromString1 = [format dateFromString:modDate];

问题答案中的状态是时间格式字符串。

您使用的是24小时的HH。改为使用适用于AM/PM格式的hh(0-12)。

您使用的是24小时的hh。改用适用于AM/PM格式的hh(0-12)。

有什么变化?NSLog输出在哪里?我已经用代码和结果更新了我的问题更改了什么?NSLog输出在哪里?我已经用代码和结果更新了我的问题是的。正如我所说的,每件事都解释清楚了,是的。正如我所说的,一切都在现场解释