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
106
votes
11 answers

How to get time (hour, minute, second) in Swift 3 using NSDate?

How can you determine the hour, minute and second from NSDate class in Swift 3? In Swift 2: let date = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components(.Hour, fromDate: date) let hour = components.hour Swift…
SaRaVaNaN DM
  • 4,390
  • 4
  • 22
  • 30
100
votes
8 answers

iPhone OS: How do I create an NSDate for a specific date?

Seems like a simple thing but I can't seem to find a way to do it. It would be great to see a couple different methods.
nickthedude
  • 4,925
  • 10
  • 36
  • 51
97
votes
9 answers

How to get NSDate day, month and year in integer format?

I want to get the day, month and year components of NSDate in integer form i.e. if the date is 1/2/1988 then I should get 1, 2 and 1988 separately as an integer. How can I do this in iOS? I found the similar question but the method…
Yogi
  • 3,578
  • 3
  • 35
  • 56
97
votes
6 answers

iOS: Compare two dates

I have a NSDate that I must compare with other two NSDate and I try with NSOrderAscending and NSOrderDescending but if my date is equal at other two dates? Example: if I have a myDate = 24/05/2011 and other two that are one = 24/05/2011 and two…
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
93
votes
10 answers

How to Check if an NSDate occurs between two other NSDates

I am trying to figure out whether or not the current date falls within a date range using NSDate. For example, you can get the current date/time using NSDate: NSDate rightNow = [NSDate date]; I would then like to use that date to check if it is in…
Brock Woolf
  • 46,656
  • 50
  • 121
  • 144
87
votes
9 answers

Sort NSArray of date strings or objects

I have an NSArray that contains date strings (i.e. NSString) like this: "Thu, 21 May 09 19:10:09 -0700" I need to sort the NSArray by date. I thought about converting the date string to an NSDate object first, but got stuck there on how to sort by…
postalservice14
  • 2,515
  • 4
  • 25
  • 33
86
votes
17 answers

Comparing NSDates without time component

In a swift playground, I have been using NSDate.date() But, this always appears with the time element appended. For my app I need to ignore the time element. Is this possible in Swift? How can it be done? Even if I could set the time element to be…
agf119105
  • 1,770
  • 4
  • 15
  • 22
86
votes
12 answers

get NSDate today, yesterday, this Week, last Week, this Month, last Month... variables

I am trying to do is to get NSDate today, yesterday, this Week, last Week, this Month, last Month variables ready for comparison for headers to be added on UITableView's titleForHeaderInSection What I want is done manually in the code below for date…
hasnat
  • 2,647
  • 1
  • 20
  • 23
86
votes
4 answers

Difference between 'YYYY' and 'yyyy' in NSDateFormatter

What is exact difference between 'YYYY' and 'yyyy'. I read in this link, it states that A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar.…
P.J
  • 6,547
  • 9
  • 44
  • 74
83
votes
3 answers

How to change the current day's hours and minutes in Swift?

If I create a Date() to get the current date and time, I want to create a new date from that but with different hour, minute, and zero seconds, what's the easiest way to do it using Swift? I've been finding so many examples with 'getting' but not…
u84six
  • 4,604
  • 6
  • 38
  • 65
83
votes
11 answers

Get current NSDate in timestamp format

I have a basic method which gets the current time and sets it in a string. However, how can I get it to format the current date & time in a UNIX since-1970 timestamp format? Here is my code: NSDate *currentTime = [NSDate date]; NSDateFormatter…
Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
81
votes
9 answers

How to add one month to an NSDate?

How To Add Month To NSDate Object? NSDate *someDate = [NSDate Date] + 30Days.....;
orthehelper
  • 4,009
  • 10
  • 40
  • 67
80
votes
3 answers

Objective-C setting NSDate to current UTC

Is there an easy way to init an NSDate with the current UTC date/time?
Brodie
  • 3,526
  • 8
  • 42
  • 61
75
votes
2 answers

What unit of time does timeIntervalSinceDate return?

What unit of time does timeIntervalSinceDate return? Is it seconds, milliseconds or something else? The documentation says that it returns an NSTimeInterval, but what unit of time is that value?
Moshe
  • 57,511
  • 78
  • 272
  • 425
73
votes
7 answers

How to get the hour of the day with Swift?

How would I get the hour of the day in Swift. I have tried NSCalendar and NSDateComponents, but I'm afraid I'm just starting with Swift.
Samuel Gómez
  • 877
  • 1
  • 6
  • 7