Questions tagged [nsdatecomponents]

NSDateComponents is a class in Apple's Cocoa and CocoaTouch frameworks which represents either a date or a period of time as composed of several elements -- days, hours, and months, for example.

NSDateComponents encapsulates the components of a date in an extendable, object-oriented manner.

Resources:

567 questions
5
votes
3 answers

Calculating the end of a month, or its last second

I have the simplest of tasks that I can't figure out how to do correctly. With the snippet below I can find out the beginning of a month. NSCalendar *cal = [NSCalendar currentCalendar]; NSDate *beginning = nil; [cal rangeOfUnit:NSMonthCalendarUnit…
epologee
  • 11,229
  • 11
  • 68
  • 104
5
votes
2 answers

Is the starting day of the week stored in NSLocale somewhere?

I live in the Netherlands. We Dutch people like to see Monday as the start of the week. I know that people in the US like to consider Sunday as the start of the week. Silly Americans ;) If I want to present a week view to my global users, can I get…
epologee
  • 11,229
  • 11
  • 68
  • 104
5
votes
2 answers

Why NSCalendar's dateFromComponents returns wrong date?

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 =…
arthur_skypro
  • 97
  • 3
  • 8
5
votes
6 answers

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…
Tim
  • 51
  • 1
  • 3
5
votes
1 answer

NSCalendarUnit flag to set all flags

The following will create an NSDateComponents object containing day and weekday but not hour: var today = NSDate() var gregorian = NSCalendar(calendarIdentifier: NSGregorianCalendar) var flags: NSCalendarUnit = .DayCalendarUnit |…
Chris Redford
  • 16,982
  • 21
  • 89
  • 109
5
votes
2 answers

NSDate behaviour when saving to plist

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]…
Ranjit
  • 4,576
  • 11
  • 62
  • 121
5
votes
4 answers

Weekday of first day of month

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.
Anton
  • 3,102
  • 2
  • 28
  • 47
5
votes
2 answers

NSCalendar dateFromComponents returns wrong date by 2

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…
5
votes
2 answers

NSDateComponents week component of this last week of the year

how many weeks there are in a year? because i have searched on the web and i found there is in total 52.1 week in a year, but if i do this in Xcode: NSCalendar *gregorian = [[NSCalendar alloc] …
Piero
  • 9,173
  • 18
  • 90
  • 160
5
votes
3 answers

In Objective-C, to get the current hour and minute as integers, we need to use NSDateComponents and NSCalendar?

I'd like to get the current hour and minute as integers. So if right now is 3:16am, I'd like to get the two integers: 3 and 16. But it looks like [NSDate date] will give the number of seconds since 1970, or it can give a string of the current time…
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
4
votes
0 answers

Getting first date in week given a year and weeknumber

I'm trying to get the first date in a week giving a year and weeknumber in objective-c. I want to take the dutch calendar into account. Monday is the first day in the week here. So just as it is in MS Outlook I would like my function to return…
4
votes
1 answer

Schedule local notification every n days (timezone safe)

I believe this has been asked several times, but there is no clear answer. There are two ways to schedule temporal notifications: UNCalendarNotification and UNTimeIntervalNotificationTrigger. Scheduling a notification for a specific time and day of…
4
votes
1 answer

iPhone/Objective-C - Help with NSDateComponents and NSCalendar issues with overseas time zones

I use the below Utility method to display the seconds, minutes, hours, etc gone by. e.g., 1 hour ago I have a friend overseas testing our iPhone application and every time they submit an item the method always returns "1 hour ago" because New…
gotnull
  • 26,454
  • 22
  • 137
  • 203
4
votes
6 answers

format a NSDate to DDMMYYYY?

I have to send a DDMMYYY date from a Date to communicate with an API. NSDateComponents *dateComponents; NSCalendar *gregorian; NSDate *date; gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; dateComponents =…
clement
  • 4,204
  • 10
  • 65
  • 133
4
votes
3 answers

IOS: problem with NSDate

I have this code: NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setYear:2011]; [components setDay:1]; [components setMonth:4]; NSCalendar *gregorianCalendar = [[[NSCalendar alloc]…
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241