NSDateFormatter issue while converting NSDate to NSString - iphone

NSDateFormatter is providing a default date of
2001-01-01 00:00:00 +0000
I need it to format in the format of January, 01 2013 04:00:00
Here is my code:
NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMMM, dd yyyy HH:mm:ss"];
NSDate *startDt=[_calendarDetailsDict objectForKey:#"dtStart"];
NSDate *endDt=[_calendarDetailsDict objectForKey:#"dtEnd"];
NSString *str=[formatter stringFromDate:startDt];
NSString *str1=[formatter stringFromDate:endDt];

NSString *dateStr =#"2012-09-15 00:11:59";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateStr];
dateFormat setDateFormat:#"MMMM, dd yyyy HH:mm:ss"];
NSString *dateString = [dateFormat stringFromDate:date];
NSLog(#"date: %#", dateString);
may help u.

Related

DateFormat Conversion

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

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

convert NSstring date to UTC date

i need to convert some date that i have in NSString to NSString in this format: yyyy-MM-dd HH:mm:ss zzz
i write this code:
date input: date ---> 27/03/2012
-(NSString *)setDateStringFormat:(NSString *)date
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
//[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate *dateFormatted = [formatter dateFromString:date];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
return [formatter stringFromDate:dateFormatted];
}
output: 2012-03-27 00:00:00 GMT
as you all can see i got the date in GMT and not in UTC
some one have an idea?
try this method..
-(NSString *)getUTCFormateDate:(NSString *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
[dateFormatter release];
return dateString;
}
i think, Here GMT show Standard time and GMT and UTC both are same.
please look on below links:
http://wwp.greenwichmeantime.com/
http://www.worldtimeserver.com/convert_time_in_UTC.aspx
thanks

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.

How to change nsstring to nsdate format in iphone?

I have struggling with to convert nsstring to nsdate format. I have a date in nsstring format like (2011-08-25 18:30:00 +0000), but i want to change it in nsdate format (08/26/2011). How to change it? Can any one help me please? Thanks in advance... I have spend one day to solve this but i cant find out the solution.. Please help me friends....
This is my code,
NSString *dateString = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"Converted date is %#",dateFromString);
Output is,
Converted date is (null)
Yuva.M
NSString *dateWithInitialFormat = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
NSLog(#"dateWithNewFormat: %#", dateWithNewFormat);
From NSLog: dateWithNewFormat: 08/25/2011
See Date Format Patterns
You need to use an NSDateFormatter and set the date format to match how you're storing it, like the below.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString: myDateString];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMMM dd, yyyy"];
NSDate* d = [df dateFromString:#"January 06, 1992"];
NSLog(#"%#", d);
NSDate *dispalyDate = d;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM"];
NSString *str = [dateFormat stringFromDate:dispalyDate];
NSLog(#"%#", str);