nsdatecomponents

sethours in NSDateComponents

巧了我就是萌 提交于 2019-12-22 11:27:58
问题 I'm trying to sethours in NSDateComponents, I wrote the following code NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *dateComps = [[NSDateComponents alloc] init]; [dateComps setDay:intDay]; [dateComps setMonth:intMonth]; [dateComps setYear:intYear]; [dateComps setHour:intHoures]; [dateComps setMinute:intMinutes]; NSDate *itemDate; itemDate = [calendar dateFromComponents:dateComps]; NSLog(@"reminder date: %@", itemDate); but when I

NSDateComponents weekday doesn't show correct weekday?

陌路散爱 提交于 2019-12-22 10:29:11
问题 I got a NSDate for example 1/6 -12 (Friday) and trying to find out what weekday it is. My week starts at Monday so Friday should be weekday 5. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setFirstWeekday:2]; NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:firstOfJulyDate]; NSLog(@"weekday %i",components.weekday); Output: weekday 6 Doesn't matter what i set setFirstWeekday to, output will always be 6.

How do I subtract a duration from an NSDate, but not include the weekends?

╄→гoц情女王★ 提交于 2019-12-22 05:15:09
问题 Using today as an example, how do I determine which date it was, 230 workdays ago? I know how to do it iteratively with a while loop checking date and subtracting 1 if it's a workday, but I'm wondering if there is a better method. Also, let's take a Sunday 1 PM as an example, and subtract 3 work days and 2 hours from that time. First, it doesn't make sense to subtract work-time from weekends. So it would have to move the time to 23:59:59 of Friday, and then subtract those 3 days and 2 hours.

How to get dates of current month in Objective c?

浪尽此生 提交于 2019-12-21 20:19:38
问题 I am developing a app which uses the date, month, year. I want the current month dates,because current month can have 28,29,30,31 days. I have tried to get current year months. but i don't know the code for above. here is my code. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSDate *today = [NSDate date]; NSCalendar *currentCalendar = [NSCalendar currentCalendar]; NSDateComponents *yearComponents = [currentCalendar components:NSYearCalendarUnit fromDate:today]; int

Working with NSDate components in Swift

眉间皱痕 提交于 2019-12-20 19:08:12
问题 I have a date that I need to split in some components. for example let components = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil) var dateToPrint = "\(date.day) days \(date.hour) hours" dateToPrint will be the number of days and hours from aDate to now. But if i want the number of weeks instead of days let components = NSCalendarUnit.CalendarUnitWeek | NSCalendarUnit.CalendarUnitHour

Where is the extra 75 seconds coming from?

不羁岁月 提交于 2019-12-20 10:58:32
问题 While writing some unit tests on a Julian Day calculator, I found that dates prior to 2nd December 1847 were being initialised incorrectly by NSDate. They appear to have 75 seconds added on. I haven't been able to find anything pointing to that date (which is well after the Gregorian calendar cutoff). Is it a bug or is there a historic calendar adjustment that I've not come across? int main(int argc, const char * argv[]) { @autoreleasepool { NSCalendar *cal = [NSCalendar currentCalendar];

Creating NSDate from NSDateComponents off by one day

為{幸葍}努か 提交于 2019-12-20 05:23:34
问题 I'm trying to set a particular date for a unit test: NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setCalendar:calendar]; [components setYear:2011]; [components setMonth:5]; [components setDay:12]; [components setHour:1]; [components setMinute:0]; [components setSecond:0]; NSDate *date = [calendar dateFromComponents:components]; NSLog(@"Date I tried to set: %

Get first day in a month from NSCalendar

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 06:05:25
问题 I have this subset of a method that needs to get day one of the current month. NSDate *today = [NSDate date]; // returns correctly 28 february 2013 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; components.day = 1; NSDate *dayOneInCurrentMonth = [gregorian dateByAddingComponents:components toDate:today options:0]; dayOneInCurrentMonth then prints out 2013-03-01 09:53:49 +0000 , the

iPhone - get number of days between two dates

自作多情 提交于 2019-12-18 05:03:34
问题 I'm writing a GTD app for the iPhone. For the due tasks, I want to display something like "Due tomorrow" or "Due yesterday" or "Due July 18th". Obviously, I need to display "Tomorrow" even if the task is less than 24 hours away (e.g. the user checks at 11pm on Saturday and sees there's a task on Sunday at 8am). So, I wrote a method to get the number of days in between two dates. Here's the code... NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@

How to find what day of the week for any given date using Cocoa

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 19:19:15
问题 I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009) Thank you. 回答1: I've been doing: NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4]; [theDateFormatter setDateFormat:@"EEEE"]; NSString *weekDay = [theDateFormatter stringFromDate:[NSDate date]]; This has the added bonus of letting you choose how you'd like the weekday to be returned (by changing