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
23
votes
4 answers

Subtract minutes from NSDate

I want to subtract some minutes 15 min 10 min etc., And i am having date object with time now i want to subtract minutes.
Ishu
  • 12,797
  • 5
  • 35
  • 51
23
votes
4 answers

NSDateFormatter detect 24-hour clock in OS X and iOS

I would like to check if the user has selected the 12-hour or 24-hour clock as their preference in OS X and iOS. So I would like to detect if the user has done the following: On the Mac, the System Preference is in Date & Time, Use a 24-hour…
wigging
  • 8,492
  • 12
  • 75
  • 117
23
votes
4 answers

NSDate with date component from one and time component from another NSDate

I have two UIDatePicker controls in my app, one configured with UIDatePickerModeDate and one with UIDatePickerModeTime. I want to configure a NSDate object with the date from UIDatePicker one and the time from the second one ... how? Thanks, Mark
Cornelius
  • 4,214
  • 3
  • 36
  • 55
23
votes
5 answers

How can i get next date using NSDate?

Possible Duplicate: iOS and finding Tomorrow How can i get next date using NSDate. Please send me the solution.
Jai
  • 417
  • 1
  • 5
  • 12
22
votes
3 answers

Nsdateformatter returns me day in language of the phone selected. Can it return me only English all the time?

I am using dateformatter to get days and time in the my application. But I am facing an issue when I change the language of the phone dateformatter returns me day and time of the selected language of the phone due to which my app crashes as we are…
breakfreehg
  • 618
  • 1
  • 6
  • 16
22
votes
5 answers

Swift NSDateFormatter not using correct locale and format

This is my code: let currentDate = NSDate() let usDateFormat = NSDateFormatter() usDateFormat.dateFormat = NSDateFormatter.dateFormatFromTemplate("d MMMM y", options: 0, locale: NSLocale(localeIdentifier: "en-US")) cmt.date =…
Lord Vermillion
  • 5,264
  • 20
  • 69
  • 109
22
votes
7 answers

Nil NSDate when trying to get date from UTC string in zulu time

Writing an iPhone app in Objective-C, I have a date in string form (in UTC format, with a Z on the end to denote zero UTC offset, or zulu time), which I need to parse into an NSDate object. A bit of code: NSDateFormatter* df = [[NSDateFormatter…
Eric Freese
  • 1,485
  • 2
  • 13
  • 25
21
votes
3 answers

Convert a NSDate to milliseconds epoch time

I need to be able to convert a date to a time stamp, an epoch in milliseconds. All I see online are for converting milliseconds to NSDate and not the other way round. Any help out there?
bakwarte
  • 317
  • 1
  • 2
  • 10
21
votes
3 answers

add NSTimeInterval to NSDate in cocoa

I've got a NSDate that represents a specific time. The format of that date is hhmmss. I want to add an NSInterval value (specified in seconds) to that time value. Example: NSDate = 123000 NSTimeInterval = 3600 Added together = 133000 What…
l Beursgens
21
votes
4 answers

NSDate subtract one month

I want two NSInteger year, month to go one month back. e.g. now is 2011-01 how can I get 2010-12. adding is one thing, but I want to subtract. NSDate *date = [NSDate date]; NSDateComponents *dateComponents = [calendar components:unitFlags…
endo.anaconda
  • 2,449
  • 4
  • 29
  • 55
21
votes
1 answer

When does dateByAddingComponents:toDate:options return nil?

I've been using dateByAddingComponents:toDate:options: and dateByAddingUnit:value:toDate:options: and using optional binding to get the date out of it. Like this: guard let startOfNextMonth = calendar.dateByAddingComponents(oneMonthComponent,…
NickCE
  • 409
  • 3
  • 7
20
votes
9 answers

How can I format a date in Objective-C similar to the jquery.timeago library?

I have a feed of items displayed in table cells, part of which is a date / timestamp in the past. In Objective-C, how can I accomplish formatting them in the same manner as the jquery.timeago plugin on the web? That is, taking in a date and…
barfoon
  • 27,481
  • 26
  • 92
  • 138
20
votes
3 answers

iOS Swift 3 : Convert "yyyy-MM-dd'T'HH:mm:ssZ" format string to date object

Hello i have a dictionary self.publishedAt = dictionary["publishedAt"] as? NSString in which i'm getting date "2017-01-27T18:36:36Z". I want to convert it in readable format : dd-MM-yyyy hh:mm:ss. i tried via let dateFormatter =…
Abhishek Thapliyal
  • 3,497
  • 6
  • 30
  • 69
20
votes
3 answers

NSDate timeIntervalSince1970 not working in Swift?

I am doing this in swift: let date = NSDate(timeIntervalSince1970: 1432233446145.0) println("date is \(date)") The log gives me this: date is 47355-09-02 23:55:45 +0000 Then I try to get an interval back out just for testing: let tI =…
zumzum
  • 17,984
  • 26
  • 111
  • 172
20
votes
1 answer

Hour in NSDateComponent in 12 or 24h format?

Is the hour component of NSDateComponent in a 12h or 24h format? I cannot find anything about this in the documentation...
Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72