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
1
vote
1 answer

Issue with Core Data attribute returning 0

Possible Duplicate: Core Data not saving NSString. Hey all! Currently experiencing a weird issue with Core Data in my latest application. Basically what I'm accomplishing is that of parsing JSON and adding each object into Core Data under the…
Sebastien Peek
  • 2,528
  • 2
  • 23
  • 32
1
vote
2 answers

Converting string to date returns nil

I am trying to convert my string to a date using a static date formatter. When I make the call to stringToDate() using the variables below, a nil value is returned. I've checked previous posts about this issue where people are saying it's because of…
Simon Jackson
  • 181
  • 1
  • 1
  • 7
1
vote
3 answers

How to check for valid time format?

I am trying to check if a string variable conforms to either hh:mm AM or hh:mm PM twelve hour time format. Where hh represents hours, mm represents minutes and AM or PM represents morning or evening. I have a CSV file where each line contains a time…
infinite369
  • 65
  • 1
  • 11
1
vote
1 answer

How can I check if a date / time is within 1 minute of the current date / time

I have an access token with an expiration time. I would like to check when using my access token, does it expire within a minute. If it does expire in less than a minute, I would like to take some action. I am struggling to understand how I can…
Tim J
  • 1,211
  • 1
  • 14
  • 31
1
vote
0 answers

NSPredicate to filter by time or hour independent of other date components

I know how to filter an array or set of fetchedResults by date ranges. How can filter by time of day as in 3PM to 5PM. Some answers on SO say you can't do this for a SQLLite backed MOC and have to fetch all the results and filter. However, I can't…
user1904273
  • 4,562
  • 11
  • 45
  • 96
1
vote
3 answers

NSDate retain message sent to deallocated instance

I've been having some problems with NSDate and saving it in NSUserDefaults. It seams that every second time NSUserDefaults saves my NSDate, it can't because it is deallocated and shows this error in the log. -[__NSDate retain]: message sent to…
Baza207
  • 2,123
  • 1
  • 22
  • 40
1
vote
1 answer

Difference between setting NSCalendar's timezone using `localTimeZone` vs using `timeZoneWithAbbreviation` giving different values

I have configured my phone (actual device, not simulator) to be in London UK which is GMT. When I set the timezone on a NSCalendar using localTimeZone, it gives me the time off by 1 minute and 15 seconds as compared to if I set it using [NSTimeZone…
1
vote
1 answer

how can I improve this "DateFromNextWeekDay:FromDate" method code?

Would be happy to hear of suggestions re how to improve / shorten this method. In short needing to: Find the next date for which it's day of week (e.g. Wed) matches what is passed into the method. For example the next WED from a given date (and…
Greg
  • 34,042
  • 79
  • 253
  • 454
1
vote
3 answers

NSDateFormatter behavior problem

I have a problem using NSDateFormatter in iPhone programming. Below is my code snippet. NSString *inputDate = @"2011-03-14"; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; NSDate *entryDate =…
Jan
  • 11
  • 1
1
vote
0 answers

NSDateFormatter starts in 2000, but NSDate starts in 2001

Does anyone know why NSDateFormatter starts in 2000, but NSDate starts in 2001? let date = Date(timeIntervalSinceReferenceDate:0) is 00:00:00 UTC on 1 January 2001 but a NSDateFormatter that converts minutes (for example 11:00) to a NSDate, returns…
Ricardo
  • 2,831
  • 4
  • 29
  • 42
1
vote
2 answers

How do I convert Swift date and TimeInterval so that I can send to php server?

I have a swift date and timeInterval that I want to send to php server via Alamofire. What should I do to these variables in order to send either via JSON or param? Is there a specific format I should use? ( I do not know how the interaction…
1
vote
1 answer

Convert hours form 12 hour time to 24 hour time Swift and save as an integer

I want to convert my variable for hours which is an integer into a 24 hour time system (for example, if it is 01:05:13 PM, hours will be saved as 13, minutes will be saved as 5, and seconds will be saved as 13) so that I can use it for some math…
1
vote
1 answer

Swift: Get localized time display - with separate AM/PM in 12 hours regions

I would like to display the time of the current date in a UILabel in the current locale. Assuming the current time would be 15:30 (24 hours display) / 3:30 PM (12 hours display). Now, if the current locale of the user is 12 hours display, the label…
Pauli
  • 343
  • 1
  • 4
  • 17
1
vote
2 answers

NSPredicate fetch Core Data objects with dates attribute within NSDate date range

I'm using an NSPredicate with a couple of <= constructs to fetch objects from a Core Data store with a startDate attribute between 2 dates but I'm getting 0 objects fetched. Here's my NSPredicate: NSPredicate *predicate = [NSPredicate…
matthew
  • 2,156
  • 5
  • 22
  • 38
1
vote
2 answers

Date from String using DateFormatter returning nil

I've a String like yyyy-MM-dd and I want create a Date with this. static func dateFromStringWithBarra(date : String) -> String { print("DATE: \(date)") let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" let…
Augusto
  • 3,825
  • 9
  • 45
  • 93