Date/time conversion to user's local time - issue - iphone

My application grabs a date / time from a remote server which is alway in GMT +1 (UTC/GMT +1 hour) timezone.
The format the server is providing is :
24 08 2011 08:45PM
I would like to convert this time stamp into the equivalent time/date of the users time zone (the user can be anywhere in the world).
thus as an example :
24 08 2011 08:45PM coming from the server should be presented
24 08 2011 09:45PM to an Italian user (rome) (GMT + 1)
This code works on some timezones but i have a bad feeling that there is something very wrong about it and that there is a much more elegant way to do it
NSString *dateString = #"24 08 2011 09:45PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSDate *dateFromString = [[[NSDate alloc] init] autorelease];
dateFromString = [dateFormatter dateFromString:dateString];
NSDate* sourceDate = dateFromString;
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"BST"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] ;
NSString *thePubDate = [dateFormatter stringFromDate:destinationDate];//[appLogic getPubDate];
NSLog(#"Result : %#",thePubDate);
[dateFormatter release];
//[dateFromString release];
[destinationDate release];
I will appreciate your thoughts and suggestions on the matter

Just set the timeZone in the dateFormatter, This code is enough
NSString *dateString = #"24 08 2011 09:45PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"BST"];
[dateFormatter setTimeZone:sourceTimeZone];
NSDate *dateFromString = [dateFormatter dateFromString:dateString];
The dateFromString will now have the date 24 08 2011 08:45PM(GMT).. Then to convert this to string with local time just code the following,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MM yyyy hh:mma"];
NSString *stringFromDAte = [dateFormatter stringFromDate:dateString];

Related

NSDateFormatter is returning null date

Below is my code
NSString *dateandtime =[NSString stringWithFormat:#"%#",self.eventtime];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateandtime];
if i print my self.event name i am getting the output as Mon, 7 Jan 2013 15:30:00 CST but when i pass this string in NSDateFormatter i am getting null value i don't know why i tried many combinations of date formats, but i cant solve it, can anyone help me
i solved my problem as Per Prateek comments, now its working for me...Thanks Prateek.
in your self.event, you are getting CST, it should be time zone for example +0530
Use :
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss z"];
Use this code...
- (NSString *)formattedDate:(NSString*)dateSte {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[NSTimeZone resetSystemTimeZone];
NSTimeZone *gmtZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:gmtZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateSte];
[dateFormatter setDateFormat:#"EEE,MMM d,''yyyy"];
NSString *strDate = [dateFormatter stringFromDate:date];
return strDate;
}
Use the below method for getting the NSDate from NSString date
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [self convertStringToDate:dateandtime];
Paste this below method in your .m file...
-(NSDate *)convertStringToDate:(NSString *) date {
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:#"EEE, d MMM yyyy HH:mm:ss 'CST'"];// set format here format in which the date should be
date = [date stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
nowDate = [formatter dateFromString:date];
// NSLog(#"date============================>>>>>>>>>>>>>>> : %#", nowDate);
return nowDate;
}
For More Information About NSDate component see this post of My Blog NSDate-formate-info-for-iphone-sdk

Set manual time to today date and make datetime object in ios6

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd";
NSTimeZone *gmt = [NSTimeZone defaultTimeZone];
[dateFormatter setTimeZone:gmt];
NSString *cur_date= [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#",cur_date);
Now I am setting the manual time to today's date:
NSString *cur_time=#"13:05:08";
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *date_obj=[dateFormatter dateFromString:[cur_date stringByAppendingFormat:#" %#",cur_time]];
NSLog(#"%#",date_obj);
but I am getting today date in NSlog to show as
2012-09-28 07:35:08 +0000
even if I set the time as 13:05:08.
How do I set the manual time to the current date and make one datetime object from that?
//Try using Time Interval
[myDate addTimeInterval:entertimeinterval];
Do this
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd";
//You can either use UTC or GMT
NSTimeZone *gmt = [NSTimeZone timeZoneWithName:#"GMT"];// This is where I made the change
[dateFormatter setTimeZone:gmt];
NSString *cur_date= [dateFormatter stringFromDate:[NSDate date]];
NSLog(#"%#",cur_date);
NSString *cur_time=#"13:05:08";
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *date_obj=[dateFormatter dateFromString:[cur_date stringByAppendingFormat:#" %#",cur_time]];
NSLog(#"date %#",date_obj);
Hope this will work

Setting NSDate from NSString not working

myObject.theEnd and myObject.theStart are strings and it has the date format of Thu Feb 31 like wise...
NSDateFormatter *format=[[NSDateFormatter alloc] init];
[format setDateFormat:#"EEE MMM dd"];
NSDate *end = [format dateFromString:myObject.theEnd];
NSDate *start = [format dateFromString:myObject.theStart];
NSDate *current = [NSDate date];
When theEnd is Thu Feb 31, NSDate *end shows as 1970-5-19 14:30 +0000. It is the same with NSDate *start and NSDate *current.
Why is this ? and How can i solve this ?
I bet theEnd is not showing as 1970-05-19 14:30 +0000. I bet it's showing as (null) since there is never a 31st February! e.g. I get:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"EEE MMM dd"];
NSDate *end = [format dateFromString:#"Thu Feb 31"];
NSLog(#"end = %#", end);
=>
2012-05-20 16:38:31.620 test-date[42307:707] end = (null)
Also I bet that you are in a timezone that is 9.5 hours east of UTC. And 1970-05-19 14:30 +0000 is actually what you will then get when you parse today's date of Sun May 20. For instance I am currently in BST so UTC+0100 and I get:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"EEE MMM dd"];
NSDate *start = [format dateFromString:#"Sun May 20"];
NSLog(#"start = %#", start);
=>
2012-05-20 16:38:31.621 test-date[42307:707] start = 1970-05-19 23:00:00 +0000
Since it's parsing to 1970-05-20 00:00:00 (there's no year so that component is "0" = 1970) in the current timezone, which is 1970-05-19 23:00:00 +0000 in UTC.
If you want to get around that problem, then set the timezone of the formatter to UTC:
[format setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
To get the year into the current year you could do something like this:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"EEE MMM dd"];
[format setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *start = [format dateFromString:#"Sun May 20"];
NSLog(#"start = %#", start);
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents = [gregorian components:NSYearCalendarUnit fromDate:today];
NSDateComponents *startComponents = [gregorian components:(NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:start];
[startComponents setYear:[todayComponents year]];
[startComponents setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
start = [gregorian dateFromComponents:startComponents];
NSLog(#"start = %#", start);
It's not that pretty, but it works. Alternatively you could just append the current year to the string you pass into the formatter and add yyyy to the formatter style:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"EEE MMM dd yyyy"];
[format setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *todayComponents = [gregorian components:NSYearCalendarUnit fromDate:today];
NSString *dateString = [NSString stringWithFormat:#"Sun May 20 %i", [todayComponents year]];
NSDate *start = [format dateFromString:dateString];
NSLog(#"start = %#", start);

Problem in getting time

HI i need to increment 15 minutes in time intrval which i get in the webservice .
The code is as follows
NSString *dateString =obj.String_date_start; // start time is 2011-07-01
dateString = [dateString stringByAppendingString:[NSString stringWithFormat:#" %# +0000",obj.String_time_start]]; //After this statement the date is 2011-07-01 03:00:00 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSDate *scheduledForYellow = [dateFromString dateByAddingTimeInterval:900];
[dateFormatter release];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSString *strTime = [dateFormatter1 stringFromDate:scheduledForYellow];
[dateFormatter1 release];
NSArray *aryy = [strTime componentsSeparatedByString:#" "];
NSLog(#"end time ======>>>%#",[aryy objectAtIndex:1]);
obj.String_StaticEndTime = [aryy objectAtIndex:1];
Here to add 15 minutes to my start time i have written the above code , but in end time that is [aryy objectAtIndex:1] does not give me the correct time.What i wanted to do is increment 15 minutes for whatever date i get in start date .Dont know whats the issue is.
`
It must be because of the default timezone (local timezone) that the date formatter uses to generate the string. I have modified your code a bit to reuse the date formatter and fixed a leak with dateFromString.
NSString *dateString = obj.String_date_start; // start time is 2011-07-01
dateString = [dateString stringByAppendingString:[NSString stringWithFormat:#" %# +0000",obj.String_time_start]]; //After this statement the date is 2011-07-01 03:00:00 +0000
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate * dateFromString = [dateFormatter dateFromString:dateString];
NSDate * scheduledForYellow = [dateFromString dateByAddingTimeInterval:900];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSString * strTime = [dateFormatter dateFromString:scheduledForYellow];
NSArray * aryy = [strTime componentsSeparatedByString:#" "];
NSLog(#"end time ======>>>%#",[aryy objectAtIndex:1]);
obj.String_StaticEndTime = [aryy objectAtIndex:1];
This should fix the issue. Let me know if you face any issues.

iPhone: NSDate convert GMT to local time

I currently have an API call returning me a date/time in the format: 2010-10-10T07:54:01.878926
Can I assume this is GMT? I also converted this to an NSDate object. Is there a way to use the local iPhone time to recalculate the time?
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
This code will convert the GMT time to the device's local time.
NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:pubDate];
NSDate *sourceDate = [NSDate dateWithTimeIntervalSince1970:gmtTimestamp];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [destinationTimeZone secondsFromGMTForDate:0];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm";
[dateFormatter setTimeZone:destinationTimeZone];
NSString *localTimeString = [dateFormatter stringFromDate:destinationDate];
NSDate *now = [NSDate date];
NSLog(#"%#", [now dateWithCalendarFormat:#"%Y-%m-%d %H:%M:%S"
timeZone:[NSTimeZone timeZoneWithName:#"GMT"]]);