nsdatecomponents

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

Hi, I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009) Thank you. ...

Getting first and last days of current week

How can I get first and last days of the current week? What I need is some way of filtering a set of objects with NSDate property to leave only ones, that appear on current week, and then filter them by days. Also I need some way to get localized day names. I've tried to play with NSCalendar, NSDate and NSDateComponents, but it seems th...

Convert NSString to Chunks of NSString that has a date format

This might be simple. I have searched, not found yet. How do I convert the NSString @"20100216190205" to an NSString of format @"yyyy-MM-dd HH:mm:ss". ie: @"2010-02-16 19:02:05" EDIT: I wanted to avoid using the NSDateFormatter to be more generic, for other situations. ...

How to create a specific date in the distant past, the BC era.

I’m trying to create a date in the BC era, but failing pretty hard. The following returns ‘4713’ as the year, instead of ‘-4712’: NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [NSDateComponents new]; [components setYear: -4712]; NSDate *date = [calendar...

Weird behavior when creating an NSDate of January the 1st and the 2nd

Hello, I'm creating a date like this : NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY"]; NSInteger year = [[dateFormatter stringFromDate:[NSDate date]] intValue]; NSLog(@"NSInteger YEAR : %...

iPhone - get number of days between two dates.

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 ge...

NSDateComponents returns a different Seconds value in iPhoneOS 3.1.3 compared to iOS4

I use this method to turn a time interval into a string of the form MM:SS NSTimeInterval testTime = 60.0; // for example NSCalendar *sysCalendar = [NSCalendar currentCalendar]; NSUInteger minuteAndSecondComponents = (NSMinuteCalendarUnit | NSSecondCalendarUnit); NSDate *intervalDate = [NSDate dateWithTimeIntervalSinceNow:testTime...

Calculating times based on generic days of the week using NSDate

I'm trying to figure out how to calculate store opening times based on the way they are normally expressed. For example: Monday - Friday, 9am - 6pm. I want to know two things from this criteria. Is the store open? If it is how long will it be before it closes? My confusion with the documentation is that NSDate calculations expect uniqu...

iPhone NSDateComponent wrong Date 1.1.2011 ?

I set a Date with 'DateFromComponents" and when I read it out... its wrong. I have to set the DAY to the 2nd or anything later so I get the right Year?!? I set year 2011 and when I read it out i get Year 2010 !! day = 1; month = 1; year = 2011; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalen...

NSDate / NSDateComponent problem

Hello, Let's say we are the 15/07/2010, I would like to get the first day of the previous week, which is the 5 of July. I have the following method but it does not work. Seems that I have a problem with the weekday attribute... + (NSDate *)getFirstDayOfPreviousWeek { // Get current date NSCalendar *cal = [[NSCalendar alloc] initWithCal...

How to create a custom date on iPhone

I'm trying to set some components of todays date with NSDateComponent like so: NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:1]; [comps setHour:1]; [comps setMinute:44]; NSCalendar *cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; NSDate *date = [...

Error when releasing NSdate from [NSDateComponents components: fromDate: toDate: options:]

Okay so i got this clas called event. It has a property called eventDate wich is a NSDate. I made a method to return how many years it is since the event date: - (double)yearsSinceEvent { double years; // Get the system calendar NSCalendar *sysCalendar = [NSCalendar currentCalendar]; // Create the NSDates NSDate *...

NSDateComponents components:fromDate and Time Zones

I have a method which extracts the hour and second components form a NSDate by breaking it down into its NSDateComponents. My code is as follows... unsigned hourAndMinuteFlags = NSHourCalendarUnit | NSMinuteCalendarUnit; NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:[N...

NSDate/NSCalendar Issue

Hi, I have the following code in my app. Its an iPad app, with five tables in a single view named monTable, tueTable etc. These tables represent monday to friday. In this code I get the date and set each table title to the date monday to friday (this week). Then if I press a button nextWeek becomes TRUE and I reload table data. This the...

NSDateComponents setWeek - Find this week

Is it possible to find the current week of the year using NSDateComponents and setWeek: ? As setWeek uses a number form 1 - 52 every week in the year, how can I find the number representing this week? ...

2 NSDateComponents - is it same week?

Hi i have got a function with BOOL return value and 2 input (NSDateComponents *) parameters. My trouble is I have two NSDateComponents values and I want to know if the two date fall within the same calendar week. i tried the simplest solutions to solve problem, my idea was the following: - (BOOL)isFunctionName:(NSDateComponents *)com...

NSDate get year/month/day

How can I get the year/month/day of a NSDate object, given no other information? I realize that I could probably do this with something similar to this: NSCalendar *cal = [[NSCalendar alloc] init]; NSDateComponents *components = [cal components:0 fromDate:date]; int year = [components year]; int month = [components month]; int day = [co...

NSDateComponents incorrectly reporting day

Hi all, I have a feeling this is due to some time zone not being set or something this is what I have: NSDateComponents *theComponents = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:workingDate]; NSLog(@"%d", theComponents.day); [theComponents setDay:3]; NSLog(@"%d", theComponents.day); se...