nsdate

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

iOS Daily Local Push Notifications

梦想与她 提交于 2019-11-29 07:33:48
I want to execute a UILocalNotification at 9:00 AM every day, forever, as long as the app is open. The closest thing I've found is this: UILocalNotification *notification = [[UILocalNotification alloc] init]; notification.fireDate = [[NSDate date] dateByAddingTimeInterval:60*60*24]; notification.alertBody = @"It's been 24 hours."; [[UIApplication sharedApplication] scheduleLocalNotification:notification]; However, this code only executes a UILocalNotification once in 24 hours, not at a specified time. I've been looking into utilizing NSDate somehow, but have been getting no where. The code

Get the Week Number in iOS SDK

南笙酒味 提交于 2019-11-29 07:18:18
I want to get the Week number of the year from the date.I tried the code as follow but gives me a wrong week number. My code for week number: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY-MM-dd"]; NSDate *date = [dateFormatter dateFromString:@"2012-09-15"]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSLog(@"week: %i", [[calendar components: NSWeekOfYearCalendarUnit fromDate:date] weekOfYear]); //Display 38 instead of 37 } Note: If i try with with [NSDate date] display the correct. Help me to solve this.. Thank you, Misha Vyrko

NSDate independent of timezone?

跟風遠走 提交于 2019-11-29 06:52:56
I have an app that displays a timetable of certain ferry trips. If I travel to a different timezone - say 4 hours behind, a 10am ferry trip now shows up as 6am? I know this has got to do with how dates are treated based on their timezones, but I can't work out how to change that behaviour. At the moment here's how I am getting the date and displaying it on a UILabel: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"HH:mm"]; [self.departureTime setText:[dateFormatter stringFromDate:[self.route objectForKey:@"departureTime"]]]; [self.arrivalTime

NSDateFormatter with default time zone gives different date than NSDate description method

我的梦境 提交于 2019-11-29 06:37:36
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp]; NSDateFormatter *_dateFormatter = [[NSDateFormatter alloc] init]; [_dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]]; [_dateFormatter setDateFormat:@"dd-MM-yy HH:mm"]; NSString *dateString = [_dateFormatter stringFromDate:date]; NSLog(@"Date: %@ formatted: %@", date, dateString); [_dateFormatter release]; Gives me wrong date formatted: Date: 2013-01-31 18:00:00 +0000 formatted: 31-01-13 19:00 (from timestamp: 1359655200.000000) Online conversion tool: http://www.onlineconversion.com/unix_time.htm indicates that 1st date is

Subtracting two NSDate objects [duplicate]

☆樱花仙子☆ 提交于 2019-11-29 05:31:49
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to Get time difference in iPhone I´m getting date and time from a JSON feed. I need to find the difference between the date I´m getting from the feed and today´s date and time. Any suggestions how I can do this? I know I need to subtract the current date with the date I get from the feed, but I don´t know how to do it. Ex: Date from feed: Date: 2011-06-10 15:00:00 +0000 Today: Date: 2011-06-10 14:50:00 +0000

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];

Sorting NSMutableArray By Object's Property

陌路散爱 提交于 2019-11-29 05:24:26
So this is a rather basic question regarding the best way to sort an NSMutableArray of custom objects. I have a an NSMutableArray of custom objects, each object with an NSString and NSDate that go together. I need to sort the array by the newest object (so latest NSDate ), and I'm pretty sure I could simply use NSDate compare: NSDate if this was an array of just NSDate , but since I need all objects to be sorted and not just the date, I'm not sure if I can use that method. In terms of pseudo-code, I need to: Look at individual object, determine if the current object's NSDate is the next

NSDate expressed in different time zones, i.e. local time zone (GMT-400) to PST

痴心易碎 提交于 2019-11-29 05:13:31
I know how to use NSTimeZone to derive the offset for the current time in another time zone. NSDate always returns relative to GMT, so how do I derive a string with the proper time zone information? i.e. I take the current time where I am (in EST) and using NSTimeZone end up subtracting the 3 hours necessary to represent the time in PST. But all I've done is subtract 3 hours from the time which is still represented relative to my time zone. How do I get NSDateFormatter to spit out the time using the destination time zone? One tack I tried was: NSCalendar *cal = [NSCalendar currentCalendar];

How to sort an array of dates in descending order

早过忘川 提交于 2019-11-29 03:25:06
I have a NSDictionary that parsed to an array one of the element is date, i tried using [startimeArray sortUsingSelector:@selector(compare:)]; (starttimeArray) is my date but it only arrange ascending. how can i sort in descending order. thanks Sort the array by puting NO is ascending parameter: NSSortDescriptor *descriptor=[[NSSortDescriptor alloc] initWithKey:@"self" ascending:NO]; NSArray *descriptors=[NSArray arrayWithObject: descriptor]; NSArray *reverseOrder=[dateArray sortedArrayUsingDescriptors:descriptors]; vikingosegundo You can use a comparator block NSArray *sortedArray = [array