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
1
vote
3 answers

Is my Contact's birthday in next 10 days

I am trying to calculate whether any of my Address Book's contacts have a birthday in the next 10 days. There's plenty of code on line to compare dates, but I only want to compare day and month. For example, if a contact was born 05/01/1960 (and…
Jeremy
  • 883
  • 1
  • 20
  • 41
1
vote
2 answers

Misaligned pointer use with std::shared_ptr dereference

I am working in a legacy codebase with a large amount of Objective-C++ written using manual retain/release. Memory is managed using lots of C++ std::shared_ptr, with a suitable deleter passed in on construction that calls…
1
vote
1 answer

NSDateFormatter dateFromString results don't make sense to me

The following code: NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease]; dateFormatter.dateFormat = @"yyyy-MM-dd"; NSDate *tempDate = [dF2 dateFromString:@"2011-07-10"]; DebugLog(@"Temp Date %@", tempDate); Results in the…
purplehey
  • 127
  • 1
  • 12
1
vote
1 answer

`[[NSDate date] timeIntervalSince1970]` in (C) free function always returns same value

If I have: double now_ms() { return 1000 * (float)[[NSDate date] timeIntervalSince1970]; }; : NSLog( @"%f", now_ms() ); // inside a CADisplayLink tick-event It gives the same value each time. I can fix this by making a Lambda: double…
P i
  • 29,020
  • 36
  • 159
  • 267
1
vote
3 answers

How to get DD/MM or MM/DD through Date Picker in IPhone

I need only DD/ MM in my picker for getting Birthday I am capable to get MM/DD/YYYY through this code datepickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 0, 0)]; datepickerView.datePickerMode = UIDatePickerModeDate; …
Vipin_iOSdev
  • 167
  • 1
  • 2
  • 7
1
vote
1 answer

Convert NSString(2011-04-18 18:57:47.453000) to NSDate

Hey, guys i searched a lot but cant get my code working. I am getting a date in form of a string with format like : 2011-04-18 18:57:47.453000 I want to convert this to NSDate to calculate time elapsed, but I cant seem to get the conversion right.…
NSRover
  • 932
  • 1
  • 12
  • 29
1
vote
2 answers

Representing hourly ranges in Objective-C

I'm trying to create a class that represents hours of operation in an Obj-C program. I have been considering using NSDatesComponent to do this, but I feel there should be an easier way. Does anyone have any tips as to how to do this? In Core Data, I…
Apophenia Overload
  • 2,485
  • 3
  • 28
  • 46
1
vote
2 answers

Detect numeric year from string using Swift

I am looking for a way to detect years e.g. 2019. The requirements I think would be that the numbers are in a row, have four digits and are not adjacent to letters or special characters. So I'd like to get the method to return "2019" in each of the…
user6631314
  • 1,751
  • 1
  • 13
  • 44
1
vote
2 answers

NSCalendar add half a month to an NSDate

I'm trying to determine payment schedules when there are 24 payments per year (i.e. twice per month). The payments aren't going to always be 1st and 15th. I have to start with a given day, and then calculate twice monthly payments starting from…
Kenny Wyland
  • 20,844
  • 26
  • 117
  • 229
1
vote
3 answers

Problem with parsing NSString to NSDate

I have XML with this value: 2011-04-02T00:00:00 I am trying to parse this on iPhone from NSString to NSDate but have no luck. NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease]; …
1110
  • 7,829
  • 55
  • 176
  • 334
1
vote
0 answers

NSDate, timezones and Unix timestamp confusion

I took over the project of some programmers who unfortunately left a lot of stuff unclear in the code. Basically we have stored some flight events in our database with a departure and arrival time. It the database (as far as we can tell) everything…
MichiZH
  • 5,587
  • 12
  • 41
  • 81
1
vote
2 answers

iPhone dev: Trying to compare current date and date of array element

I have a working UITable with sections. The UITable gets its data from an external XML file this also works good. But what I want is that the XMLparser excludes the rows where the date is older then today. I thought to do this at the moment where my…
iJar
  • 147
  • 2
  • 15
1
vote
1 answer

NSCalendar components not taking time into account

We know that NSDate is UTC/GMT. In order words, no time zone info is associated with it. There are high level classes such as NSCalendar and NSTimeZone and NSDateComponents which do take timezone into account. Based on that, I am using the following…
vance
  • 1,071
  • 11
  • 19
1
vote
3 answers

How do I get the NSDate plist representation programmatically?

Since plist is xml, which is text, when an NSDate object is written into a plist, the result is something like this:
executor21
  • 4,532
  • 6
  • 28
  • 38
1
vote
2 answers

Calling a Method at NSDate

I'm trying to call a method at a certain time and date (using an NSDate) but I can't work out how to do it. I don't want to use NSTimer because that becomes paused when the app goes into the background or is closed. I don't want to run one in the…
Baza207
  • 2,123
  • 1
  • 22
  • 40