iPhone: NSDate convert GMT to local time - iphone

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

Related

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

iPhone: Date conversion issues

I need to check an event date, which should be between Current date and 60 days from now. The below code is used, but it is NOT working correctly. Please note, i'm getting event string like this - "2012-04-14T16:50:02Z" from my server.
// current date
double currDateInMilliSecs = [NSDate timeIntervalSinceReferenceDate] * 1000;
NSLog(#"currDateInMilliSecs: %f", currDateInMilliSecs);
// sixty days
double sixtydaysvalue = 60.0 * 24.0 * 3600.0 * 1000.0;
NSLog(#"sixtydaysvalue: %f", sixtydaysvalue);
// add current date + sixt days
double sixtyDaysMilliSecsFromCurrDate = currDateInMilliSecs + sixtydaysvalue;
NSLog(#"sixtyDaysMilliSecsFromCurrDate: %f", sixtyDaysMilliSecsFromCurrDate);
// check does the event date between current date + 60 days
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//[df setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
//[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
// [eventDict objectForKey:#"begin_at"] gives date string like this "2012-04-14T16:50:02Z" for ex.
NSDate *eventdate = [df dateFromString:[eventDict objectForKey:#"begin_at"]];
NSTimeInterval nowSinceEventDate = [eventdate timeIntervalSince1970];
NSLog(#"nowSinceEventDate: %f", nowSinceEventDate);
double eventDateInMilliSecs = nowSinceEventDate * 1000;
NSLog(#"eventDateInMilliSecs: %f", eventDateInMilliSecs);
// this is not working as expected
if ( eventDateInMilliSecs<sixtyDaysMilliSecsFromCurrDate && eventDateInMilliSecs>currDateInMilliSecs )
{
}
else
{
}
Any help please?
try this
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss SSS"];
Try this:
NSString *dateString = #"2012-04-14T16:50:02Z";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *eventDate = [formatter dateFromString:dateString];
NSTimeInterval nowSinceEventDate = [eventDate timeIntervalSince1970];
NSLog(#"interval = %f", nowSinceEventDate);
UPDATE:
NSString *dateString = #"2012-05-21T16:50:02Z";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *eventDate = [formatter dateFromString:dateString];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:60];
NSDate *minDate = [NSDate date];
NSDate *maxDate = [gregorian dateByAddingComponents:components toDate:minDate options:0];
NSLog(#"eventDate interval = %f", [eventDate timeIntervalSince1970]);
NSLog(#"minDate interval = %f", [minDate timeIntervalSince1970]);
NSLog(#"maxDate interval = %f", [maxDate timeIntervalSince1970]);
BOOL isBetween = (([eventDate compare:minDate] == NSOrderedDescending) && ([eventDate compare:maxDate] == NSOrderedAscending));
NSLog(#"isBetween = %d", isBetween);
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss:SSS"];
// [eventDict objectForKey:#"begin_at"] gives "2012-04-14T16:50:02Z"
NSDate *eventdate = [df dateFromString:[eventDict objectForKey:#"begin_at"]];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
SSS for milli seconds

Splitting time to 10 mins interval in iphone

i want to split the time in the interval of 30mins, so i can get time like this:
12:00 AM 12:10 AM 12:20 AM .......... till 11:50 PM.
i'm trying something like this :
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"hh:mm a"];
NSDate* fromTime = [timeFormat dateFromString:startTime];
NSDate* toTime = [timeFormat dateFromString:endTime];
NSLog(#"Start time %#",fromTime);
NSLog(#"End time %#",toTime);
NSDate *dateByAddingThirtyMinute;
dateByAddingThirtyMinute = [fromTime dateByAddingTimeInterval:1800];
NSString *formattedDateString;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
formattedDateString = [dateFormatter stringFromDate:dateByAddingThirtyMinute];
NSLog(#"Time after 10 min %#",formattedDateString);
but i am able to print the first ten minute splitting ...
can any one help me how to loop it ...
Both of the other answers are wrong, because they do not account for daylight savings time. To do that, you have to use NSDateComponents and NSCalendar:
NSDate *startDate = ...;
NSDate *endDate = ...;
NSDateComponents *diff = [[NSDateComponents alloc] init];
[diff setMinute:0];
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *tmp = startDate;
NSMutableArray *dates = [NSMutableArray arrayWithObject:tmp];
while ([tmp laterDate:endDate] == endDate) {
[diff setMinute:[diff minute] + 10];
tmp = [cal dateByAddingComponents:diff toDate:startDate options:0];
[dates addObject:tmp];
}
In this code, I'm not actually running the NSDate objects through the NSDateFormatter, because that should happen at a different level. This code is all about the underlying data (the Model), and NSDateFormatter usually operates at the user-visible level (the View). Plus, it's generally more useful to have the raw data object than the formatted string.
Try with below code
NSString *startTime = #"12:00 AM";
NSString *endTime = #"11:40 AM";
NSDateFormatter *timeFormat = [[[NSDateFormatter alloc] init] autorelease];
[timeFormat setDateFormat:#"hh:mm a"];
NSDate* fromTime = [timeFormat dateFromString:startTime];
NSDate* toTime = [timeFormat dateFromString:endTime];
NSDate *dateByAddingThirtyMinute ;
NSTimeInterval timeinterval = [toTime timeIntervalSinceDate:fromTime];
NSLog(#"time Int %f",timeinterval/3600);
float numberOfIntervals = timeinterval/3600;
NSLog(#"Start time %f",numberOfIntervals);
for(int iCount = 0;iCount<numberOfIntervals*6 ;iCount ++)
{
dateByAddingThirtyMinute = [fromTime dateByAddingTimeInterval:600];
fromTime = dateByAddingThirtyMinute;
NSString *formattedDateString;
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm a"];
formattedDateString = [dateFormatter stringFromDate:dateByAddingThirtyMinute];
NSLog(#"Time after 10 min %#",formattedDateString);
}
Try this out
int numberOfIntervals = [toDate timeIntervalSinceDate:fromDate]/(10*60);
for (int i = 0; i < numberOfIntervals; i++) {
NSDate *theDate = [date dateByAddingTimeInterval:i * 60 * 10];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
NSString *formattedDateString = [dateFormatter stringFromDate:dateByAddingThirtyMinute];
NSLog(#"Time after 10 min %#",formattedDateString);
[dateFormatter release];
}

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

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

How do I display timezone as text?

I want to be able to display the system timezone as separate plain text in my app when it launches:
- (void)viewDidLoad {
[super viewDidLoad];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSString* date = [dateFormatter stringFromDate:[NSDate date]];
lblDate.text = date;
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString* zone = [gmt:[NSTimeZone defaultTimeZone]];
txtTZ.text = zone;`
My app can display the date correctly but crashes upon the timezone. Any suggestions?
Try replacing this:
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString* zone = [gmt:[NSTimeZone defaultTimeZone]];
txtTZ.text = zone;
With this:
NSString *zone = [[NSTimeZone systemTimeZone] abbreviation];
txtTZ.text = zone;