Different NSDate for same timestapm on ios versions (ios 6 and iOS7) - objective-c

I ran into this weird issue where one of the method in my code is returning different dates for same timestamp value in different ios version.
Following is the code for that method.
+(NSDate *)getDateFromTheTimeStampAsDate:(NSInteger)timeStamp
{
NSLog(#"Timestamp - %d",timeStamp);
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLocale];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"PST"]];
[dateFormatter setDateFormat:#"MMM DD,yyyy"];
return [dateFormatter dateFromString:[dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:timeStamp]]];
}
For timestamp = 1361581013 following are the NSDates values returned from above function:
iOS6 - 2013/02/01
iOS7 - 2013/02/22
Is there anything wrong with this code ?
Update: I forgot to look at the date format, earlier. Thanks to one of the comment I managed to get correct date. This was a part of legacy code so in the decided to just trash it and use following:
[NSDate dateWithTimeIntervalSince1970:timeStamp];

First the output isn't ever close to the date. On the terminal try date -r 1361581013. At least then you can see if you are getting close.
Second, convert the timestamp to an NSDate. Log that see if its close.
NSDate *d = [NSDate dateWithTimeIntervalSince1970:1361581013] ;
NSLog(#"d = %#" , d) ;
Third after you know you are getting the NSDate then apply formats to it.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterLongStyle;
df.timeStyle = NSDateFormatterLongStyle;
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"PST" ];
NSLog(#"d with formatting 1 = %#" , [df stringFromDate:d]) ;
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"EST" ];
NSLog(#"d with formatting 1 = %#" , [df stringFromDate:d]) ;
df.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT" ];
NSLog(#"d with formatting 1 = %#" , [df stringFromDate:d]) ;
Then you will know you got it right.
d = 2013-02-23 00:56:53 +0000
d with formatting 1 = February 22, 2013 at 4:56:53 PM PST
d with formatting 1 = February 22, 2013 at 7:56:53 PM EST
d with formatting 1 = February 23, 2013 at 12:56:53 AM GMT

Related

Convert string date time from one region to another in Objective-C

I have a string date in UK format: 19 March 2014 10:17 and I want to be able to convert it to US format as a string like so: March 19, 2014, 10:17 AM, whenever the user changes region on their iPhone. This is what I have tried so far:
NSString * lastResetDateTime = [[NSUserDefaults standardUserDefaults] objectForKey:LAST_STATS_REST_DATETIME]; //date I want to convert
if(lastResetDateTime != nil){
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSDate* lastResetDate = [dateFormatter dateFromString:lastResetDateTime];
self.lastResetDateTime.text = [dateFormatter stringFromDate:lastResetDate];
}
However, lastResetDate is null, is there any other way to use the formatter for that type of date ?

Need to convert custom timestamp to different format in Objective-C iOS

I have a string "2012-06-04" and am having a hard time converting it to: June 4, 2012.
Is there a quick way to transform this? I come from a ruby world where you would convert everything to seconds and that back out to the format you need it. Is there a reference that shows how to do that?
Thanks
One way to do so is to convert the string to an NSDate using the NSDateFormatter with the 2012-06-04 format, and then convert the NSDate back to a string using the June 4, 2012 format:
NSString* input = #"2012-06-04";
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate* date = [df dateFromString:input];
[df setDateFormat:#"MMMM d, yyyy"];
return [df stringFromDate:date];
The format string's syntax is described in UTS #35.
Something like
NSDateFormatter *fromDateFormatter = [[NSDateFormatter alloc] init];
fromDateFormatter.dateFormat = #"yyyy-MM-dd";
NSDate *date = [fromDateFormatter dateFromString:#"2012-06-04"];
NSLog(#"%#", date);
NSDateFormatter *toDateFormatter = [[NSDateFormatter alloc] init];
toDateFormatter.dateFormat = #"MMMM d, yyyy";
NSString *toDate = [toDateFormatter stringFromDate:date];
NSLog(#"%#", toDate);
#=> 2012-05-30 20:51:12.205 Untitled[1029:707] 2012-06-03 23:00:00 +0000
#=> 2012-05-30 20:51:12.206 Untitled[1029:707] June 4, 2012
To work this out you can use Apple's class references NSDateFormatter and other sources like this IPHONE NSDATEFORMATTER DATE FORMATTING TABLE and some trial and error

Use (Billy Meltdown) NSDate-helper methods to convert Unix timestamp and display?

I would like to convert a Unix timestamp e.g 1315401371 stored as a NSString into a date format of 23:00 12 October 2011.
There are a number of similar questions being ask in relation to this however I wish to use billymeltdown / nsdate-helper methods
I have imported the 2 files (.h and .m) into my project, but have not manage to get further than that.
There is documentation, which I have read, however I am still very new to objective C and do not understand how I actual use the libraries.
Thanks in Advance.
You should use NSDateFormatter
NSTimeInterval timestamp = 1315401371;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm dd MMMM yyyy"]; // use one d for 7 October instead of 07 October
// For english month names
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLocale];
[usLocale release];
NSString *dateString = [dateFormatter stringFromDate:date];
[dateFormatter release];

How to Format Unknown Date Format in Objective-C

I've probably overlooked something simple, but I can't figure out how to convert a specific date format in Objective-C. I'm receiving dates in this format:
Sun, 10 Oct 2010 01:44:00 +0000
And I need to convert it to a long format with no time, like this:
October 10, 2010
I've tried using NSDateFormatter, but when I try to get a date from the string, it always comes back nil. I've tried it this way:
NSString *dateFromData = [articleData objectAtIndex:5];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
NSDate *date = [dateFormatter dateFromString:dateFromData];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
The date string always comes back nil. Am I missing something simple here?
This is the code I got to work, couldn't get the other format string to function
NSString *dateFromData = #"Sun, 10 Oct 2010 01:44:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy hh:mm:ss zzzz"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate * date = [dateFormatter dateFromString:dateFromData];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
The problem is you need to use two date formatters. One to parse the original format and one to produce the desired output. You also can't use the LongStyle date format as it doesn't match your input style.
NSDateFormatter *df = [[NSDateFormatter alloc] initWithDateFormat:#"%a, %d %b %Y %H:%M:%S %z"
allowNaturalLanguage:false];
NSDate *d = [df dateFromString:#"Sun, 10 Oct 2010 01:44:00 +0000"];
NSDateFormatter *df2 = [[NSDateFormatter alloc] initWithDateFormat:#"%B %d, %Y"
allowNaturalLanguage:false];
[df2 stringFromDate:d]
(Note, I wrote this code in MacRuby and ported it back to Objective-C so there maybe syntax errors.)
irb(main):001:0> df = NSDateFormatter.alloc.initWithDateFormat("%a, %d %b %Y %H:%M:%S %z", allowNaturalLanguage:false)
=> #<NSDateFormatter:0x20021b960>
irb(main):002:0> d = df.dateFromString("Sun, 10 Oct 2010 01:44:00 +0000")
=> #<NSCalendarDate:0x2002435e0>
irb(main):004:0> df2 = NSDateFormatter.alloc.initWithDateFormat("%B %d, %Y", allowNaturalLanguage:false)
=> #<NSDateFormatter:0x20026db60>
irb(main):005:0> df2.stringFromDate(d)
=> "October 09, 2010"

obj-c : iphone programming NSTimeInterval problem

i got a problem with my time interval. I need to get the interval of two times in this format : HH:MM. If i enter : 15:35 and 16:35 it is ok, but when i do 20:30 to 01:30, it gives me like 18 hours interval.. anyone have an idea?
NSString *startDate= starthere.text;
NSString *endDate = endhere.text;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
NSDate *dateSelected = [dateFormatter dateFromString:startDate];
NSDate *dateSelected2 = [dateFormatter dateFromString:endDate];
[dateFormatter release];
if ([dateSelected2 earlierDate:dateSelected] == dateSelected2)
{
dateSelected2 = [dateSelected2 dateByAddingTimeInterval:86400];
}
interval = [dateSelected2 timeIntervalSinceDate:dateSelected];
Your end date is before your start date, so there are 19 hours between them. A quick fix might be to add 24 hours to your end date if it compares as earlier than the start date. Something like:
if ([dateSelected2 earlierDate:dateSelected] == dateSelected2)
{
dateSelected2 = [dateSelected2 dateByAddingTimeInterval:86400];
}
The NSDate documentation has everything you need to know.