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
295
votes
17 answers

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…
Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
128
votes
25 answers

NSDate beginning of day and end of day

-(NSDate *)beginningOfDay:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |…
user1028028
  • 6,323
  • 9
  • 34
  • 59
68
votes
8 answers

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

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 =…
NiravS
  • 1,144
  • 1
  • 12
  • 24
49
votes
7 answers

Calculate age from birth date using NSDateComponents in Swift

I am trying calculate the age from birthdayDate in Swift with this function: var calendar : NSCalendar = NSCalendar.currentCalendar() var dateComponentNow : NSDateComponents = calendar.components( NSCalendarUnit.CalendarUnitYear, …
user3745888
  • 6,143
  • 15
  • 48
  • 97
45
votes
7 answers

All dates between two Date objects (Swift)

I’m creating a date using NSDateComponents(). let startDate = NSDateComponents() startDate.year = 2015 startDate.month = 9 startDate.day = 1 let calendar = NSCalendar.currentCalendar() let startDateNSDate =…
ixany
  • 5,433
  • 9
  • 41
  • 65
45
votes
4 answers

Where is the extra 75 seconds coming from?

While writing some unit tests on a Julian Day calculator, I found that dates prior to 2nd December 1847 were being initialised incorrectly by NSDate. They appear to have 75 seconds added on. I haven't been able to find anything pointing to that…
Vince O'Sullivan
  • 2,611
  • 32
  • 45
29
votes
4 answers

NSDateComponents of an NSDate

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?
cfischer
  • 24,452
  • 37
  • 131
  • 214
26
votes
6 answers

how do I set an existing NSDate's time?

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…
Greg
  • 34,042
  • 79
  • 253
  • 454
21
votes
4 answers

NSDate subtract one month

I want two NSInteger year, month to go one month back. e.g. now is 2011-01 how can I get 2010-12. adding is one thing, but I want to subtract. NSDate *date = [NSDate date]; NSDateComponents *dateComponents = [calendar components:unitFlags…
endo.anaconda
  • 2,449
  • 4
  • 29
  • 55
21
votes
1 answer

When does dateByAddingComponents:toDate:options return nil?

I've been using dateByAddingComponents:toDate:options: and dateByAddingUnit:value:toDate:options: and using optional binding to get the date out of it. Like this: guard let startOfNextMonth = calendar.dateByAddingComponents(oneMonthComponent,…
NickCE
  • 409
  • 3
  • 7
20
votes
1 answer

Hour in NSDateComponent in 12 or 24h format?

Is the hour component of NSDateComponent in a 12h or 24h format? I cannot find anything about this in the documentation...
Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72
19
votes
2 answers

Making Date With Given Numbers

I have the following Swift (Swift 3) function to make a date (Date) with date components (DateComponents). func makeDate(year: Int, month: Int, day: Int, hr: Int, min: Int, sec: Int) -> NSDate { let calendar = NSCalendar(calendarIdentifier:…
El Tomato
  • 6,479
  • 6
  • 46
  • 75
17
votes
1 answer

NSDate from NSDateComponents

I am trying to alter the hh, mm, ss component of an NSDate to that of another NSDate - but for some reason, the time gets set to midnight for some reason. I can't figure out why! Here is the code; + (NSDate *) fixTime:(NSDate*)fromDate …
Sam
  • 827
  • 4
  • 9
  • 21
16
votes
1 answer

Can DateComponentsFormatter format fractional seconds?

I want to print, e.g., the value 1.5 as "1.5 sec", like Safari's timeline does, and I'm trying to use DateComponentsFormatter for it. Unfortunately, its .allowedUnits only goes down to .second (even though the enum has .nanosecond). I tried setting…
16
votes
2 answers

Differences in NSDateComponents syntax?

I have been working on a clock app in Swift and with Xcode 6.3.2 the following code builds and runs just fine. // Get current time let date = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components(.CalendarUnitHour…
zkarj
  • 657
  • 9
  • 23
1
2 3
37 38