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
9
votes
1 answer

Adding year component to islamic calendar fails

Following code shows the problem: Advancing a full year from the first day of the year 1435 does not result in the first day of 1436. Any ideas what i'm missing? NSDateComponents *components = [[NSDateComponents alloc] init]; [components…
Frank Martin
  • 2,584
  • 2
  • 22
  • 25
8
votes
6 answers

Get first day in a month from NSCalendar

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]…
bogen
  • 9,954
  • 9
  • 50
  • 89
8
votes
1 answer

In NSDateComponents, how do you know if it's AM or PM?

When I use NSDateComponents, how do I know I'm setting a time as Am or Pm? Or is it 24-hour format? Here's the code: NSDate *morningEnd = [NSDate date]; NSDateComponents *components1 = [gregorian components:NSUIntegerMax…
SunnyChopper
  • 183
  • 1
  • 8
7
votes
4 answers

Given year & month, how to check a particular day exists in that month

I am trying to check if a particular day exists in a month or not. However unable to find anything of help. To give an example I tried below code in Playground; var components = DateComponents() components.month = 2 components.year =…
Rameswar Prasad
  • 1,331
  • 17
  • 35
7
votes
4 answers

How to add a number of weeks to an NSDate?

I have in the past used the below function to add on a specific time interval using NSDateComponents to an existing date. (NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date …
Rich
  • 882
  • 1
  • 8
  • 19
7
votes
1 answer

NSTime: Why [NSDateComponent date] returns nil here?

I want to convert a NSDate from one timezone to another. Here is my code: NSDate* convertTime(NSDate* fromDate, NSTimeZone* fromTimeZone, NSTimeZone* toTimeZone) { if (fromTimeZone == toTimeZone) { return fromDate; } …
Anthony Kong
  • 37,791
  • 46
  • 172
  • 304
7
votes
6 answers

Rounding of NSdate to nearest hour in iOS

I have data in the format time: 13:52, 10:30, 11:48 etc I would like to round to the nearest hour. like for 13:52 -> 14:00 , 10:30 -> 11:00 and 11:48 -> 12:00. How can I do that with NSDate?
Sumit Patel
  • 2,547
  • 8
  • 33
  • 45
6
votes
3 answers

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

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…
AWF4vk
  • 5,810
  • 3
  • 37
  • 70
6
votes
1 answer

What is NSEraCalendarUnit?

I was playing around with NSCalendar and NSDateComponents today and I saw constants like NSDayCalendarUnit and NSEraCalendarUnit. NSDayCalendarUnit returns 4 if today is the 4th of December for instance. NSEraCalendarUnit returns the era which is…
hollow7
  • 1,506
  • 1
  • 12
  • 20
6
votes
3 answers

Swift 4 : Set Different Date and Time

I know how to get local date and time, but what I want to do is getting the date and time from different places. For example, I want to find out what the time and date is in New York. How can i solve this simple problem? Here is my code for local…
jorjj
  • 1,479
  • 4
  • 20
  • 36
6
votes
5 answers

Swift 2.0, validation on DatePicker maximum Date

Suppose today's date is 16 May 2016, I want to restrict the user to display date up-to 16 jun 2016.
Nisha Nair
  • 327
  • 1
  • 5
  • 17
6
votes
1 answer

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…
alloy
  • 20,908
  • 2
  • 30
  • 40
6
votes
1 answer

NSDateComponents week is working differently on iOS8

NSDateComponents week is deprecated since iOS8. Apple suggest using weekOfMonth or weekOfYear instead, depending on the context, but they are working in a different way. For example: components1 = [calendar components:NSMonthCalendarUnit |…
KlimczakM
  • 12,576
  • 11
  • 64
  • 83
6
votes
2 answers

I want to get a quarter value in NSDateComponents class

I want to get a quarter value from specified date. (*date variable) So. I have made following code and run. but, quarter value is zero. Why quarter value is zero? where's problem? Please advice from advanced man. NSDateComponents *comp1 =…
S.J. Lim
  • 3,095
  • 3
  • 37
  • 55
6
votes
1 answer

How to determine day, month, and year as int in iOS

I want to determine day, month, and year of current date in iOS. I found this after a search on the net: NSDate *date = [NSDate date]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents…
rudasagani
  • 157
  • 2
  • 12
1 2
3
37 38