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
151
votes
10 answers

How to convert NSDate into unix timestamp iphone sdk?

How to convert an NSDate into Unix timestamp? I've read many posts which do the reverse. But I'm not finding anything related to my question.
neha
  • 6,327
  • 12
  • 46
  • 78
145
votes
15 answers

iOS: Convert UTC NSDate to local Timezone

How do I convert a UTC NSDate to local timezone NSDate in Objective C or/and Swift?
Zap
132
votes
8 answers

Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds

I basically need to get current date and time separately, formatted as: 2009-04-26 11:06:54 The code below, from another question on the same topic, generates now: |2009-06-01 23:18:23 +0100| dateString: |Jun 01, 2009 23:18| parsed: …
bcsantos
  • 2,635
  • 5
  • 21
  • 22
132
votes
12 answers

Subtract 7 days from current date

It seems that I can't subtract 7 days from the current date. This is how i am doing it: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *offsetComponents = [[NSDateComponents alloc]…
Alex Tau
  • 2,639
  • 4
  • 26
  • 30
128
votes
2 answers

Get current date in Swift 3?

How can I set label.text current date in Swift 3? I want to print just today to the screen. I did not find how to do that. In c# is very simple: var date = DateTime.Now I need to write 15.09.2016 in swift 3. thanks
yucel
  • 1,311
  • 2
  • 9
  • 9
128
votes
25 answers

NSDate beginning of day and end of day

-(NSDate *)beginningOfDay:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |…
user1028028
  • 6,323
  • 9
  • 34
  • 59
126
votes
9 answers

NSPredicate: filtering objects by day of NSDate property

I have a Core Data model with an NSDate property. I want to filter the database by day. I assume the solution will involve an NSPredicate, but I'm not sure how to put it all together. I know how to compare the day of two NSDates using…
125
votes
12 answers

How do I get an ISO 8601 date on iOS?

It's easy enough to get the ISO 8601 date string (for example, 2004-02-12T15:19:21+00:00) in PHP via date('c'), but how does one get it in Objective-C (iPhone)? Is there a similarly short way to do it? Here's the long way I found to do…
JohnK
  • 6,865
  • 8
  • 49
  • 75
120
votes
9 answers

How can I calculate the difference between two dates?

How can I calculate the days between 1 Jan 2010 and (for example) 3 Feb 2010?
Frederik Heyninck
  • 3,493
  • 4
  • 20
  • 19
117
votes
9 answers

How do I get hour and minutes from NSDate?

In my application I need to get the hour and minute separately: NSString *currentHour=[string1 substringWithRange: NSMakeRange(0,2)]; int currentHourInNumber=[currentHour intValue]; Consider string1 contains 11:59:13 AM which is coming from…
mac
  • 4,760
  • 7
  • 31
  • 33
115
votes
12 answers

first and last day of the current month in swift

I'm trying to get the first and last day of the month in swift. So far I have the following: let dateFormatter = NSDateFormatter() let date = NSDate() dateFormatter.dateFormat = "yyyy-MM-dd" let calendar = NSCalendar.currentCalendar() let components…
user2363025
  • 6,365
  • 19
  • 48
  • 89
114
votes
21 answers

Get day of week using NSDate

I created a method that is supposed to take in a string in "YYYY-MM-DD" form and spit out an int that represents the dates position in relation to the week it is in (regardless if it overlaps between months). So e.g sunday=1 monday=2 and so on.…
boidkan
  • 4,691
  • 5
  • 29
  • 43
110
votes
6 answers

Swift - check if a timestamp is yesterday, today, tomorrow, or X days ago

I'm trying to work out how to decide if a given timestamp occurs today, or +1 / -1 days. Essentially, I'd like to do something like this (Pseudocode) IF days_from_today(timestamp) == -1 RETURN 'Yesterday' ELSE IF days_from_today(timestamp) == 0…
Ben
  • 4,707
  • 5
  • 34
  • 55
109
votes
13 answers

How do I get the day of the week with Foundation?

How do I get the day of the week as a string?
Moshe
  • 57,511
  • 78
  • 272
  • 425
107
votes
6 answers

Find difference in seconds between NSDates as integer using Swift

I'm writing a piece of code where I want to time how long a button was held down. To do that I recorded an NSDate() when the button was pressed, and tried using the timeIntervalSinceDate function when the button was released. That seems to work but…
Erik
  • 2,299
  • 4
  • 18
  • 23