Date will be different when convert from NSString to NSDate - iphone

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

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

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 issue while converting NSDate to NSString

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.

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.