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
32
votes
6 answers

Truncate NSDate (Zero-out time)

I want to generate a new NSDate with 0 hours, 0 minutes, and 0 seconds for time. The source date can be any random NSDate. Is there a way to achieve this? The documentation did not help me with this. Example Have: 2010-10-30 10:14:13 GMT Want:…
Benjamin
  • 323
  • 1
  • 3
  • 5
32
votes
13 answers

Swift convert time to time ago

In Swift5, we have RelativeDateTimeFormatter Prior to Swift5: I'm trying to convert time to time ago, what i wanna do is: from 1 to 15 seconds it will say " Just now " from 60 minutes to 119 minutes it will say " an hour ago " from 24 hours to 47…
Salah
  • 933
  • 3
  • 13
  • 32
32
votes
4 answers

Convert ISO 8601 to NSDate

I have a timestamp coming from server that looks like this: 2013-04-18T08:49:58.157+0000 I've tried removing the colons, I've tried all of these: Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset? Why…
random
  • 8,568
  • 12
  • 50
  • 85
32
votes
7 answers

How does one subtract hours from an NSDate?

I would like to subtract 4 hours from a date. I read the date string into an NSDate object use the following code: NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; [dateFormatter…
Miriam Roberts
31
votes
5 answers

get time and date by NSDate

how can I retrieve the date and time from a NSDate. I want to see them separately. thanks
Safari
  • 11,437
  • 24
  • 91
  • 191
31
votes
4 answers

NSDate day of the year (swift)

How might the day number of the year be found with swift? Is there a simple way that I'm not seeing, or do I have to find the number of seconds from Jan 1 to the current date and divide by the number of seconds in a day?
Dan Oswalt
  • 2,201
  • 2
  • 14
  • 24
31
votes
3 answers

How to display date with human language like "Today at xx:xx pm", "Yesterday at xx:xx am"?

I have a date "2014-07-02 20:57:38 +0000" and I want to format it as "Today at 8:57 pm". I want that if a string is yesterday, then display it as "Yesterday at 9:00 am". If it is neither today or yesterday, just show the actually date like "27/6 at…
Henry Ngan
  • 572
  • 1
  • 4
  • 24
31
votes
5 answers

Sort array of objects by their NSDate property

Possible Duplicate: How to sort an NSMutableArray with custom objects in it? How does one sort an array of objects by accessing their NSDate properties? I know one can use [mutableArray sortUsingSelector:@selector(compare:)]; on an array of…
some_id
  • 29,466
  • 62
  • 182
  • 304
29
votes
4 answers

NSDateComponents of an NSDate

How do I get the NSDateComponents of a single NSDate? I don't want the components of the difference between 2 dates, just the seconds, minutes, hours, day, month and year of a NSDate?
cfischer
  • 24,452
  • 37
  • 131
  • 214
29
votes
5 answers

NSDate for first day of the month

This is quite a simple concept, but as of yet I have been unable to find an elegant (and calendar locale independent) solution. I need to find the first day of the month for an arbitrary NSDate. For example, given an arbitrary NSDate (arbitraryDate)…
Skoota
  • 5,280
  • 9
  • 52
  • 75
29
votes
5 answers

How to find weekday from today's date using NSDate?

I know that I can figure out today's date by [NSDate date]; but how would I find out today day of the week, like Saturday, Friday etc. I know that %w can be used in NSDateFormatter, but I don't know to use it.
Vibhor Goyal
  • 2,405
  • 2
  • 22
  • 35
29
votes
3 answers

NSDateFormatter not showing full month name, only single digit number

Not sure what the issue is, but instead of getting a month name "July", I get "07". dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setLocale:[NSLocale systemLocale]]; [dateFormat setTimeZone:[NSTimeZone localTimeZone]]; [dateFormat…
Chris
  • 1,421
  • 1
  • 15
  • 21
28
votes
1 answer

How to convert Unix timestamp into Swift NSDate object?

I'm getting dates as Unix timestamps through my API and I want to convert them into NSDate objects in Swift. How can I do that?
dead_man
  • 409
  • 1
  • 6
  • 10
28
votes
4 answers

NSDateFormatter in 12-hour mode

I have the following code. NSDateFormatter *df = ...; [df setTimeZone:[NSTimeZone defaultTimeZone]]; [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"]; NSDate * date = [df dateFromString:date_string]; //here is the problem In 24-hour mode…
Sergey
  • 1,168
  • 2
  • 13
  • 28
27
votes
4 answers

create a NSDate from a string

I have an NSString like this: @"3/15/2012 9:15 PM" and I would like to convert it to NSDate, I have done like this: NSString *str =@"3/15/2012 9:15 PM"; NSDateFormatter *formatter = [[NSDateFormatter alloc]init]; [formatter…
samir
  • 4,501
  • 6
  • 49
  • 76