How to Format Unknown Date Format in Objective-C - 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"

Related

NSDateFormatter produces null result Objective-C

So I have this string date
"2017-07-25T11:02:00.000Z"
I want to format this date so that I can assign it to my date picker
Below is my code
//Set the date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:self.userDefaultQuotationModel.dateTime];
[self.quotationDatePicker setDate:dateFromString];
however, if I set the date format as #"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ", it gives me the correct result based on the format. But afterwards, I can't assign this date to the date picker.
I set the date format as #"MMM d, yyyy" because I am looking for the the nearest format as Date Picker's
my Date picker format is Jul 25 2017. I get the date format from this following website
http://nsdateformatter.com/
The *dateFromString does not contain any value after the dateFromString function, which is nil
I have been searching regarding this topic but couldn't find any. What am I missing here? thank you
NSString *strInputDateString = #"2017-07-25T11:02:00.000Z";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];
//Set new dateFormate
NSDate *date1 = [dateFormat dateFromString:strInputDateString];
[dateFormat setDateFormat:#"MMM d, yyyy"];
NSString *strOutputDateString = [dateFormat stringFromDate:date1];
NSLog(#"%#",strInputDateString);
NSLog(#"%#",strOutputDateString);

NSDateFormater stringFromDate to dateFromString return wrong date [duplicate]

This question already has an answer here:
NSDateFormatter show wrong year
(1 answer)
Closed 6 years ago.
My code:
NSString *dateStr = #"03-02-2017";//[responseObject objectForKey:#"event_date"];
NSLog(#"'%#'", dateStr);
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_HK"];
[dateFormat setLocale:locale]; //To fix the format into something like: 10 Feb 2017, but not: 10 2月 2017
[dateFormat setDateFormat:#"dd-MM-YYYY"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"'%#'", date);
// Convert date object to desired output format
[dateFormat setDateFormat:#"dd MMM YYYY"];
dateStr = [dateFormat stringFromDate:date];
NSLog(#"'%#'", dateStr);
And in log it returns
'03-02-2017'
'2016-12-24 16:00:00 +0000'
'25 Dec 2016'
Anyone know why this happened? I searched in google that most cases are causing by timezone. However, I'm pretty sure it's not about timeZone because the difference of dates are too large, but I can't figure out the root of this problem.
I think your dateformat is wrong.
It should be
dd-MM-yyyy
I found that my format is wrong from answer objective-c - NSDateFormatter returns wrong date
and reference by http://nsdateformatter.com .
It should be "yyyy" instead of "YYYY"

objective c convert string to UTC- the time is moving in error

I return a string from my database and value is '04/27/2016 1:16pm'. This value is already in UTC.
Now I want to convert that string to NSDATE, keeping it in UTC. When I try to convert string to date, the time is actually moving by 1 hour.
This is how I am doing it
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
//date conversions
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
the result is coming back as 2016-04-27 12:16:01 UTC.
result is
2016-04-27 10:12:44.612 x[1558:48608] tuploadtime=4/27/2016 2:10:33 PM, duploadtime=2016-04-27 12:10:33 +0000
basically the time is moving back 1 hour but I want to keep it the same.
hope I am making sense
Proper String Format for Date is most Important.
There are some methods to have HOURS format as follow with their differences..
kk: will return 24 format Hour in (01-24) hours will (look like 01, 02..24).
HH will return 24 format Hour in (00-23) hours will(look like 00, 01..23).
hh will return 12 format Hour (look like 01, 02..12).
so you should use your code like
NSString *tuploadtime = [tempDictionary valueForKey:#"uploadTime"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yyyy hh:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *duploadtime = [[NSDate alloc] init];
duploadtime = [dateFormatter dateFromString:tuploadtime];
NSLog(#"tuploadtime=%#, duploadtime=%#", tuploadtime, duploadtime);
for More Date format refer this link
Your date format string is inconsistent:
[dateFormatter setDateFormat:#"MM-dd-yyyy HH:mm:ss a"];
HH means to use a 24-hour format. But then you also used a to indicate AM/PM. Using both is confusing the formatter and giving you off-by-one. You meant to use hh here.

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