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

Find difference in two dates in iOS

I am not sure what I am doing wrong, I need to find difference between two dates and extract seconds from it, below is my code. I am not getting correct seconds. There is difference of seconds. public func captureStartTime() { …
3
votes
1 answer

DateComponentsFormatter and .nanosecond

So, I'm using a DateComponentsFormatter in a situation where I need to format only the nanoseconds of a given TimeInterval. The problem is that it just does not work as intended, or I'm confusing something here that I'm not aware of. Here are 3…
3
votes
3 answers

Get current month date in date components

I am getting NSDateComponents of date 2018-06-01 00:00:00 +0000 like this NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth|NSCalendarUnitYear |NSCalendarUnitHour | NSCalendarUnitMinute…
3
votes
0 answers

Set repeat notification trigger using DateComponents

Currently working on an App need set different type of repeat rule of notification Like repeat everyday, workday, weekday or several days of the week. var dateInfo = DateComponents() dateInfo.hour = 7 dateInfo.minute = 0 dateInfo.weekday = 1 let…
3
votes
1 answer

iPhone - NSDateComponents/NSCalendar and Elapsed time (e.g., Scanned 3 hours ago.)

I'd like to display to the user the following elapsed times: e.g, Scanned 13 seconds ago Scanned 2 minutes ago Scanned 1 hour ago Scanned 2 days ago Scanned 1 month ago Scanned 1 year ago I use the following method to achieve this, however it's not…
gotnull
  • 26,454
  • 22
  • 137
  • 203
3
votes
1 answer

Swift - Date from components is incorrect when printed. Date has 2 values?

I'm trying to store a time of day in a Date: let calendar = NSCalendar.init(identifier: .gregorian) let components = NSDateComponents() components.hour = 7 components.minute = 0 var newDate = calendar?.date(from: components as…
AaronBaker
  • 1,013
  • 11
  • 15
3
votes
4 answers

swift 3 getting list of nearest hour quarters

Okay, I have let date = Date() in Swift 3 and I need to get list of nearest hour quarters. Let's say current date is 2017-02-08 15:24:56 +0000 so that desired list will be 2017-02-08 15:30:00 +0000 2017-02-08 15:45:00 +0000 2017-02-08 16:00:00…
Eugene Brusov
  • 17,146
  • 6
  • 52
  • 68
3
votes
1 answer

DateComponents giving wrong date

I am trying to convert today's date using DateComponents: let calendar = Calendar.current //add today's date var todayDate = Date() var dateComponents = calendar.dateComponents([.day, .month, .year], from: todayDate) dateComponents.timeZone…
StudentX
  • 2,506
  • 5
  • 19
  • 28
3
votes
2 answers

dateFromComponents gives Wrong date

I am trying to get a Date object from NSDateComponents, but Calendar.date(from: Components) is giving a date one day before the date that was in my original components. let calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian) let…
iPhone Guy
  • 1,285
  • 3
  • 23
  • 34
3
votes
1 answer

What's the correct way to store a time interval in Core Data?

I need to store a time interval in an app's database that will be added occasionally to NSDate. Problem is, I don't know what data type to choose for it. Some people advise to store NSTimeInterval, but will it cause miscalculations when user's…
Randex
  • 770
  • 7
  • 30
3
votes
5 answers

How to show 7 days from current date in ios if today is last day of the month

I am showing seven days of month in datepickerview. But my problem is how to show seven days from current date if todays current date is last day of the month. Example : Suppose todays month is june, june has 30 days so todays date is 30 .Then my…
Sandeep
  • 725
  • 1
  • 14
  • 28
3
votes
3 answers

Get last tuesday in month ios

Can get last weekday in the month through NSDateComponents? For example: last monday in month or last friday in month. etc
Sergey
  • 79
  • 9
3
votes
4 answers

Get the exact difference between 2 dates for a single NSDateComponent

How can I get the exact difference (in decimal) between 2 values of NSDate. Eg. Jan 15 2016 to Jul 15 2017 = 1.5 Years. I can use something like: NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: date1, toDate:…
rgamber
  • 5,749
  • 10
  • 55
  • 99
3
votes
1 answer

NSDateComponents converting UTC date to local timezone, messing up "today" nsdate comparison

I am trying to use NSDateComponents in order to test if a dynamic date occurs "today". I use it to extract the year/month/day and then compare those values for two NSDates. In theory this should work. I am on east coast, so EST, -5 from UTC. …
Miro
  • 5,307
  • 2
  • 39
  • 64
3
votes
2 answers

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; …
christian Muller
  • 5,016
  • 6
  • 34
  • 44