Converting UTC date format to local nsdate

Something along the following worked for me in Objective-C : // create dateFormatter with UTC time format NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@”yyyy-MM-dd’T’HH:mm:ss”]; [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@”UTC”]]; NSDate *date = [dateFormatter dateFromString:@”2015-04-01T11:42:00″]; // create date from string // change to a readable time format and change to local time zone [dateFormatter setDateFormat:@”EEE, MMM d, … Read more

get current date from [NSDate date] but set the time to 10:00 am

As with all date manipulation you have to use NSDateComponents and NSCalendar NSDate *now = [NSDate date]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now]; [components setHour:10]; NSDate *today10am = [calendar dateFromComponents:components]; in iOS8 Apple introduced a convenience method that saves a few lines of code: NSDate *d = [calendar … Read more

How to compare two NSDate objects in objective C

Here’s an example using the NSDate method, compare: if([startDate compare: endDate] == NSOrderedDescending) // if start is later in time than end { // do something } From the class reference: If… The receiver and anotherDate are exactly equal to each other, NSOrderedSame. The receiver is later in time than anotherDate, NSOrderedDescending. The receiver is … Read more

Swift 3.0 : Convert server UTC time to local time and vice-versa

I don’t know what’s wrong with your code.But looks too much unnecessary things are there like you’re setting calendar, fetching some elements from string. Here is my small version of UTCToLocal and localToUTC function. But for that you need to pass string in specific format. Cause I’ve forcly unwrapped date objects. But you can use … Read more

How can I calculate the difference between two dates?

NSDate *date1 = [NSDate dateWithString:@”2010-01-01 00:00:00 +0000″]; NSDate *date2 = [NSDate dateWithString:@”2010-02-03 00:00:00 +0000″]; NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1]; int numberOfDays = secondsBetween / 86400; NSLog(@”There are %d days in between the two dates.”, numberOfDays); EDIT: Remember, NSDate objects represent exact moments of time, they do not have any associated time-zone information. When you convert … Read more

get NSDate today, yesterday, this Week, last Week, this Month, last Month… variables

Adapted from the Date and Time Programming Guide: // Right now, you can remove the seconds into the day if you want NSDate *today = [NSDate date]; // All intervals taken from Google NSDate *yesterday = [today dateByAddingTimeInterval: -86400.0]; NSDate *thisWeek = [today dateByAddingTimeInterval: -604800.0]; NSDate *lastWeek = [today dateByAddingTimeInterval: -1209600.0]; // To get the … Read more