How to make date conversion? - iphone

I am getting date in string formate ad "2012-04-11T07:51:10+0000" & I want to convert it to NSDate as "11-04-2012". I dont understand how to use "setDateFormat" for this.
This is what I am doing--
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'+'zzzz"];
Please help.
And whatever NSDate I will get I again want to convert to string ie. only 11-04-2012.
I have searched on ggl. But no satisfactory result :(
please help.

Here is how to parse the string to generate a date:
NSString *dateString = #"2012-04-11T07:51:10+0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyy-MM-dd'T'HH:mm:ssZ";
NSDate *date = [formatter dateFromString:dateString];
NSLog(#"Date: %#", date);
Now you can generate a new string representation of the date as follows:
formatter.dateFormat = #"dd-MM-yyyy";
NSString *newDateString = [formatter stringFromDate:date];
NSLog(#"New string: %#", newDateString);
// 11-04-2012
The problem with the dateFormat you were using is that you don't have to escape all the symbols such as 'MM' and that zzzz is not the correct symbol to parse the timezone of type +0000.
Have a look at this documentation for more information.

Try this code.
NSString *dateStr = #"Wed, 22 Jun 2011 12:36:00 +0100"; // change value base on your requirement.
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"EEE, d MMM yyyy HH:mm:ss ZZZ"]; // Change format base on dareStr.
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEE, MMM d YYYY"]; // Change format base on your output requirement
dateStr = [dateFormat stringFromDate:date];
NSLog(#"Date -- %#",dateStr);
[dateFormat release];

Related

Parsing custom string gives wrong date

I have a string that has a date format of HH:mm so for example it could 12:00 or 22:00, and I input that into my NSDateFormatter by setting it as the date format. I just need to construct a custom date. The problem is when I have done this and I get my parsed string as 2012-05-17 12:00:00 +0000 if the date is the 17th of May.
NSDate *today = [NSDate date];
NSDateFormatter *output = [[NSDateFormatter alloc] init];
[output setDateFormat:[NSString stringWithFormat:#"yyyy-MM-d %#:00 +0000",#"12:00"]];
NSString *finalTodayString = [output stringFromDate:today];
parsedDateString = [NSString stringWithString:finalTodayString];
The problem is when I parse it again to just include the HH:mm I get something totally different. For example if I have this code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-d HH:mm ZZZ"];
NSDate *date = [dateFormatter dateFromString:fullDateString];
NSDateFormatter *dateFormmater2 = [[NSDateFormatter alloc] init];
[dateFormmater2 setDateFormat:#"HH:mm"];
NSString *string =[dateFormmater2 stringFromDate:date];
Then the string should be 12:00 but instead it becomes 14:00. Please help.
Remove the Greenwich Meridian time "+0000" from the first code

NSString with date: reduce size of date

I have a NSString like Mon Feb 06 20:02:17 +0000 2012. I want to write it in a shorter way, maybe like: DD-MM-YY, HH:MM. I have think that maybe I can convert it to NSDate and again to a shorter NSString, but I don't know how to convert this strange date format to NSDate.
If you need more information, ask me.
Thanks!
You need to use an NSDateFormatter to tell NSDate the format the original date is in and the format you want it in, then you can convert between the two like this:
NSString *oldDate = #"Mon Feb 06 20:02:17 +0000 2012";
NSString *oldFormat = #"EEE MMM dd HH:mm:ss ZZ yyyy";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:oldFormat];
NSDate *date = [dateFormatter dateFromString:sourceString];
NSString *newFormat = #"dd-MM-yy, HH:mm";
[dateFormatter setDateFormat:newFormat];
NSString *newDate = [dateFormatter stringFromDate:date];
There are similar question and answers in StackOverflow and a tutorial that may help you:
Tutorial:
http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html
Similar questions:
NSString to NSDate
Convert NSString->NSDate?
How to convert NSDate to NSString?
Convert NSString of a date to an NSDate
EDIT
NSString *dateStr = #"20081122";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
Try with NSDateFormatter.
You can convert to NSDate using one of its method:
- (NSDate *)dateFromString:(NSString *)string
And then convert that to whatever you want using NSDateFormatter.
Edit: Actually NSDate has a class method so you may check that as well:
+ (id)dateWithString:(NSString *)aString

NSString to NSDate keeps getting null

I know this been asked for so many times but I always end up getting null in my NSDate. I have a string like this "January 16, 2012 21:44:56" I want to convert it to "January 16, 2012 09:44:56 PM". I want to add a PM in the converted date and convert the 24 hour time format to 12 hour time format. Here's my code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY HH:ii:ss a"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
As Ali3n correctly pointed out, you should first set the format of dateString to the formatter to get a valid date object. Next you should set the formatter's format to the desired one and continue. Do the following:
NSString *dateString = #"January 16, 2012 21:44:56";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, yyyy HH:mm:ss"];
NSDate *dateFromString;
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"MMMM dd, YYYY HH:mm:ss a"];
NSString *stringFromDate = [dateFormatter stringFromDate:dateFromString];
#"MMMM dd, YYYY HH:ii:ss a" this format should match with the date the ypu are passing to the date formatter ..
There is an error in your format string an also you need to tell the formatter the Locale in which your date string is presented.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
[dateFormatter setDateFormat:#"MMMM dd, yyyy HH:mm:ss a"];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release], dateFormatter = nil;
Setting the Local is very important since you have an name of a date in your input. You will need to tell the NSDateFormatter is wich language the name will be. In the example given it is in english. I've you run you code without setting the local on a device where the language is not set to english it wil fail to parse the date.
Try to escape literals in the format string.
[dateFormatter setDateFormat:#"MMMM dd',' YYYY HH:ii:ss a"];
As for your requirements you have to change the dateFormatter.Check this link for more.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMMM dd, YYYY hh:mm:ss a"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
NSLog(#"%#",dateString);

NSDate from NSString gives null result

I am using following code to generate NSDate -> NSString
+(NSString *)getCurrentTime
{
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString* str =[dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(#"%#",str);
return str;
}
everything is fine in above code. I am using above code to store string in Database. Now while retrieving that string gives me NULL. Following is my code to retrieve date in specific format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt =[dateFormatter dateFromString:crdInfo.swipeTime];
NSLog(#"Date : %#",dt);
[dateFormatter release];
How should I retrieve or store with particular format?? My crdInfo.swipeTime is retrieving String propertly...
First off, why not just store the NSDate object or epoch timestamp? This will give you much more flexibility in the future.
Now to your problem, I suspect it is due to your configuration of the NSDateFormatter, you're saving it in one format and trying to convert it to a date using a different format. Make the formats the same and try again. If you want to display it differently than it is stored you're likely going to need to convert it to and NSDate using the stored format and then again use another date formatter to get it in the format you want it as a string.
As Narayana suggested you need to retrieve the date with same format as you have stored. Retrieve it as below : -
NSDateFormatter *reDateFormatter = [[NSDateFormatter alloc] init];
[reDateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[reDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt = [reDateFormatter dateFromString:str];
NSLog(#"The Date : %#",dt);
[reDateFormatter setDateFormat:#"hh:MM:SS a"];
NSString *currentTime = [reDateFormatter stringFromDate:dt];
NSLog(#"%#",currentTime);
Hope it helps you.
Try to format it to dd-MM-yyyy hh:mm:ss a.
You wrote dd-MM-yyyy hh:MM:SS a where MM in hh:MM:SS gives month which is unrecognized in this format and there is no point writing upercase SS for seconds
Hope you understand it.

NSDate from NSString problems

Getting a NSString as follows:
NSString *lastSyncDate = [[items objectAtIndex:0] objectForKey:#"LastSyncDate"];
This value is: 1/1/2010 12:00:00 AM
I am using the following to convert to a date:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeStyle:NSDateFormatterFullStyle];
[df setDateFormat:#"MMMM d, y HH:mm:ss ZZZ"];
NSDate *mySyncDate = [df dateFromString:lastSyncDate];
This gives me a null value?
Your date format doesn't match the format of the string.
Try
[df setDateFormat:#"M/d/y hh:mm:ss a"];
or
[df setDateFormat:#"M/d/y h:mm:ss a"]; // if the hours aren't zero-padded
The format strings are a Unicode standard described at http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
I agree with Seamus Campbell. However, be aware that setting the time style on the formatter (NSDateFormatter) will have no effect on the result returned by dateFromString.