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
15
votes
3 answers

Compare two NSDates for same date/time

Is date1 == date2 not a valid way to compare? If not, what is the correct alternative? Here's my code: - (NSDate*) dateWithNoTime { unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar* calendar =…
Greg
  • 34,042
  • 79
  • 253
  • 454
13
votes
2 answers

Repeat HMTimerTrigger On multiple days (Ex: Every Monday,Wednesday.... like in iOS 10 Home app)

I have checked the iOS 10 Home app. The screenshot is captured from Home app only. Since last 2 days I have been trying to implement the HMTimerTrigger repeat functionality. My requirement is I have to repeat the Trigger on every monday,tuesday and…
SRI
  • 1,514
  • 21
  • 39
12
votes
7 answers

Getting first day of a week with NSDateComponents : changing year

Hello I have a problem with setting a firstWeekDay, here is what I do: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit |…
Roman
  • 121
  • 1
  • 3
12
votes
3 answers

How to get NSDate for 00:00 on the day of [NSDate date]?

I need to get an NSDate object for 00:00(beginning of the day) from [NSDate date], let's say if currently it is 11:30am(returned by [NSDate date]), on 01/06/2012, now I need to have an NSDate for 00:00am on 01/06/2012. I haven't tried this, but what…
hzxu
  • 5,753
  • 11
  • 60
  • 95
12
votes
4 answers

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

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

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 =…
mmccomb
  • 13,516
  • 5
  • 39
  • 46
11
votes
4 answers

NSDateComponentsFormatter's stringFromDate(_, toDate:) returns nil

Question Why is string nil? let formatter = NSDateComponentsFormatter() let referenceDate = NSDate(timeIntervalSinceReferenceDate: 0) let intervalDate = NSDate(timeInterval: 3628810, sinceDate: referenceDate) let string =…
11
votes
3 answers

NSDateComponentsFormatter stringFromTimeInterval returns incorrect string for intervals shorter than 1 minute

I'm using NSDateComponentsFormatter on iOS 8 and i've noticed a possible bug in the implementation of the stringFromTimeInterval: method, but I can't say it for sure. This is how I have set up the formatter: NSDateComponentsFormatter *formatter =…
JonasG
  • 9,274
  • 12
  • 59
  • 88
11
votes
4 answers

Working with NSDate components in Swift

I have a date that I need to split in some components. for example let components = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil) var…
Giorgio
  • 330
  • 1
  • 3
  • 9
10
votes
4 answers

Swift DateComponentsFormatter drop leading zeroes but keep at least one digit in Minutes place

I am using the following DateComponentsFormatter in Swift: let formatter = DateComponentsFormatter() formatter.unitsStyle = .positional formatter.allowedUnits = [.hour, .minute, .second] formatter.zeroFormattingBehavior = [.default] This produces…
10
votes
2 answers

How to set repeat frequency in User Notification

Till iOS 9 we write local notifications like this UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = pickerDate; localNotification.alertBody = self.textField.text; localNotification.timeZone =…
S.J
  • 3,063
  • 3
  • 33
  • 66
10
votes
4 answers

How to convert NSDateComponents to NSDate?

I have this function : private func getCurrentDateComponent() -> NSDateComponents { let date = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute…
YMonnier
  • 1,384
  • 2
  • 19
  • 30
9
votes
2 answers

ios Swift Date without Time

I am trying to get only date that means hours, mins, seconds are zeroes. This is what i am trying extension Date { var onlyDate: Date? { get { let calender = Calendar.current var dateComponents =…
Ahsan Aasim
  • 1,177
  • 3
  • 14
  • 40
9
votes
1 answer

What are the iOS 8.0+ NSCalendarUnits?

I noticed that all the NSCalendarUnits (e.g., NSDayCalendarUnit) is officially deprecated as of iOS 8.0. What is the replacement?
Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
9
votes
4 answers

NSDateComponents: difference between weekOfYear and weekOfMonth

In my app, I have to set up a weekly reminder so that an alert goes off at the same day/time, a week from now. I have been using NSDateComponenents.week = 1 and adding that to an NSDate, using NSCalendar's dateByAddingComponents:toDate:options: API.…
Z S
  • 7,039
  • 12
  • 53
  • 105
1
2
3
37 38