I am using the following code to calculate time difference
NSString *strTemp = [[NSString alloc] initWithString:[dicTemp objectForKey:@"created_at"]];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
NSDateFormatter *dtFormatter = [[NSDateFormatter alloc] init];
dtFormatter.dateFormat = [NSString stringWithFormat:DATEFORMAT_TYPE];
[dtFormatter setLocale:locale];
[dtFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSDate *dt = [dtFormatter dateFromString:strTemp];
NSDateFormatter *todayFormatter = [[NSDateFormatter alloc] init];
[todayFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
todayFormatter.dateFormat = DATEFORMAT_TYPE;
[todayFormatter setLocale:locale];
NSString *strToday = [todayFormatter stringFromDate:[NSDate date]];
NSDate *today = [todayFormatter dateFromString:strToday];
NSCalendar *c = [NSCalendar currentCalendar];
NSDateComponents *components = [c components:NSCalendarUnitHour fromDate:dt toDate:today options:0];
NSDateComponents *componentsMinute = [c components:NSCalendarUnitMinute fromDate:dt toDate:today options:0];
NSInteger diffHours = components.hour;
NSInteger diffMinutes = componentsMinute.minute;
While sending date to server I send it in the following way:
NSDate *date = [NSDate date];
NSDateFormatter *dateFormate = [[NSDateFormatter alloc] init];
[dateFormate setDateFormat:DATEFORMAT_TYPE];
[dateFormate setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];
[dateFormate setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
NSString *strCurrentDate = [dateFormate stringFromDate:date];
I get the same date from server which I send, while converting nsdate to get current date to calculate hours, I always start getting time in negative i.e. the hour difference in negative, I have noted that it happens after 5:30 PM in India, before that it is proper
Kindly help.
Aucun commentaire:
Enregistrer un commentaire