DateFormat Conversion - iphone

I have a string that is in this format:
2012-11-08T13:50:05.284-08:00
I need to convert it into this format:
November 8, 2012 1:50 PM,
I have identified the pattern of the date to YYYY-MM-DDThh:mm:ss.sTZD, like this:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DDThh:mm:ss.sTZD"];
NSDate *date = [dateFormat dateFromString:#"2012-11-08T13:50:05.284-08:00"];
[dateFormat setDateFormat:#"MMMM dd, yyyy hh:mm a"];
NSString *newDateStr = [dateFormat stringFromDate:date];
Could anyone please tell me what's wrong here? Is the format template correct or not?

Try this:As I getting valid output.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.S'-'S':'S"];
NSDate *date = [dateFormat dateFromString:#"2012-11-08T13:50:05.250-8:00"];
[dateFormat setDateFormat:#"MMMM dd, yyyy hh:mm a"];
NSString *newDateStr = [dateFormat stringFromDate:date];
NSLog(#"newDateStr is %#", newDateStr);

You can try like this just ( just remove last 5 characters from the string )
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSDate *date = [dateFormat dateFromString:#"2012-11-08T13:50:05.284"];
[dateFormat setDateFormat:#"MMMM dd, yyyy hh:mm a"];
NSString *newDateStr = [dateFormat stringFromDate:date];

Related

String converson to date with unknown formate iphone

I have a date string like this "2013-09-05T06:40:19Z" from server
i want to convert it into date which format should be same as my iphone follow .
how to do this ?
when i do this ?
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MMM dd yyyy"];
[dateFormatter1 setLocale:[NSLocale currentLocale]];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date =[dateFormatter1 dateFromString:time];
NSLog(#"%#",date)//getting null here
i am getting null in date.
NSString *dateStr = #"2013-09-05T06:40:19Z";
NSString *outDateFormate = #"MMM dd yyyy";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale currentLocale]];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *intime = [dateFormat dateFromString:dateStr];
[dateFormat setDateFormat:outDateFormate];
NSString *dateWithNewFormat = [dateFormat stringFromDate:intime];
Since you are getting the date in specified format so you need to define your date formatter accordingly
NSString *gameDateString = #"2013-09-05T06:40:19Z";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *gameDate = [dateFormatter dateFromString:gameDateString];
NSLog#("** The date is %#",gameDate);
// Now convert date to string in the format which you required.
[dateFormatter setDateFormat:#"MMMM dd, yyyy"];
NSString *dateString = [dateFormatter stringFromDate:gameDate];
NSLog#("** The date string is %#",dateString);
Hope this helps.
NSString *str=#"2013-09-05T06:40:19Z";
str=[[str stringByReplacingOccurrencesOfString:#"T" withString:#" "]stringByReplacingOccurrencesOfString:#"Z" withString:#""];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormatter1 setLocale:[NSLocale currentLocale]];
[dateFormatter1 setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *date =[dateFormatter1 dateFromString:str];
[dateFormatter1 setDateFormat:#"MMM dd yyyy"];
NSString *newDate=[dateFormatter1 stringFromDate:date];
NSLog(#"%#",newDate)

Date will be different when convert from NSString to NSDate

This is My code
NSString * dateStr = #"7/25/2013 12:00:00 AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"dateStr : %# , date : %#",dateStr,date);
But my Date will be one Day behind the actual date. See this log
dateStr : 7/25/2013 12:00:00 AM , date : 2013-07-24 18:30:00 +0000
What am I doing wrong?
Try this.
NSString * dateStr = #"7/25/2013 12:00:00 AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
[dateFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"dateStr : %# , date : %#",dateStr,date);
Try Something like this . this will help you
NSString * dateStr = #"7/25/2013 12:00:00 AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSString * dateStr = #"7/25/2013 12:00:00 AM";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:dateStr];
NSLog(#"dateStr : %# , date : %#",dateStr,date);
Setting the time zone fixes this issue

NSDate is not transformed correctly

I am trying to transform a datestring into another format. I am doing it like this.
NSLog(#"datestring is NOW %#",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DD"];
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(#"date transformed %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/YYYY"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER %#",dateStr);
But this is what I get from my NSLOGs
2013-04-18 08:43:36.181 mosaqua2[9629:907] datestring is NOW 2013-05-04
2013-04-18 08:43:36.184 mosaqua2[9629:907] date transformed 2013-01-03 23:00:00 +0000
2013-04-18 08:43:36.184 mosaqua2[9629:907] datestring is AFTER donderdag, 03/01/2013
What I want it to be is
Saterday, 04/05/2013
Can anybody help me ?
Use my this bellow custom method for convert Date Format ...
-(NSString *)changeDateFormat:(NSString*)stringDate dateFormat:(NSString*)dateFormat getwithFormat:(NSString *)getwithFormat{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:dateFormat];
NSDate *date = [dateFormatter dateFromString:stringDate];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:getwithFormat];
NSString *convertedString = [dateFormatter stringFromDate:date];
NSLog(#"Converted String : %#",convertedString);
return convertedString;
}
and use it like bellow...
NSString *strSDate = [self changeDateFormat:_dayObject.p_date dateFormat:#"yyyy-MM-dd" getwithFormat:#"EEEE, dd/MM/yyyy"];
NSLog(#"datestring is AFTER %#",strSDate);
See My Blog with this post...
Check this.....
NSLog(#"datestring is NOW ==>> %#", _dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDate *date = [dateFormat dateFromString: _dayObject.p_date];
NSLog(#"date transformed ==>> %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER ==>> %#",dateStr);
OUTPUT:~
datestring is NOW ==>> 2013-05-04
date transformed ==>> 2013-05-04 00:00:00 +0000
datestring is AFTER ==>> Saturday, 04/05/2013
Check the character case of parameters
NSLog(#"datestring is NOW %#",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; //MODIFIED
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(#"date transformed %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/yyyy"]; //MODIFIED
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER %#",dateStr);
Please try ..this it will work to get the week name
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"eeee"];
NSCalendar * cal = [NSCalendar currentCalendar];
NSUInteger numWeekdays = [cal maximumRangeOfUnit:NSWeekdayCalendarUnit].length;
NSDateComponents * comp = [[NSDateComponents alloc] init];
for( NSUInteger day = 1; day <= numWeekdays; day++ ){
[comp setWeekday:day];
[comp setWeek:0];
NSDate * date = [cal dateFromComponents:comp];
NSString * dayName = [dateFormat stringFromDate:date];
NSLog(#"%#", dayName);
}
You are doing all right except setTimeZone , comment that you will get your output.
NSLog(#"datestring is NOW %#",_dayObject.p_date);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DD"];
NSDate *date = [dateFormat dateFromString:_dayObject.p_date];
NSLog(#"date transformed %#",date);
[dateFormat setDateFormat:#"EEEE, dd/MM/YYYY"];
//[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]]; //NOT NEEDED
NSString *dateStr = [dateFormat stringFromDate:date];
NSLog(#"datestring is AFTER %#",dateStr);
Change this : "YYYY"
To : "yyyy"
Reason for the error,
It's a common mistake to use YYYY. yyyy specifies the calendar year
whereas YYYY specifies the year (of “Week of Year”), used in the ISO
year-week calendar. In most cases, yyyy and YYYY yield the same
number, however they may be different. Typically you should use the
calendar year.

NSDateFormatter how can I format this date?

I am attempting to format an NSString which looks like:
#"2012-07-19T02:58:33Z"
to an NSDate with the following format:
#"MMMM dd, yyyy HH:mm a"
The NSDateFormatter always returns nil and I think it is because it cannot convert the NSString in it's current format to the desired one. Here is my code:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *date = [formatter dateFromString:dateString];
Do I need to store the date in a differant format? Can I make this conversion possible with the current NSString?
Thanks!
Your dateFormatter doesn't match your dateString... Try this
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *date = [formatter dateFromString:dateString];
[formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
dateString=[formatter stringFromDate:date];
now dateString contains "July 19, 2012 02:58 AM"
Hope this helps
Try it.
NSString *myDateString=#"2012-07-19T02:58:33Z";//#"2009-07-06T17:42:12";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *myDate = [[NSDate alloc] init];
myDate = [dateFormatter dateFromString:myDateString];
NSLog(#"MyDate is: %#", myDate);
Your problem has already been answered.
Store your string in a NSDate with the format of the original string:
NSString *originalDateString = #"2012-07-19T02:58:33Z";
NSDateFormatter *originalFormatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *originalDate = [formatter dateFromString:originalDateString];
NSDateFormatter *newFormatter = [formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
NSString *newDateString = [formatter stringFromDate:originalDate];

iPhone/OS X: Convert UTC format date&time value Calendar required format

I am struggling a bit to convert the UTC time to the date format what i want.
2012-03-10T09:30:00Z
needs to be converted to the below format
3:00:00pm April 19, 2012
Could you someone please help me to resolve this?
I tried like below,
NSString *formatted;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:#"HH:mm:ss EEEE, MMM d"];
// Here datetime= 2012-03-10T09:30:00Z
NSDate *date = [NSDate dateWithTimeIntervalSince1970: [datetime intValue]];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
But, its printing wrong date like "06:03:32 Thursday, Jan 1". Please correct me.
UPDATED: I am having another problem, its not giving me the correct time, but its giving the correct date though. My Code is below.
NSString *formatted;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:#"HH:mm:ss EEEE, MMM d"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [df dateFromString:datetime];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
Thank you!
To convert an ISO date string to NSDate:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [df dateFromString:date_str];
You can then convert it to your local time zone:
[df setTimeZone:[NSTimeZone localTimeZone]];
[df setDateFormat:#"HH:mm:ss EEEE, MMM d"];
In your code, your [datetime intValue] is probably incorrect.