Questions tagged [nsdate]

NSDate objects represent a single point in time. NSDate is a class cluster; its single public superclass, NSDate, declares the programmatic interface for specific and relative time values. The objects you create using NSDate are referred to as date objects. They are immutable objects. Because of the nature of class clusters, objects returned by the NSDate class are instances not of that abstract class but of one of its private subclasses.

NSDate objects represent a single point in time. NSDate is a class cluster; its single public superclass, NSDate, declares the programmatic interface for specific and relative time values. The objects you create using NSDate are referred to as date objects. They are immutable objects. Because of the nature of class clusters, objects returned by the NSDate class are instances not of that abstract class but of one of its private subclasses. Although a date object’s class is private, its interface is public, as declared by the abstract superclass NSDate. Generally, you instantiate a suitable date object by invoking one of the date... class methods.

NSDate is an abstract class that provides behavior for creating dates, comparing dates, representing dates, computing intervals, and similar functionality. NSDate presents a programmatic interface through which suitable date objects are requested and returned. Date objects returned from NSDate are lightweight and immutable since they represent an invariant point in time. This class is designed to provide the foundation for arbitrary calendrical representations.

The sole primitive method of NSDate, timeIntervalSinceReferenceDate, provides the basis for all the other methods in the NSDate interface. This method returns a time value relative to an absolute reference date—the first instant of 1 January 2001, GMT.

To parse strings containing dates and to generate string representations of a date, you should use an instance of NSDateFormatter using the methods dateFromString: and stringFromDate: respectively—see Date Formatters for more details.

NSDate models the change from the Julian to the Gregorian calendar in October 1582, and calendrical calculations performed in conjunction with NSCalendar take this transition into account. Note, however, that some locales adopted the Gregorian calendar at other times; for example, Great Britain didn't switch over until September 1752.

NSDate is “toll-free bridged” with its Cocoa Foundation counterpart, CFDateRef. See Toll-Free Bridging for more information on toll-free bridging.

Subclassing Notes

The major reason for subclassing NSDate is to create a class with convenience methods for working with a particular calendrical system. But you could also require a custom NSDate class for other reasons, such as to get a date and time value that provides a finer temporal granularity. Methods to Override

If you want to subclass NSDate to obtain behavior different than that provided by the private or public subclasses, you must do these things:

  • Declare a suitable instance variable to hold the date and time value (relative to an absolute reference date).

  • Override the timeIntervalSinceReferenceDate instance method to provide the correct date and time value based on your instance variable.

  • Override initWithTimeIntervalSinceReferenceDate:, one of the designated initializer methods.

If you are creating a subclass that represents a calendrical system, you must also define methods that partition past and future periods into the units of this calendar.

Because the NSDate class adopts the NSCopying and NSCoding protocols, your subclass must also implement all of the methods in these protocols.

Special Considerations

Your subclass may use a different reference date than the absolute reference date used by NSDate (the first instance of 1 January 2001, GMT). If it does, it must still use the absolute reference date in its implementations of the methods timeIntervalSinceReferenceDate and initWithTimeIntervalSinceReferenceDate:. That is, the reference date referred to in the titles of these methods is the absolute reference date. If you do not use the absolute reference date in these methods, comparisons between NSDate objects of your subclass and NSDate objects of a private subclass will not work.

4836 questions
366
votes
30 answers

How do I add 1 day to an NSDate?

Basically, as the title says. I'm wondering how I could add 1 day to an NSDate. So if it were: 21st February 2011 It would become: 22nd February 2011 Or if it were: 31st December 2011 It would become: 1st January 2012.
Andrew
  • 15,935
  • 28
  • 121
  • 203
308
votes
25 answers

How do I convert a date/time string into a different date string?

How will I convert this datetime from the date? From this: 2016-02-29 12:24:26 to: Feb 29, 2016 So far, this is my code and it returns a nil value: let dateFormatter = NSDateFormatter() dateFormatter.dateFormat =…
Sydney Loteria
  • 10,171
  • 19
  • 59
  • 73
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
263
votes
19 answers

Convert NSDate to NSString

How do I convert, NSDate to NSString so that only the year in @"yyyy" format is output to the string?
4thSpace
  • 43,672
  • 97
  • 296
  • 475
262
votes
20 answers

Getting the difference between two Dates (months/days/hours/minutes/seconds) in Swift

I am trying to get the difference between the current date as NSDate() and a date from a PHP time(); call for example: NSDate(timeIntervalSinceReferenceDate: 1417147270). How do I go about getting the difference in time between the two dates. I'd…
uhfocuz
  • 3,178
  • 2
  • 13
  • 27
247
votes
13 answers

How to compare two NSDates: Which is more recent?

I am trying to achieve a dropBox sync and need to compare the dates of two files. One is on my dropBox account and one is on my iPhone. I came up with the following, but I get unexpected results. I guess I'm doing something fundamentally wrong when…
n.evermind
  • 11,944
  • 19
  • 78
  • 122
233
votes
17 answers

Converting NSString to NSDate (and back again)

How would I convert an NSString like "01/02/10" (meaning 1st February 2010) into an NSDate? And how could I turn the NSDate back into a string?
cannyboy
  • 24,180
  • 40
  • 146
  • 252
231
votes
12 answers

How do you create a Swift Date object?

How do you create a date object from a date in swift xcode. eg in javascript you would do: var day = new Date('2014-05-20');
code_cookies
  • 3,910
  • 4
  • 16
  • 14
220
votes
18 answers

Convert string to date in Swift

How can I convert this string "2016-04-14T10:44:00+0000" into an NSDate and keep only the year, month, day, hour? The T in the middle of it really throws off what I am used to when working with dates.
asheyla
  • 3,175
  • 5
  • 18
  • 34
197
votes
18 answers

How can I convert string date to NSDate?

I want to convert "2014-07-15 06:55:14.198000+00:00" this string date to NSDate in Swift.
Shardul
  • 4,266
  • 3
  • 32
  • 50
178
votes
20 answers

Measure elapsed time in Swift

How can we measure the time elapsed for running a function in Swift? I am trying to display the elapsed time like this: "Elapsed time is .05 seconds". Saw that in Java, we can use System.nanoTime(), are there any equivalent methods available in…
jay
  • 3,517
  • 5
  • 26
  • 44
178
votes
4 answers

What's the optimum way of storing an NSDate in NSUserDefaults?

There's two ways of storing an NSDate in NSUserDefaults that I've come across. Option 1 - setObject:forKey: // Set NSDate *myDate = [NSDate date]; [[NSUserDefaults standardUserDefaults] setObject:myDate forKey:@"myDateKey"]; // Get NSDate *myDate =…
John Gallagher
  • 6,208
  • 6
  • 40
  • 75
158
votes
20 answers

How to determine if an NSDate is today?

How to check if an NSDate belongs to today? I used to check it using first 10 characters from [aDate description]. [[aDate description] substringToIndex:10] returns string like "YYYY-MM-DD" so I compared the string with the string returned by…
Seunghoon
  • 5,632
  • 5
  • 35
  • 41
154
votes
16 answers

Number of days between two NSDates

How could I determine the number of days between two NSDate values (taking into consideration time as well)? The NSDate values are in whatever form [NSDate date] takes. Specifically, when a user enters the inactive state in my iPhone app, I store…
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
153
votes
16 answers

NSDate Comparison using Swift

I am working on an app the requires checking the due date for homework. I want to know if a due date is within the next week, and if it is then perform an action. Most of the documentation I could find is in Objective-C and I can't figure out how…
Henry oscannlain-miller
  • 1,991
  • 3
  • 14
  • 13
1
2 3
99 100