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
16
votes
1 answer

Escape NSDateFormatter String

How to escape characters from NSDateFormatter format? NSDate *currentDate = [NSDate date]; NSDateFormatter *dateFormater = [[NSDateFormatter alloc] init]; [dateFormater setDateFormat:@"dd/MM/yyyy \\a\\t HH:mm"]; NSString *dateFormated =…
Luciano Nascimento
  • 2,600
  • 2
  • 44
  • 80
16
votes
5 answers

Core Data Predicate Date Comparison

Im trying to fetch all the objects in an entity matching a user selectedDate (it's an NSDate). The Core Data code is fine but my predicate keeps returning 0 results, the dates are the same in the database as the user is selecting. How should the…
user2228755
  • 397
  • 1
  • 2
  • 10
16
votes
7 answers

How to set seconds to zero for NSDate

I'm trying to get NSDate from UIDatePicker, but it constantly returns me a date time with trailing 20 seconds. How can I manually set NSDate's second to zero?
deukyun
  • 163
  • 1
  • 6
16
votes
1 answer

change NSDate from one time zone to another

Given a date in NSString like "2012-12-17 04:36:25" (which is GMT) how one can simply change it to other time zones like EST, CST All the steps I saw so far took so many unnecessary steps
shebelaw
  • 3,992
  • 6
  • 35
  • 48
16
votes
9 answers

Convert date in MM/dd/yyyy format in xcode?

I have a UIDatePicker and I m getting the selected date from it in yyyy-MM-dd format.Now I want to convert it to MM/dd/yyyy format ..How can i do it ? NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease]; df.dateFormat =…
Honey
  • 2,840
  • 11
  • 37
  • 71
15
votes
2 answers

Why isn't my time zone being saved into my NSDate?

I must initialize an NSDate object from NSString in objective-c. I do it like this: NSString *dateString = [[webSentence child:@"DateTime"].text stringByReplacingOccurrencesOfString:@"T" withString:@" "]; NSDateFormatter *dateFormatter =…
Tom
  • 3,899
  • 22
  • 78
  • 137
15
votes
3 answers

Compare two NSDates for same date/time

Is date1 == date2 not a valid way to compare? If not, what is the correct alternative? Here's my code: - (NSDate*) dateWithNoTime { unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSCalendar* calendar =…
Greg
  • 34,042
  • 79
  • 253
  • 454
15
votes
2 answers

How to check if 24 hours have passed in Swift

I have to implement in my app where after a user had saved his recording, I will have to determine if 24 hours have passed from the creation date of that recording. So far what I have at the moment is just to determine if current date is not equal…
15
votes
3 answers

Find out if an NSDate is today, yesterday, tomorrow

With the iOS SDK I need to find an easy and secure way to see if an NSDate is today, yesterday, tomorrow. What I'm looking for is something like this in pseudo code: NSDate *myDate = someDate; if ([myDate isTomorrow]) { NSLog("Tomorrow");…
mrrmatinsi
  • 283
  • 1
  • 6
  • 11
15
votes
2 answers

Setting an NSDate to specific date, time and timezone

I need to set a NSDate to a specific date, time and timezone for an iPhone App. The example code below works fine on iOS 4 as the setTimeZone was introduced with iOS 4. How could I easily set a NSDate to a date, time and timezone without using…
Structurer
  • 694
  • 1
  • 10
  • 23
15
votes
2 answers

Using core plot for iPhone, drawing date on x axis

I have available an array of dictionary that contains NSDate and NSNumber values. I wanted to plot date on X axis. For plotting I need to supply xRanges to plot with some decimal values. I don't understand how can I supply NSDate values to xRange…
xmax
  • 231
  • 1
  • 3
  • 8
15
votes
7 answers

Set NSDate 0 seconds in swift

I'm trying to get NSDate from UIDatePicker, but it constantly returns me a date time with trailing 20 seconds. How can I manually set NSDate's second to zero in swift?
aftab ahmed
  • 181
  • 1
  • 1
  • 6
15
votes
5 answers

Create NSDate Monotouch

I am trying to take a date string and turn it into a specific NSDate (eg. July 1, 1981), but I don't see and methods for setting the date. Does anyone know how to accomplish this? Perhaps convert a DateTime object to NSDate?
Bryan
  • 17,201
  • 24
  • 97
  • 123
15
votes
5 answers

NSString to NSDate for yyyy-MM-dd'T'HH:mm:ss.SSS+05:30 format

My date string that I am getting is 2014-01-08T21:21:22.737+05:30 of this format. How do i confer it to NSDate? I tried: NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30"; [dateFormatter…
kagmanoj
  • 5,038
  • 5
  • 19
  • 21
15
votes
7 answers

iOS : Switching between 12/24 hour times from strings

Interesting issue thats caught me out. I receive string times from a server to a device. which I then convert in to a NSDate. When the device was set to displaying 24hour times, life was good. Now I am testing it on a device set to 12hour times.…
Burf2000
  • 5,001
  • 14
  • 58
  • 117