nsdatecomponents

Objective-C/iOS: Get date for specific day in week (sunday)

时间秒杀一切 提交于 2019-12-10 19:48:34
问题 I need to get the date of a specific day (Sunday) in the current week. I use this date to determine if some weekly resetting should occur, and I do some calculations on last reset date is before sundays date etc. This has in principal been ask and answered many times, and my solution has worked for the past 6 months. But today, the 29th of December 2013, it stopped working. The function used at present time: + (NSDate *) getSundaysDate{ NSDate *currentDate = [NSDate date]; NSCalendar

NSCalendar dateFromComponents returns wrong date by 2

老子叫甜甜 提交于 2019-12-10 17:17:45
问题 I want to find out the date of the first week of a mouth in a year: NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setYear:2013]; [components setMonth:1]; [components setWeekOfMonth:1]; [components setWeekday:1]; NSDate *newDate = [calendar dateFromComponents:components]; NSLog(@"%@",newDate); What I get is: 2012-12-29 23:00:00 +0000 And when I compare to my mac calendar what I need to get is: 2012-12-31 23:00

IOS: problem with NSDate

久未见 提交于 2019-12-10 15:28:40
问题 I have this code: NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setYear:2011]; [components setDay:1]; [components setMonth:4]; NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; NSDate *firstDate = [gregorianCalendar dateFromComponents:components]; NSLog(@"date:%@", firstDate); the result in console is: date:2011-03-31 why???? if I set "setDay:1" it would be "date:2011-04-01", no? 回答1:

NSDate behaviour when saving to plist

柔情痞子 提交于 2019-12-10 14:26:15
问题 I am creating a date from [NSdate date] and saving it to plist, Here is how I am creating a date - (void)applicationWillResignActive:(UIApplication *)application { NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSMinuteCalendarUnit)fromDate:[NSDate date]

Display NSDates in terms of week

风格不统一 提交于 2019-12-10 11:48:08
问题 Scenario: I have an expense tracking iOS Application and I am storing expenses from a expense detail view controller into a table view (with fetched results controller) that shows the list of expenses along with the category and amount and date. I do have a date attribute in my entity "Money" which is a parent entity for either an expense or an income. Question: What I want is to basically categorize my expenses for a given week, a month, or year and display it as the section header title for

How to manage NSDate for different timezones

孤街醉人 提交于 2019-12-09 22:17:11
问题 I have created a calendar in my app, using the date object this way: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSMinuteCalendarUnit)fromDate:[NSDate date]]; NSInteger day = [weekdayComponents day]; NSInteger month = [weekdayComponents month]; NSInteger year = [weekdayComponents year]; m_dateFormatter.dateFormat = @"dd/MM

finding NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?

百般思念 提交于 2019-12-07 19:52:53
问题 Is there a quick way in Objective-C of identifying NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)? I can't quite see anyway other than manually walking through each NSDate in the array and then using NSDateComponents to break out the hour/minute/second...Not even sure if there is a simple way to get the time from an NSDate in a fashion that represents a fraction of 24hours, as this might help a little. (e.g. 6pm would be 18/24 = 0.75 in this case) 回答1: There is

Weekday of first day of month

拈花ヽ惹草 提交于 2019-12-07 06:58:11
问题 I need to get the weekday of the first day of the month. For example, for the current month September 2013 the first day falls on Sunday. 回答1: At first, get the first day of current month (for example): NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [gregorian components:(NSEraCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit) fromDate:today]; components.day = 1; NSDate

finding NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)?

强颜欢笑 提交于 2019-12-06 13:29:55
Is there a quick way in Objective-C of identifying NSDate's in an NSArray that have a time of day after a given time (e.g. 8pm)? I can't quite see anyway other than manually walking through each NSDate in the array and then using NSDateComponents to break out the hour/minute/second...Not even sure if there is a simple way to get the time from an NSDate in a fashion that represents a fraction of 24hours, as this might help a little. (e.g. 6pm would be 18/24 = 0.75 in this case) There is no need to break in NSDateComponents . NSTimeInterval interval = [date1 timeIntervalSinceDate:date2]; if

Determine NSDate for Memorial Day in a given year

♀尐吖头ヾ 提交于 2019-12-06 07:35:05
Is there a better way to determine the NSDate for Memorial Day (the last Monday in May) in a given year? NSInteger aGivenYear = 2013 ; NSCalendar* calendar = [NSCalendar currentCalendar] ; NSDateComponents* firstMondayInJuneComponents = [NSDateComponents new] ; firstMondayInJuneComponents.month = 6 ; // Thanks, Martin R., for pointing out that `weekOfMonth` is wrong for returning the first Monday in June. firstMondayInJuneComponents.weekOfMonth = 1 ; firstMondayInJuneComponents.weekday = 2 ; //Monday firstMondayInJuneComponents.year = aGivenYear ; NSDate* firstMondayInJune = [calendar