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
17
votes
2 answers

Get NSDate from NSDate adjusted with timezone

I have a NSDate object. Let's say it represents "1-10-2011" NSDate *date = [df dateFromString:@"2011-10-01 00:00:00"]; That date translates into "2011-09-30 22:00:00" because of my timezone. Question: How do I get a new Date object representing…
Tycho Pandelaar
  • 7,367
  • 8
  • 44
  • 70
17
votes
1 answer

NSDate from NSDateComponents

I am trying to alter the hh, mm, ss component of an NSDate to that of another NSDate - but for some reason, the time gets set to midnight for some reason. I can't figure out why! Here is the code; + (NSDate *) fixTime:(NSDate*)fromDate …
Sam
  • 827
  • 4
  • 9
  • 21
17
votes
4 answers

NSDateFormatter return wrong date + Swift

Code : let dateString = "2016-04-02" var formatter: NSDateFormatter = NSDateFormatter() formatter.timeZone = NSTimeZone(abbreviation: "GMT +3:00") formatter.dateFormat = "yyyy-MM-dd" println("dateString: \(dateString)") …
Sanjay Kumar
  • 415
  • 4
  • 9
  • 19
17
votes
2 answers

convert epoch time to NSDate in cocoa/iPhone

I have the value of the epoch time like say 123456789, now I want to convert this into NSDate in cocoa framework. Can anyone show me? thanks
Raja
  • 6,354
  • 9
  • 30
  • 34
17
votes
7 answers

NSDateFormatter dateFromString Always Returns nil

I want to apologize ahead of time for asking a repeat question, but none of the other solutions have worked for me yet. Every time I try to pass a date string to the dateFromString function I get nil. I haven't read anywhere that things have changed…
Macros185
  • 231
  • 1
  • 3
  • 9
17
votes
2 answers

Converting a gregorian date string to Islamic date gives correct & incorrect results

I have the following two date strings: (1) 24/04/2013 and (2) 19/03/2013 I'm trying to convert these dates into Islamic (Um Al Qura) dates, I'm using this code block to do so: NSDateFormatter *df = [[NSDateFormatter alloc] init]; …
JAHelia
  • 6,934
  • 17
  • 74
  • 134
17
votes
8 answers

Get firstdate, lastdate of month?

I want to get firstdate、lastdate of month,I try NSDateComponents *components = [calendar components:units fromDate:[NSDate date]]; [components setDay:1]; self.currentDate = [calendar dateFromComponents:components]; int m = components.month; int y =…
Alice Chen
  • 231
  • 1
  • 2
  • 11
16
votes
7 answers

NSTimeInterval to NSDate

How can I convert a NSTimeInterval to NSDate? Think of it like a stopwatch. I want the initial date to be 00:00:00, and I have a NSTimeInterval of X seconds. I need to do it like this because the NSTimeInterval needs to be converted to an int by…
Baub
  • 5,004
  • 14
  • 56
  • 99
16
votes
5 answers

Sort NSArray of custom objects by their NSDate properties

I am attempting to sort an NSArray that is populated with custom objects. Each object has a property startDateTime that is of type NSDate. The following code results in an array, sortedEventArray, populated but not sorted. Am I going about this the…
markdorison
  • 139,374
  • 27
  • 55
  • 71
16
votes
3 answers

Date/Time parsing in iOS: how to deal (or not deal) with timezones?

I have an ASP.NET MVC 3 website that communicates with my iOS app via JSON. As part of the objects sent in the JSON response, I have dates in the format of yyyy-MM-dd HH:mm:ss ZZZ which outputs 2011-04-05 16:28:22 -07:00. How do I parse that in…
Gup3rSuR4c
  • 9,145
  • 10
  • 68
  • 126
16
votes
2 answers

how can I add one minutes in current NSDate of iphone and save in NSDate?

I can get current date of iphone via this code as NSDate *currentDate = [NSDate date]; NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm"]; NSString *dateString = [dateFormatter1…
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154
16
votes
2 answers

Swift convert NSTimeInteval to NSDate

I've been following an Objective-C tutorial and the tutor is able to cast an NSTimeInterval object to a NSDate object. The lesson uses CoreData, and stores the date of a post as an NSTimeInterval, later on we want to retrieve that interval and set…
Halfpint
  • 3,967
  • 9
  • 50
  • 92
16
votes
5 answers

Modifying NSDate to represent 1 month from today

I'm adding repeating events to a Cocoa app I'm working on. I have repeat every day and week fine because I can define these mathematically (3600*24*7 = 1 week). I use the following code to modify the date: [NSDate…
bmalicoat
  • 2,508
  • 3
  • 24
  • 23
16
votes
4 answers

Parsing a RFC 822 date with NSDateFormatter

I'm using a NSDateFormatter to parse a RFC 822 date on the iPhone. However, there is no way to specify optional elements in the date format. There are a couple of optional parts in the RFC 822 specification which is breaking the date parser. If…
Anurag
  • 140,337
  • 36
  • 221
  • 257
16
votes
2 answers

Get all days of any month with objective-c

A seemingly simple question...how can I return a list of days for any specified month? NSDate *today = [NSDate date]; //Get a date object for today's date NSCalendar *c = [NSCalendar currentCalendar]; NSRange days = [c rangeOfUnit:NSDayCalendarUnit…
rson
  • 1,455
  • 2
  • 24
  • 43