nsdate

Simplest way to loop between two NSDates on iPhone?

ⅰ亾dé卋堺 提交于 2019-11-27 03:41:47
问题 What's the simplest way to loop from one date to another? What I want conceptually is something like this: for (NSDate *date = [[startDate copy] autorelease]; [date compare: endDate] < 0; date = [date dateByAddingDays: 1]) { // do stuff here } This doesn't work, of course: there's no dateByAddingDays: . And even if it did, it would leave a wide trail of autoreleased objects waiting for their destruction. Here's what I've thought of: I can't just add an NSTimeInterval , since the number of

Current Week Start and End Date

断了今生、忘了曾经 提交于 2019-11-27 03:38:58
I want to get the current week start and end date and I also want to use the previous week start and end date and next week of the start and end date in current month. Thanks in Advance. rangeOfUnit:startDate:interval:forDate: . It gives you the start and the interval for a certain time unit. With it it is easy to find the start of the week in the used calendar and add the range-1 to get the latest second in that week. NSCalendar *cal = [NSCalendar currentCalendar]; NSDate *now = [NSDate date]; NSDate *startOfTheWeek; NSDate *endOfWeek; NSTimeInterval interval; [cal rangeOfUnit

Minimum and maximum date in UIDatePicker

笑着哭i 提交于 2019-11-27 03:38:12
I want to get the minimum and maximum date from a date picker, but minimum date should be "- 18" of the current date and the maximum date should be "- 100" of current date. Suppose current year is 2018 then I want minimum date 2000 and maximum date 1918. What I have done so far is : NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorian components:NSYearCalendarUnit fromDate:[NSDate date]]; NSInteger year = [components year]; int mindt = year - 18; int maxdt = year -100; // NSDate * MinDate = [components year] - 18

How can I set an NSDate object to midnight?

霸气de小男生 提交于 2019-11-27 03:37:26
I have an NSDate object and I want to set it to an arbitrary time (say, midnight) so that I can use the timeIntervalSince1970 function to retrieve data consistently without worrying about the time when the object is created. I've tried using an NSCalendar and modifying its components by using some Objective-C methods, like this: let date: NSDate = NSDate() let cal: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)! let components: NSDateComponents = cal.components(NSCalendarUnit./* a unit of time */CalendarUnit, fromDate: date) let newDate: NSDate = cal.dateFromComponents

NSDate set timezone in swift

风格不统一 提交于 2019-11-27 03:29:12
问题 how can i return a NSDate in a predefined time zone from a string let responseString = "2015-8-17 GMT+05:30" var dFormatter = NSDateFormatter() dFormatter.dateFormat = "yyyy-M-dd ZZZZ" var serverTime = dFormatter.dateFromString(responseString) println("NSDate : \(serverTime!)") the above code returns the time as 2015-08-16 18:30:00 +0000 回答1: how can i return a NSDate in a predefined time zone? You can't. An instance of NSDate does not carry any information about timezone or calendar. It just

NSDate beginning of day and end of day

安稳与你 提交于 2019-11-27 03:24:55
-(NSDate *)beginningOfDay:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date]; [components setHour:0]; [components setMinute:0]; [components setSecond:0]; return [cal dateFromComponents:components]; } -(NSDate *)endOfDay:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit |

How to compare two NSDate objects in objective C

不羁的心 提交于 2019-11-27 03:24:13
I have objects of Date type. I want to compare them, and I have written an if condition in the following way: if (([startDate1 isEqualToDate:[self getDefaultDate]]) || (startDate1 != [ self getDefaultDate] && m_selectedDate >= m_currentDate1 && cycleStopped)) { ///execute some condition } Am I right or wrong in this approach? One more thing. Is this way right: if (dtdate > [m_array objectatIndex:i] { } Because I am getting random behavior. Here's an example using the NSDate method, compare: if([startDate compare: endDate] == NSOrderedDescending) // if start is later in time than end { // do

Fuzzy date algorithm

风格不统一 提交于 2019-11-27 03:13:24
I'm looking for a fuzzy date algorithm. I just started writing one and realised what a tedious task it is. It quickly degenerated into a lot of horrid code to cope with special cases like the difference between "yesterday", "last week" and "late last month" all of which can (in some cases) refer to the same day but are individually correct based on today's date. I feel sure there must be an open source fuzzy date formatter but I can't find it. Ideally I'd like something using NSDate (OSX/iPhone) and its formatters but that isn't the difficult bit. Does anyone know of a fuzzy date formatter

ios- Check whether current time is between two times or not

有些话、适合烂在心里 提交于 2019-11-27 03:06:16
问题 I want to check whether the current time is between two time or not. For example, I want to check if the current time is between today morning 10AM to 12PM or Tomorrow morning 10AM to 12PM. How can I do this? 回答1: Try this NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]]; NSInteger currentHour = [components hour]; NSInteger currentMinute = [components minute]; NSInteger

How does one subtract hours from an NSDate?

蹲街弑〆低调 提交于 2019-11-27 03:03:22
问题 I would like to subtract 4 hours from a date. I read the date string into an NSDate object use the following code: NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"]; NSDate * mydate = [dateFormatter dateFromString:[dict objectForKey:@"published"]]; What do I do next? 回答1: NSDate *newDate = [theDate dateByAddingTimeInterval:-3600*4]; Link to documentation. NSDate *newDate = [[[NSDate alloc]