0

I am having a double value that represents a NSTimeInterval. I want to create the correct date from the double value, here is my code. I am getting the output, but there is some changes from the actual value.

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]);

What went wrong here? Any idea?

EDIT: For testing first i converted a date to NSTimeInterval and tried to get back the date. Here is the code and output.

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]);

and the output is:

 result: 05/30/2012 12:02:55 PM actual date:5/30/2012 2:02:55 PM
Mithun
  • 459
  • 1
  • 10
  • 32

3 Answers3

4

First, NSTimeInterval is a float, so you don't need to use NSString or NSNumber (NSNumber are made for storing numbers in dictionaries or arrays).

You can simplify your code as:

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]);

This give me : 05/28/2012 19:30:00 PM

It corresponds to the initial value : 1338229800 minus one hour (- 3600 sec).

EDIT :

In your edit, your write :

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"];

But you should check the date format paterns page:
2:02:55 PMcorresponds to @"h:mm:ss a" and not to "HH:mm:ss aaa"

Martin
  • 11,881
  • 6
  • 64
  • 110
  • yes it is. As I said, everything is explained at http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns – Martin May 30 '12 at 10:23
1

There are many thing wrong with this code:

Why create all these object:

NSString *modiDate = [NSString stringWithFormat:@"1338229800"]; //example time interval value
NSNumber *time = [NSNumber numberWithDouble:([modiDate doubleValue] - 3600)];
NSTimeInterval interval = [time doubleValue];   

When this is the same:

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

Here you assing a new date to online:

NSDate *online = [NSDate date];

only to set an other date to online here:

online = [NSDate dateWithTimeIntervalSince1970:interval];  

You should have done this:

NSDate *online = [NSDate dateWithTimeIntervalSince1970:interval];  

I'm sure what you problem with this code, but it works correctly. You might want to set the locale, not really need but might have some effect in localization:

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

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

Added new code from edit

In your edit you added this code:

NSDate *dateFromString1 = [[NSDate alloc] init];
dateFromString1 = [format dateFromString:modDate];

Here you are leaking an NSDate object,just do this:

NSDate *dateFromString1 = [format dateFromString:modDate];

And as state in the answer by martin the problem is in the time format string.

Community
  • 1
  • 1
rckoenes
  • 69,092
  • 8
  • 134
  • 166
0

You are using HH which is for 24 hours. Instead use hh (0-12) which is suitable for AM/PM format.

chirag
  • 345
  • 2
  • 7