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
4
votes
2 answers

NSDate, NSCalendar and NSDateComponents timing

NSDate *today = [NSDate date]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+2"]]; NSDateComponents *dateComponents = [calendar…
clement
  • 4,204
  • 10
  • 65
  • 133
4
votes
0 answers

DateComponentsFormatter doesn't allow fractional units

I can't make DateComponentsFormatter print out fractional units! Here's the code in Swift 4: let duration = 5400 // 1.5 hours in seconds let dateComponentsFormatter = DateComponentsFormatter.init() dateComponentsFormatter.allowedUnits =…
Wiingaard
  • 4,150
  • 4
  • 35
  • 67
4
votes
1 answer

How to prevent DateComponents from updating for time zone

I am trying to get date components from a date value from an API endpoint. I need to preserve the date values as they are already adjusted for time zone. Unfortunately, I have no control over how the API returns a date value. When I get the date…
Pheepster
  • 6,045
  • 6
  • 41
  • 75
4
votes
1 answer

swift user notifications: why is nextTriggerDate nil?

When I run the following (on January 7 2017): let dateComponents = Calendar.current.dateComponents(in: TimeZone.current, from: date) print("dateComponents = \(dateComponents)") let trigger =…
ragnarius
  • 5,642
  • 10
  • 47
  • 68
4
votes
0 answers

NSDateComponents date is returning NSTaggedDate (internal class) instead of NSDate

NSCalendar *gCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *oldDate = [NSDate date]; // Or however you get it. unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | …
Mike Dee
  • 61
  • 4
4
votes
4 answers

Iterating over a year to get each days NSDate object in Swift

Hello I have a method that returns an array of times for each day. prayTimesDate(date: NSDate, latitide : Double, longitude : Double, timeZone : Double) -> NSMutableArray I need to iterate through a whole year or maybe a date range to get an array…
gnm1978
  • 137
  • 1
  • 9
4
votes
2 answers

Finding the locale-dependent first day of the week

Given an NSDate, how do I find the first day of that date's week, given the user's locale. For example, I've heard that some countries treat Monday as the first day of the week and others use Sunday. I need to return the preceding Monday in the…
Ben Packard
  • 26,102
  • 25
  • 102
  • 183
4
votes
1 answer

Setting the day in NSDateComponents to something higher than 31 when using dateFromComponents?

Say I have a date and I want to do two things: Reset the hour to a specified one. Increment the day by a certain amount. By reading the documentation, it looks like you need to use two distinct steps: // Step 1: Get today at 9 AM. NSDateComponents…
Senseful
  • 86,719
  • 67
  • 308
  • 465
4
votes
4 answers

Trying to use NSDate for time only

I'm struggling to find a way to use NSDate for time only purposes. I was trying to do the following code: - (void)awakeFromInsert { NSDateComponents *comps = [[NSDateComponents alloc] init]; comps.minute = 45; comps.second = 0; …
Noam Solovechick
  • 1,127
  • 2
  • 15
  • 29
4
votes
1 answer

Setting NSDateComponents results in incorrect NSDate

I'm trying to get an NSDate object that has 21:00 as the local time - don't care about what day. I'm scratching my head at this very strange result: NSCalendar *calendar = [[NSCalendar…
apb
  • 3,270
  • 3
  • 29
  • 23
4
votes
2 answers

Most performant way to increment dates

I'd like to increment over days in a loop, so that it counts 2012-11-10, 2012-11-11, 2012-11-12, … What's the most performant way to achieve this? NSDate *iterationDate = [NSDate date]; for (int i = 0; i < 100; i++) { NSDateComponents *comps =…
swalkner
  • 16,679
  • 31
  • 123
  • 210
3
votes
2 answers

How to group NSDate objects by day?

I'm setting a timeStamp property for an object. Right now I'm just using [NSDate date]. The user might create 10 objects per day. But in the UI, I want to drop them by date. So each day will show all the objects that were created for that day. …
Jon
  • 4,732
  • 6
  • 44
  • 67
3
votes
2 answers

Find Date of last Sunday From Current Date

I want to find the date of last Sunday from Now. I have looked into NSDateComponents but Nothing worked right for me. Any Suggestions.
3
votes
3 answers

NSDateComponents is missing 1 second from difference of 2 dates

Got a really frustrating problem that doesn't seem to make any sense. I'm trying to get the number of years between 2 dates. Here is my code. // Initialize variable to store calendar NSCalendar *gregorian = [[NSCalendar alloc] …
Gino
  • 93
  • 1
  • 6
3
votes
2 answers

DateComponents doesn't seem to honor year

I'm attempting to convert a Date to a DateComponents, change a few properties, and get a new Date back. I've written the following code: import Foundation var components = Calendar.current.dateComponents(in: .current, from: Date()) components.day =…
Michael Hulet
  • 3,253
  • 1
  • 16
  • 33