nsdatecomponents

iPhone - get number of days between two dates

自古美人都是妖i 提交于 2019-11-29 07:38:27
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:@"yyyy-MM-dd-HH-mm"]; NSDate *nowDate = [dateFormatter dateFromString:@"2010-01-01-15-00"]; NSDate

NSDateComponents of an NSDate

只愿长相守 提交于 2019-11-29 05:28:20
How do I get the NSDateComponents of a single NSDate? I don't want the components of the difference between 2 dates, just the seconds, minutes, hours, day, month and year of a NSDate? There's an example in the Apple docs , Listing 3: Getting a date’s components : NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today]; NSInteger day = [weekdayComponents day]; NSInteger weekday = [weekdayComponents weekday];

How to set repeat frequency in User Notification [duplicate]

你离开我真会死。 提交于 2019-11-28 21:39:43
This question already has an answer here: How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications? 3 answers Till iOS 9 we write local notifications like this UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = pickerDate; localNotification.alertBody = self.textField.text; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.repeatInterval = NSCalendarUnitMinute; localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1; [[UIApplication

Why NSCalendar's dateFromComponents returns wrong date?

别说谁变了你拦得住时间么 提交于 2019-11-28 13:46:13
I am expecting 2010-10-11 00:00:00 But instead I receive 2010-10-10 21:00:00 +0000 I know, that I can add a few hours and receive desired date, but I do not understand why I get previous day with 21 hours... Please, help! NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:2010]; [comps setMonth:10]; [comps setDay:11]; [comps setHour:0]; [comps setMinute:0]; [comps setSecond:0]; NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *retDate = [cal dateFromComponents:comps]; KingofBliss I think your problem may be solved by setting

Wrong date comes back from NSDateComponents

倖福魔咒の 提交于 2019-11-28 12:17:01
问题 Why is this giving me the wrong date ? NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; NSDateComponents* components = [myCalendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:[NSDate date]]; [components setYear:2013]; [components setMonth:06]; [components setDay:28]; [components setHour:5]; [components setMinute:00]; NSDate *startDate1 = [myCalendar dateFromComponents:components]; NSLog(@"start date is %@"

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

自闭症网瘾萝莉.ら 提交于 2019-11-28 09:57:56
I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009) Thank you. 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 the date format string in the setDateFormat: call Lots more information at: http://developer.apple.com

Compare two NSDates for same date/time [duplicate]

别等时光非礼了梦想. 提交于 2019-11-28 08:25:29
This question already has an answer here: How to compare two dates in Objective-C 14 answers Is date1 == date2 not a valid way to compare? If not, what is the correct alternative? Here's my code: - (NSDate*) dateWithNoTime { unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:flags fromDate:self]; NSDate* dateOnly = [calendar dateFromComponents:components]; return dateOnly; } - (BOOL) sameDayAsDate:(NSDate*)dateToCompare { NSDate *date1 = [self dateWithNoTime]

how do I set an existing NSDate's time?

限于喜欢 提交于 2019-11-28 06:50:29
how do I set an existing NSDate 's time? That is I have a date (say current date/time), but then want to set this to a specific time (e.g. 11.23am) say. What is the quickest way to do this in objective C? I'm looking at NSCalendar and see methods such as dateByAddingComponents:toDate:options: , and dateFromComponents: , but these don't seem to quite hit the mark for what I need here. For example: dateFromComponents: - I would have to work out all components for this to work which seems like overkill dateByAddingComponents:toDate:options: - I don't want to ADD in my case but rather "set" the

NSDateComponents weekOfYear returns wrong date

蓝咒 提交于 2019-11-28 06:22:41
问题 so i am making a basic week picker, where you can pick a week number and year, and get the corresponding date from the weeks startpoint. A basic test scenario let calendar = NSCalendar.currentCalendar() let components = NSDateComponents() components.year = 2015 components.weekOfYear = 15 print(calendar.dateFromComponents(components)!) You can see, that i set the wanted year to 2015 and the wanted week to 15, but the result i get is: "2014-12-31 23:00:00 +0000" ... and thats really gets me

Swift 3.0 : Convert server UTC time to local time and vice-versa

帅比萌擦擦* 提交于 2019-11-28 03:59:15
I want to convert server UTC time to local time and vice-versa. Here is my code.. var isTimeFromServer = true var time:String! var period:String! let timeString = "6:59 AM" //Current UTC time if isTimeFromServer { let index = timeString.index(timeString.startIndex, offsetBy: 5) let twelve = timeString.substring(to: index) var dateString:String! let dateFormatter = DateFormatter() dateFormatter.dateFormat = "H:mm" let date12 = dateFormatter.date(from: twelve)! dateFormatter.dateFormat = "h:mm a" let date22 = dateFormatter.string(from: date12) //print(date22) dateString = date22 //print(