How do I get an ISO 8601 date on iOS?

Use NSDateFormatter: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@”en_US_POSIX”]; [dateFormatter setLocale:enUSPOSIXLocale]; [dateFormatter setDateFormat:@”yyyy-MM-dd’T’HH:mm:ssZZZZZ”]; [dateFormatter setCalendar:[NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian]]; NSDate *now = [NSDate date]; NSString *iso8601String = [dateFormatter stringFromDate:now]; And in Swift: let dateFormatter = DateFormatter() let enUSPosixLocale = Locale(identifier: “en_US_POSIX”) dateFormatter.locale = enUSPosixLocale dateFormatter.dateFormat = “yyyy-MM-dd’T’HH:mm:ssZZZZZ” dateFormatter.calendar = Calendar(identifier: .gregorian) let iso8601String = … Read more

NSDate beginning of day and end of day

Start Of Day / End Of Day — Swift 5.7 extension Date { var startOfDay: Date { return Calendar.current.startOfDay(for: self) } var endOfDay: Date { var components = DateComponents() components.day = 1 components.second = -1 return Calendar.current.date(byAdding: components, to: startOfDay)! } var startOfWeek: Date { Calendar.current.dateComponents([.calendar, .yearForWeekOfYear, .weekOfYear], from: self).date! } var endOfWeek: Date { var … Read more

NSPredicate: filtering objects by day of NSDate property

Given a NSDate * startDate and endDate and a NSManagedObjectContext * moc: NSPredicate *predicate = [NSPredicate predicateWithFormat:@”(date >= %@) AND (date <= %@)”, startDate, endDate]; NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease]; [request setEntity:[NSEntityDescription entityForName:@”EntityName” inManagedObjectContext:moc]]; [request setPredicate:predicate]; NSError *error = nil; NSArray *results = [moc executeFetchRequest:request error:&error];

Get current date in Swift 3? [closed]

You say in a comment you want to get “15.09.2016”. For this, use Date and DateFormatter: let date = Date() let formatter = DateFormatter() Give the format you want to the formatter: formatter.dateFormat = “dd.MM.yyyy” Get the result string: let result = formatter.string(from: date) Set your label: label.text = result Result: 15.09.2016

Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds

this is what i used: NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@”yyyy-MM-dd”]; NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; [timeFormat setDateFormat:@”HH:mm:ss”]; NSDate *now = [[NSDate alloc] init]; NSString *theDate = [dateFormat stringFromDate:now]; NSString *theTime = [timeFormat stringFromDate:now]; NSLog(@”\n” “theDate: |%@| \n” “theTime: |%@| \n” , theDate, theTime); [dateFormat release]; [timeFormat release]; [now release];

Subtract 7 days from current date

code: NSDate *currentDate = [NSDate date]; NSDateComponents *dateComponents = [[NSDateComponents alloc] init]; [dateComponents setDay:-7]; NSDate *sevenDaysAgo = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0]; NSLog(@”\ncurrentDate: %@\nseven days ago: %@”, currentDate, sevenDaysAgo); [dateComponents release]; output: currentDate: 2012-04-22 12:53:45 +0000 seven days ago: 2012-04-15 12:53:45 +0000 And I’m fully agree with JeremyP. BR. Eugene

iOS: Convert UTC NSDate to local Timezone

NSTimeInterval seconds; // assume this exists NSDate* ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds]; NSDateFormatter* df_utc = [[[NSDateFormatter alloc] init] autorelease]; [df_utc setTimeZone:[NSTimeZone timeZoneWithName:@”UTC”]]; [df_utc setDateFormat:@”yyyy.MM.dd G ‘at’ HH:mm:ss zzz”]; NSDateFormatter* df_local = [[[NSDateFormatter alloc] init] autorelease]; [df_local setTimeZone:[NSTimeZone timeZoneWithName:@”EST”]]; [df_local setDateFormat:@”yyyy.MM.dd G ‘at’ HH:mm:ss zzz”]; NSString* ts_utc_string = [df_utc stringFromDate:ts_utc]; NSString* ts_local_string = [df_local stringFromDate:ts_utc]; // you … Read more

NSDate Comparison using Swift

If you want to support ==, <, >, <=, or >= for NSDates, you just have to declare this somewhere: public func ==(lhs: NSDate, rhs: NSDate) -> Bool { return lhs === rhs || lhs.compare(rhs) == .OrderedSame } public func <(lhs: NSDate, rhs: NSDate) -> Bool { return lhs.compare(rhs) == .OrderedAscending } extension NSDate: Comparable … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)