Changing date format on iOS - ios

I have some dates in this format :
2012-06-26 12:00:00 +0000
and what i want to do is convert them in this format :
Jun 26, 2012 12:00:00 AM
Is there an easy way to do this on obj-c , or i must parse everything hardcore , and make my own function for this? If yes , any ideas how this would look like , cause i am new on iOS.
Thank you very much for reading my post :D

To convert from 1 string to the other use the following
NSString *inputString = #"2012-06-26 12:00:00 +0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];//input
NSDate *date = [formatter dateFromString:inputString];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSString *outputString = [formatter stringFromDate:date];
NSLog(#"output : %#",outputString);

First, you should convert your string (with a NSDateFormatter ) in a NSDate and than convert it back to a NSString (still with the same NSDateFormatter) with the target format.
Here is an example of a NSDateFormatter :
NSString *dateString = #"2013-04-01T22:00:00Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone];
NSDate *theDate = [dateFormatter dateFromString:dateString];

Related

How to convert a string Date to NSDate in iOS

I googled so many times, but I am not satisfied with given answer. Please anyone can give correct answer what I need.
This is the retrieve date string from DB : 2015-05-27 10:19 (yyyy-MM-dd HH:mm)
I want to convert into NSDate.
My code is like Below:
NSString *date_str = #"2015-05-27 10:19";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:date_str];
NSLog(#"date == %#",date);
But output is : date == 2015-05-27 04:49:00 +0000
Its showing 04:49:00 time , but my retrieve time is 10:19.
How can i retrieve perfect time from DB.
Please help out me..
Just convert your date to GMT time like this:-
NSString *date_str = #"2015-05-27 10:19";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormat setTimeZone:gmt];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:date_str];
And the output is :-
date == 2015-05-27 10:19:00 +0000
And to get the date without +0000, you can store it in NSString directly:-
NSString *s = [dateFormat stringFromDate:date];
Output :-
2015-05-27 10:19
You are facing the timezone issue with conversion, because server time and local timezone may have difference. So handle this you need to set the timezone to your dateformatter like
[dateFormater setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];

how to convert string into date format in iOS?

I have string from web service like 12/31/2013 09:12:15 A.M.
Now I Want convert it like 12 Dec 2013 09:12:15 A.M.
with the use of NSDAteFormatter in iOS
I'm giving you the answer, but downvoting your question for being too elementary and whose solution could easily found with a simple search.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [formatter dateFromString:myDateString];
[formatter setDateFormat:#"dd MMMM yyyy aa:mm:ss a"];
NSString *newDate = [formatter stringFromDate:date];
NSString *dateStr = #"12/31/2013 09:12:15 AM";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
[dateFormat release];

NSDateFormatter in iOS

I'm using the Twitter API to download the number of tweets because I need them in my app for iOS. I'm using this function to do this. The problem is that I need to know the day, month and year when the tweet was written.
The date that comes back is like this:
Thu Oct 25 10:34:58 +0000 2012
But I need a date like this format:
25/10/2012 10:34
So, I want to transform the first format to the second format. I've tried to do it with NSDate and NSDateFormatter, obtaining a NSDate with the first NSString, and after transforming this NSDate in the second NSString, but I didn't get it.
NSArray *dateArray = [tweets valueForKey:#"created_at"];
NSString *dateString = [dateArray objectAtIndex:0]; // Thu Oct 25 10:34:58 +0000 2012
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#", date);
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSString *realDateStr = [dateFormatter stringFromDate:date];
NSLog(#"%#", realDateStr);
The two NSLogs each return null.
Can anybody help me? Thanks.
Try setting the date format originally to parse out the date string you are getting, like this:
NSString *dateString = #"Thu Oct 25 10:34:58 +0000 2012";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd hh:mm:ss Z yyyy"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#", date);
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm"];
NSString *realDateStr = [dateFormatter stringFromDate:date];
NSLog(#"%#", realDateStr);
Try this simple method:
NSString* string=#"26-01-2014";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-mm-yyyy"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:string];
NSLog(#"Date = %#",date);

Conversion of NSString to NSDate failing

I have the following piece of code..
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/YYYY HH:mm"];
NSString *dateString = #"10/27/2012 18:00";
NSDate *parsedDate = [dateFormatter dateFromString:dateString] ;
NSLog(#"Parsed Date.. %#",[parsedDate description]);
I am getting the NSLog statement as Parsed Date.. 2011-12-25 00:00:00 +0000. I would appreciate any help in resolving this. I just want to convert that NSString to its NSDate equivalent. Tried using NSTimeZone as well, but no effect. What am I missing here?
You need to use lower case year indication, yyyy instead of YYYY:
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];
If you want to get the timezone right you need to set the timezone of the NSDateFormatter. By default it uses the timezone of your device. For the time at GMT use the following.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];
NSString *dateString = #"10/27/2012 18:00";
NSDate *parsedDate = [dateFormatter dateFromString:dateString] ;
NSLog(#"Parsed Date.. %#",[parsedDate description]);
You are using an incorrect format string. See the documentation. Basically, capital YYYY is "week of year year". Try to use lowercase yyyy instead, i.e.
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm"];

Strange date format behaviour , iOS

I am trying to convert a date from this :
2012-06-26 12:00:00 +0000
to this :
Jun 26, 2012 12:00:00 AM
If i use this code , it works :
NSString *inputString = #"2012-06-25 12:00:00 +0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];//input
NSDate *date = [formatter dateFromString:inputString];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSString *outputString = [formatter stringFromDate:date];
NSLog(#"Output Date is: %#" , outputString);
and the date printed is correctly this : Jun 26, 2012 12:00:00 AM
However the thing gets really strange when i dont hardcode the date , but use a function to get it. The code :
NSString *inputString2 = [self.selectedDate description];
NSLog(#"%#",inputString2);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];//input
NSDate *date = [formatter dateFromString:inputString2];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSString *outputString = [formatter stringFromDate:date];
NSLog(#"Output Date is: %#" , outputString);
When i print in the beginning of the code the inpuString2 variable just to make sure that the date is correctly delivered by the function there i have this: 2012-06-25 12:00:00 +0000 which is the correct date. But now the output date is nil when printed!!
Any ideas?? how is this possible??
Its the date format string for formatter
You have
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss z"];
Try instead
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
Note: HH not hh and Z not z.
RE: the second formatter string - you could change that to
[formatter setDateFormat:#"MMM dd, yyyy HH:mm:ss a"];
(but I thought you were just asking about the nil)
Am assuming selectedDate date is NSDate, instead of converting use the following
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMM dd, yyyy hh:mm:ss a"];
NSString *outputString = [formatter stringFromDate:self.selectedDate];
NSLog(#"Output Date is: %#" , outputString);
This may not answer your question, but it should give you correct date

Resources