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
45
votes
7 answers

All dates between two Date objects (Swift)

I’m creating a date using NSDateComponents(). let startDate = NSDateComponents() startDate.year = 2015 startDate.month = 9 startDate.day = 1 let calendar = NSCalendar.currentCalendar() let startDateNSDate =…
ixany
  • 5,433
  • 9
  • 41
  • 65
45
votes
4 answers

Where is the extra 75 seconds coming from?

While writing some unit tests on a Julian Day calculator, I found that dates prior to 2nd December 1847 were being initialised incorrectly by NSDate. They appear to have 75 seconds added on. I haven't been able to find anything pointing to that…
Vince O'Sullivan
  • 2,611
  • 32
  • 45
44
votes
2 answers

Sorting NSMutableArray By Object's Property

So this is a rather basic question regarding the best way to sort an NSMutableArray of custom objects. I have a an NSMutableArray of custom objects, each object with an NSString and NSDate that go together. I need to sort the array by the newest…
Rich Haygren
  • 443
  • 1
  • 4
  • 4
44
votes
5 answers

converting timestamp to nsdate format

I want to convert my timestamp value 1308031456 to NSDate format (which yields the value Tue, 14 Jun 2011 06:04:16 GMT in online web conversion). How would I convert between the two programmatically?
ramya
  • 451
  • 1
  • 4
  • 3
44
votes
11 answers

Swift - Get local date and time

How can I get local date and time in Swift? let last_login = String(NSDate.date())
Nurdin
  • 23,382
  • 43
  • 130
  • 308
44
votes
8 answers

How do I get the current Date in short format in Swift

In the images below you can see the code I wrote and the values of all the variables: class fun getCurrentShortDate() -> String { var todaysDate = NSDate() var dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "dd-MM-yyyy" …
Friso Buurman
  • 569
  • 1
  • 4
  • 7
44
votes
6 answers

NSDate of yesterday

How do I create an NSDate object with a custom date other than the current date? For example I would like to create a var of yesterday or of 2 days ago.
Marcus
  • 9,032
  • 11
  • 45
  • 84
44
votes
18 answers

Current Week Start and End Date

I want to get the current week start and end date and I also want to use the previous week start and end date and next week of the start and end date in current month. Thanks in Advance.
Rushabh
  • 3,208
  • 5
  • 28
  • 51
43
votes
4 answers

Generate NSDate from day, month, and year

I'm trying to generate a NSDate from a month day and year (all in integer format). Right now my attempt is such: NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; NSNumber *day =…
minimalpop
  • 6,997
  • 13
  • 68
  • 80
43
votes
2 answers

Why does NSDateFormatter return nil date for these 4 time zones?

Try running this in iOS6 (haven't tested pre iOS6): NSDateFormatter *julianDayDateFormatter = nil; julianDayDateFormatter = [[NSDateFormatter alloc] init]; [julianDayDateFormatter setDateFormat:@"g"]; for (NSString *timeZone in [NSTimeZone…
lpa
  • 710
  • 6
  • 14
42
votes
4 answers

NSDate() vs NSDate.date() in Swift

I´m following along with the Bloc.io Swiftris tutorial where they initialize a date by: lastTick = NSDate.date() Which causes a compile error: 'date()' is unavailable: use object construction 'NSDate()' Which should equal: NSDate *lastTick =…
max
  • 96,212
  • 14
  • 104
  • 165
42
votes
16 answers

How to convert NSDate in to relative format as "Today","Yesterday","a week ago","a month ago","a year ago"?

I want to convert nsdate in to relative format like "Today","Yesterday","a week ago","a month ago","a year ago","date as it is". I have written following method for it.. but some how its just printing as it is date.. can you please tell me what…
BhavikKama
  • 8,566
  • 12
  • 94
  • 164
39
votes
6 answers

Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset?

I'm having trouble converting an ISO 8601 timestamp into an NSDate. I tried to use NSDateFormatter, but I can't get it to work with the UTC time offset that appears on the end of the timestamps. To explain, I would like to convert a timestamp such…
EJV
  • 1,009
  • 1
  • 10
  • 17
39
votes
5 answers

Swift 3: Date vs NSDate?

What is the difference between the NS and non NS classes? Particularly NSDate vs Date? Does NS represent some type of wrapper around the core non NS functionality?
Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
39
votes
4 answers

How to get the Current Date in ReactNative?

I am building my first ReactNative iOS and Android app. I am an iOS coder with Swift and Obj-C. How do I fetch the current date using ReactNative. Shall I use Native Modules or is there an ReactNative API from which I can get this data ?
Gautham Pai
  • 568
  • 1
  • 5
  • 11