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
27
votes
5 answers

iOS: Compare two NSDate-s without time portion

I want to compare two dates: date1 and date2 2011-06-06 12:59:48.994 Project[419:707] firstDate:2011-06-06 10:59:21 +0000 2011-06-06 12:59:49.004 Project[419:707] selectedData:2011-06-06 10:59:17 +0000 but these dates have different time and when I…
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
26
votes
6 answers

how do I set an existing NSDate's time?

how do I set an existing NSDate's time? That is I have a date (say current date/time), but then want to set this to a specific time (e.g. 11.23am) say. What is the quickest way to do this in objective C? I'm looking at NSCalendar and see methods…
Greg
  • 34,042
  • 79
  • 253
  • 454
26
votes
6 answers

How do you calculate the day of the year for a specific date in Objective-C?

This is something I found myself spending hours to figure out and therefore want to share with you. The question was: How do I determine the day of the year for a specific date? e.g. January 15 is the 15th day and December 31 is the 365th day when…
Godisemo
  • 1,813
  • 2
  • 18
  • 26
26
votes
3 answers

Difference in hours between two NSDate

Here I'm trying to calculate the hours between two dates. When i run the application, it crashes. Could you please tell me the mistake in this code? NSString *lastViewedString = @"2012-04-25 06:13:21 +0000"; NSDateFormatter *dateFormatter =…
shebi
  • 707
  • 2
  • 14
  • 23
25
votes
3 answers

Subtracting two NSDate objects

Possible Duplicate: How to Get time difference in iPhone I´m getting date and time from a JSON feed. I need to find the difference between the date I´m getting from the feed and today´s date and time. Any suggestions how I can do this? I know I…
Magnus
  • 1,444
  • 5
  • 22
  • 31
25
votes
1 answer

how to use NSDateFormatter to see "Today" string

apple use it in the messages app for today messages Today 11:45 AM Yesterday 11:45 AM i see it in the apple developer site To specify a custom fixed format for a date formatter, you use setDateFormat:. The format string uses the format patterns…
user301212
  • 311
  • 1
  • 4
  • 11
25
votes
8 answers

NSInvalidArgumentException, reason: 'Invalid type in JSON write (__NSDate)'

I'm getting this exception when I try to JSON encode NSDate object.I believe NSDate is not compatible for JSON encoding. but I must encode the date.Any solutions? *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:…
Harikrishnan
  • 9,688
  • 11
  • 84
  • 127
24
votes
3 answers

How to get nsdate with millisecond accuracy?

I need to get time in millisecond accuracy. How can I get it from NSDate. Currently when I NSLog time, it is showing only upto seconds.
sujith1406
  • 2,822
  • 6
  • 40
  • 60
24
votes
2 answers

Removing time components from an NSDate object using Objective C/Cocoa

I'm trying to write a simple function using Objective C that accepts an NSDate object and returns an NSDate object that contains the same date value but has removed any time components from the date. For example if I were to pass an NSDate with a…
Adam Oberhausen
  • 341
  • 1
  • 2
  • 4
24
votes
3 answers

Sort array of custom objects by date swift

I have a custom object "Meeting" I am adding all the objects in an array. What I want to do is sort the array from Meeting.meetingDate. I'm also using Parse, I have two queries and putting the results into one array. I am able to add the data to…
Hassan Mahmood
  • 1,591
  • 2
  • 12
  • 24
24
votes
4 answers

Creating an NSDateFormatter in Swift?

I am new to swift and am very unfamiliar with Objective-C. Could someone help me convert this to Swift? I got this code from Ray Wenderlich's best iOS practices - http://www.raywenderlich.com/31166/25-ios-app-performance-tips-tricks Where would you…
John Pollard
  • 3,729
  • 3
  • 24
  • 50
24
votes
3 answers

Swift: using UIDatePicker as UITextField inputView not updating

I have a textfield and when it is selected a datepicker pops up instead of a keyboard, but it only updates with todays date the first time. The user changes the date, but the textfield does not continue to update. What is the problem? This is how I…
agf119105
  • 1,770
  • 4
  • 15
  • 22
24
votes
2 answers

NSTimer not firing when runloop is blocked

I am just about finished with my app and beta testing found a bug in the stopwatch portion... The stopwatch uses an nstimer to do the counting and has a table for storing laps, but when the lap table is scrolled the watch stops or pauses and does…
echobravo
  • 393
  • 1
  • 3
  • 11
24
votes
4 answers

NSDate Min and Max Possible Values

Anyone know what the minimum and maximum possible values are for an NSDate?
Oliver Pearmain
  • 19,885
  • 13
  • 86
  • 90
23
votes
3 answers

How do I determine whether an NSDate (including time) has passed

How do I determine whether an NSDate (including time) has passed? I need to basically compare today's date to a date in the db but am stumped.
TheLearner
  • 19,387
  • 35
  • 95
  • 163