nsdate

NSDate and NSDateComponents return wrong date

随声附和 提交于 2019-11-27 08:37:29
问题 I know that this question was asked a million times, I just can't find a solution. The problem is: I'm modifying an NSDate to return either the date of the first day in the current month or the date of the first day in the next month. It seems to be working fine except for 1 thing: it gives back the time minus 2 or 3 hours. Code: NSCalendar *theCalendar = [NSCalendar currentCalendar]; int comp = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSDateComponents *theComp =

Removing time components from an NSDate object using Objective C/Cocoa

孤街醉人 提交于 2019-11-27 08:17:06
I'm trying to write a simple function using Objective C that accepts an NSDate object and returns an NSDate object that contains the same date value but has removed any time components from the date. For example if I were to pass an NSDate with a value of '2010-10-12 09:29:34' the function would return 2010-10-12 00:00:00. The I'm using function seems to work properly. However, when I test the iPhone app I'm developing with Instruments it reports there is a memory leak in the function I'm using. Take a look at my function below. Where is there a memory leak? Is there a better way to achieve

Sorting NSArray containing NSDate objects

旧街凉风 提交于 2019-11-27 08:00:42
问题 I have an array containing NSDate objects. With Objective-C, I am able to sort this array by using: NSArray *array = [unsortedArray sortedArrayUsingSelector:@selector(compare:)] I'm wondering if there's a Swift equivalent of doing this same thing. Relatively new to Swift here, hacking my way through. 回答1: Using the native Array type in Swift. If you are interfacing with legacy code from ObjC, use this: let array = unsortedArray.sortedArrayUsingSelector("compare:") 回答2: If you use Swift array.

Find the NSDate for next Monday [closed]

时光怂恿深爱的人放手 提交于 2019-11-27 07:46:07
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . I want to get the date of the next Monday after the current date. So if today's date is 2013-08-09 (Friday) then I want to get the date 2013-08-12. How can I do this? 回答1: This piece of code should get what you want. It simply calculates how many days are from monday and append it from current's date. NSDate

Why NSDateFormatter can not parse date from ISO 8601 format [duplicate]

有些话、适合烂在心里 提交于 2019-11-27 07:40:09
Possible Duplicate: Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset? I use rails as backend, the default date output is 2008-12-29T00:27:42-08:00 But after my research NSDateFormatter can not support it, except I change date out to 2008-12-29T00:27:42-0800 Here is the code I used to parse ISO 8601 date, but it's not work NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; NSLog(@"%@", [dateFormatter dateFromString:@"2008-12-29T00:27:42-08:00"]); Any ideas? The problem

Check whether time falls between two time iOS

我与影子孤独终老i 提交于 2019-11-27 07:37:42
问题 I have two times let say 8.40am and 4.00pm, What i want to do is, want to check whether current time falls between given time or not ? I have tried this code snippet but it is not working :( can you please help me out where i am making mistake ? NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]]; NSInteger currHr = [components hour]; NSInteger currtMin = [components minute]; NSString

Swift converting NSDate to 24h Format fails on Device

孤者浪人 提交于 2019-11-27 07:30:48
问题 I'm facing an annoying error when i run my application on my iPhone6. I need to convert an hour (picked by a datePicker) from 12h to 24h format. Simple thing! Right, I'm pretty sure about that but... my code works on simulator but not on device. The code is the following: func convertTimeTo24HoursFomratString() -> String { let dateFormatter = NSDateFormatter() // isFormatTime24Hours is a static function that returns me if the device has a 24h clock or not. It works fine both on device and on

Dateformatter gives wrong time on conversation [duplicate]

只谈情不闲聊 提交于 2019-11-27 07:30:29
问题 This question already has an answer here: NSDate Format outputting wrong date 5 answers Getting date from [NSDate date] off by a few hours 3 answers I am trying to convert my date string to NSDate but its return correct date and wrong time. This is my code : NSString *dateStr = @"2013-12-20 12:10:40"; // Convert string to date object NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat: @"yyyy-MM-dd HH:mm:ss"]; NSDate *lastUpdatedate = [dateFormatter

Getting Null from NSDateFormatter

北城余情 提交于 2019-11-27 07:23:52
问题 I am trying to convert a String into NSDate, but I am getting Null. Sample String Input is = 8/8/2013 12:32:22 PM Code: [self.dateFormat setDateFormat:@"MM/dd/yyyy hh:mm:ss a"]; NSLog(@"Date %@", [[communications objectAtIndex:x] objectForKey:@"dateUpdated"]); NSDate *date = [[NSDate alloc] init]; date = [self.dateFormat dateFromString:[[communications objectAtIndex:x] objectForKey:@"dateUpdated"]]; NSLog(@"Date from NSDate %@", [self.dateFormat stringFromDate:date]); What am I doing wrong?

Core Data Predicate Date Comparison

感情迁移 提交于 2019-11-27 07:08:19
Im trying to fetch all the objects in an entity matching a user selectedDate (it's an NSDate). The Core Data code is fine but my predicate keeps returning 0 results, the dates are the same in the database as the user is selecting. How should the selectedDate be compared with a date from an entity using a predicate? NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(eDate = %@)", selectedDate]; Your predicate looks to be fine. The reason you're finding zero result are returned however may be that the dates aren't entirely the same. For example: 05/04/2012 13:37:00 will not match with