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
19
votes
3 answers

Getting date from [NSDate date] off by a few hours

I am using NSDate *date = [NSDate date]; for getting the date, but the date I get is off by 2 hours.
YosiFZ
  • 7,792
  • 21
  • 114
  • 221
19
votes
1 answer

NSDateFormatter and Time Zone issue?

I have a string with time in GMT and i want to make it according to the system time zone but its not working properly - NSLog(@"Time Str = %@",Time); NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat…
saadnib
  • 11,145
  • 2
  • 33
  • 54
19
votes
4 answers

String description of NSDate

I have the following code: [ [NSDate date] descriptionWithLocale: @"yyyy-MM-dd" ] I want it to return a date in the following format: "2009-04-23" But it returns: Thursday, April 23, 2009 11:27:03 PM GMT+03:00 What am I doing wrong? Thank you in…
Ilya Suzdalnitski
  • 52,598
  • 51
  • 134
  • 168
19
votes
4 answers

Date._unconditionallyBridgeFromObjectiveC(NSDate?) crash in Swift 3

I have following function in a swift file. I am calling this from Obj C file with NSDate in place of startDate. And quite often, not every time, my app crashes with Date._unconditionallyBridgeFromObjectiveC(NSDate?) How can I fix this? func…
Vinuta
  • 575
  • 4
  • 18
19
votes
1 answer

How to convert "2017-01-09T11:00:00.000Z" into Date in Swift 3?

My problem is that the date is nil. My code looks like print(article_date) // output "2017-01-09T11:00:00.000Z" as string let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US_POSIX") dateFormatter.dateFormat =…
Lyndon King McKay
  • 647
  • 2
  • 7
  • 10
19
votes
2 answers

Making Date With Given Numbers

I have the following Swift (Swift 3) function to make a date (Date) with date components (DateComponents). func makeDate(year: Int, month: Int, day: Int, hr: Int, min: Int, sec: Int) -> NSDate { let calendar = NSCalendar(calendarIdentifier:…
El Tomato
  • 6,479
  • 6
  • 46
  • 75
19
votes
1 answer

Finding the difference in time between two NSDates Swift

I have two NSDate objects: startDate and endDate, both are NSDate objects. I want to find the difference between these two dates. I know there is a resolved question about this already (Getting the difference between two NSDates in…
ButtonMasterBot
  • 317
  • 2
  • 5
  • 14
19
votes
2 answers

Do an action when a sound has finished playing in AVAudioPlayer?

I am using the AVAudioPlayer framework, and I have several sounds that play one at a time. When a sound is finished playing, I want the application to do something. I tried to use audioPlayerDidFinishPlaying to do the action at the end of the first…
Evelyn
  • 2,588
  • 3
  • 22
  • 47
18
votes
3 answers

Unable to format UTC date with NSDateFormatter

I have the following date: 2011-10-20T01:10:50Z I would like it to be formatted to "M/d/yy 'at' h:mma" Here is my code: NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; //The Z at the end of your string represents Zulu which…
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
18
votes
1 answer

How to properly convert the Last-Modified header from an HTTP response to an NSDate on iOS

I am looking for a fully working solution, one that works with: any iOS locale or timezone any/most HTTP servers Xcode 4.0.2 (see why) Current broken code: NSString lastModifiedString = @"Mon, 06 Jun 2011 12:47:05 GMT"; NSDateFormatter *df =…
sorin
  • 161,544
  • 178
  • 535
  • 806
18
votes
1 answer

NSDateComponents returning nil

I'm trying to get an NSDate object with the hour, minute, and second initialized to 0. I'm using the code below to get it, but for some reason I get nil from [NSDateComponents date]. What's wrong with my code? NSCalendar *gregorian = [[[NSCalendar…
Ovesh
  • 5,209
  • 11
  • 53
  • 73
18
votes
2 answers

iphone how can i get month name from month number?

Possible Duplicate: iOS: How to get a proper Month name from a number? How can I get month name from month number? I have month number like 02, but would like the month as a string, in this case February.
user564968
18
votes
1 answer

DateFormatter's returns nil for specific date strings without time in Swift

I really don't get what is happening with this code, I'm trying to convert a string to date. What I don't understand is that the conversion works for most of the dates, but doesn't work specially for only 2 dates. let dateFormatter =…
Glauco Neves
  • 3,483
  • 1
  • 24
  • 36
18
votes
5 answers

Swift ios date as milliseconds Double or UInt64?

I'm not iOS developer but started learning Swift. I try to convert some logic from Android project to iOS. I have the following method: func addGroupItemSample(sample : WmGroupItemSample){ // some custom class var seconds: NSTimeInterval =…
snaggs
  • 5,543
  • 17
  • 64
  • 127
17
votes
1 answer

Why NSDateFormatter can not parse date from ISO 8601 format

Possible Duplicate: Converting an ISO 8601 timestamp into an NSDate: How does one deal with the UTC time offset? I use rails as backend, the default date output is 2008-12-29T00:27:42-08:00 But after my research NSDateFormatter can not support…
allenwei
  • 4,047
  • 5
  • 23
  • 26