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

Sort NSArray of custom objects by their NSDate properties

Are you sure that the startDateTime instance variables of the node events are non-nil? If you don’t have one already, you might add a (custom) -description method to your node event objects that does something like this: – (NSString *)description { return [NSString stringWithFormat:@”%@ – %@”, [super description], startDateTime]]; } Then in your sorting code … Read more

Fuzzy date algorithm

There is a property in NSDateFormatter – “doesRelativeDateFormatting”. It appears only in 10.6/iOS4.0 and later but it will format a date into a relative date in the correct locale. From Apple’s Documentation: If a date formatter uses relative date formatting, where possible it replaces the date component of its output with a phrase—such as “today” … Read more

NSDate of yesterday

You should use NSCalendar for calculating dates. For example, in Swift 3 the date two days before today is: let calendar = Calendar.current let twoDaysAgo = calendar.date(byAdding: .day, value: -2, to: Date()) Or in Swift 2: let calendar = NSCalendar.currentCalendar() let twoDaysAgo = calendar.dateByAddingUnit(.Day, value: -2, toDate: NSDate(), options: []) Or to get the first … Read more

Minimum and maximum date in UIDatePicker

Try this: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *currentDate = [NSDate date]; NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:-18]; NSDate *minDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0]; [comps setYear:-150]; NSDate *maxDate = [gregorian dateByAddingComponents:comps toDate:currentDate options:0]; [comps release]; self.datePicker.minimumDate = minDate; self.datePicker.maximumDate = maxDate; It may be easily translated to Swift 4.2: let calendar … 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

tech