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
59
votes
9 answers

How can I set an NSDate object to midnight?

I have an NSDate object and I want to set it to an arbitrary time (say, midnight) so that I can use the timeIntervalSince1970 function to retrieve data consistently without worrying about the time when the object is created. I've tried using an…
AstroCB
  • 12,337
  • 20
  • 57
  • 73
57
votes
3 answers

How can I get the current month as String?

I need to get may as a current month, but I could not do. How can I achieve this? let date = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Day , .Month , .Year], fromDate: date) let year…
iron
  • 715
  • 1
  • 6
  • 15
56
votes
4 answers

How to convert string to date to string in Swift iOS?

Am learning swift and am struck in converting the date String to NSDate to string. Am getting the date string in this format "Thu, 22 Oct 2015 07:45:17 +0000". I need to show the date in the MM-dd-yyyy format. I tried the following code but, it…
Yuvaraj.M
  • 9,741
  • 16
  • 71
  • 100
54
votes
11 answers

How to calculate the age based on NSDate

how to calculate the age based on the birth date in this format 6/24/1976 mon/date/year...
Linux world
  • 3,750
  • 11
  • 44
  • 59
53
votes
5 answers

Difference between two NSDate objects -- Result also a NSDate

I have two NSDate objects and I want the difference between the two and the result should again be a NSDate object. Any idea how to achieve this? Here, I am trying to address a unique problem where I have to find out the elapsed time and then…
Abhinav
  • 37,684
  • 43
  • 191
  • 309
52
votes
6 answers

Core Data Save Error (NSValidationErrorKey, Cocoa error 1570) saving NSDate

I'm getting an error with saving to a Core data object in Xcode. Xcode says that the error is in the NSDate variable 'datum' but I have tried almost everything. Error is: 2011-07-12 18:01:29.068 WeekLijstje[3205:207] Core Data Save…
Peter
  • 521
  • 1
  • 4
  • 4
52
votes
9 answers

Getting the last day of a month

How can I get the last day of the current month as an NSDate?
pradox
  • 533
  • 1
  • 4
  • 5
52
votes
6 answers

NSDate - Convert Date to GMT

I need the ability to convert an NSDate value to a GMT Date. How can I go about converting an NSDate value to a GMT formatted NSDate value, independent of whatever date locale settings the iPhone device is using?
Mick
  • 521
  • 1
  • 5
  • 3
51
votes
6 answers

How to add hours to an NSDate?

I have a date converted to double value and saved in database. Now, I want to compare if currentDate > myDataBaseDate + 8 hours i.e., I want to get 8 hours added to myDataBaseDate. I'm converting date into double values. So how do I get 8 hours…
Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256
48
votes
7 answers

How to parse a date string into an NSDate object in iOS?

I am trying to parse a date string from xml into an NSDate object in an iPhone app I am aware this must have been asked before, however, I believe I have the right syntax, but it is not working. Is there a problem with my code? The date string I…
JohnRock
  • 6,795
  • 15
  • 52
  • 61
46
votes
7 answers

Checking if one NSDate is greater than another

if (datStartDate > datEndDate) { This doesn't seem to work. I know there's a isEqual, etc., but how do I perform "is greater than"? There are both NSDate.
Jules
  • 7,568
  • 14
  • 102
  • 186
46
votes
8 answers

UIDatePicker, setting maximum and minimum dates based on todays date

If I have a UIDatePicker, and I wish to set the minimum and maximum date range to be between thirty years ago and thirty years in the future, how would I set that up?
cannyboy
  • 24,180
  • 40
  • 146
  • 252
46
votes
8 answers

Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?

If I use the following code: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm"]; NSDate *myDate = [dateFormatter dateFromString:@"2010-01-28T15:22:23.863"]; NSLog(@"%@",…
rson
  • 1,455
  • 2
  • 24
  • 43
46
votes
2 answers

Difference between 2 dates in seconds ios

I have an app where content is displayed to the user. I now want to find out how many seconds a user actually views that content for. So in my header file, I've declared an NSDate *startTime; NSDate *endTime; Then in my viewWillAppear startTime…
Adam Altinkaya
  • 649
  • 1
  • 12
  • 23
45
votes
6 answers

How to format time intervals for user display (social network like) in swift?

I have a time interval, say, 12600, which is equivalent to 3 hours 30 minutes. How could I format any such time interval so that only the highest part of the interval (for example in this case figure, the hours) is kept and have the correct locale…
Quantaliinuxite
  • 3,133
  • 4
  • 18
  • 32