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

How Do I write a Timer in Objective-C?

I am trying to make a stop watch with NSTimer. I gave the following code: nst_Timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(showTime) userInfo:nil repeats:NO]; and it is not working in milliseconds. It takes…
new_programmer
  • 840
  • 3
  • 12
  • 22
39
votes
8 answers

Objective-C, How can I get the current date in UTC timezone?

I am trying: NSDate *currentDateInLocal = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:SS.SSS'Z'"]; NSString *currentLocalDateAsStr = [dateFormatter…
williamsandonz
  • 15,864
  • 23
  • 100
  • 186
39
votes
8 answers

NSDateFormatter, am I doing something wrong or is this a bug?

I'm trying to print out the date in a certain format: NSDate *today = [[NSDate alloc] init]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyyMMddHHmmss"]; NSString *dateStr = [dateFormatter…
rustyshelf
  • 44,963
  • 37
  • 98
  • 104
38
votes
5 answers

NSString to NSDate

I got a string that contains the current date by using this : NSString *date = [[NSDate date] description]; At a different point I want to retrieve the date from this string and I used the following code: [NSDateFormatter…
Minar
  • 483
  • 1
  • 6
  • 13
37
votes
8 answers

How do I get weekday and/or name of month from a NSDate variable?

Alright. The problem we're having is that we have NSStrings filled with dates in the format of yyyyMMdd and what we want to do is to get the current weekday and name of month. The function dagOmvandlare converts the datestring to weekday and month.…
Jens Bergvall
  • 1,617
  • 2
  • 24
  • 54
36
votes
6 answers

NSDate in full style but without year

I'm working with Objective C for iPhone and have a NSDate that I want to display in full style but without the year. Right now I'm using the code below but it also show the year, and I don't want that. As I want to show the date in the right region…
Martin
  • 2,269
  • 3
  • 19
  • 10
36
votes
7 answers

NSDateFormatter "Month" in 3 letters instead of full word

NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"dd-MM-YYYY HH:mm"]; [formatter setTimeZone:[NSTimeZone systemTimeZone]]; If i choose MM i get the month in number: 09-05-2012 15:33 If i…
bruno
  • 2,154
  • 5
  • 39
  • 64
35
votes
6 answers

How to compare two NSDate objects in objective C

I have objects of Date type. I want to compare them, and I have written an if condition in the following way: if (([startDate1 isEqualToDate:[self getDefaultDate]]) || (startDate1 != [ self getDefaultDate] && m_selectedDate >= m_currentDate1 &&…
Ranjit
  • 4,576
  • 11
  • 62
  • 121
35
votes
1 answer

Can't pass Date to NSPredicate(format: ...) without "as CVarArg"

Is this how I'm supposed to pass a Date to NSPredicate.init(format predicateFormat: String, arguments argList: CVaListPointer). let endDate = Date() NSPredicate(format: "endDate == %@", endDate as CVarArg) It looks kinda clumsy, and I suspect I'm…
netdigger
  • 3,659
  • 3
  • 26
  • 49
35
votes
11 answers

Converting date between timezones swift

I have a date stored on my online server database which is in GMT. I load the date and convert it to the user's timezone using the following code : if let messagedate = oneitem["timestamp"] as? String { let dateFormatter = NSDateFormatter() …
Alk
  • 5,215
  • 8
  • 47
  • 116
35
votes
3 answers

Get current iPhone device timezone date and time from UTC-5 timezone date and time iPhone app?

In my iPhone application i am very struggling to convert the webservice response time to current device time. The webservice time in UTC-5 timezone. The app is worldwide use app. So i have to convert the response time to current device timezone.…
Gopinath
  • 5,392
  • 21
  • 64
  • 97
34
votes
2 answers

iPhone iOS 4 addTimeInterval deprecated

I'm using addTimeInterval for creating local notification but it seems that it is now deprecated (iOS 4). My code: localNotif.fireDate = [now addTimeInterval:timeInterval]; Xcode's warning: 'addTimeInterval:' is deprecated (declared at…
Iñigo Beitia
  • 6,303
  • 4
  • 40
  • 46
34
votes
4 answers

How to get the date without Time from NSDate?

I want to get the date from NSDate and do not need time. Example: NSDate* today = [NSDate date]; // want to give 20-06-2012 only I want to get the date for today without showing the time and use it in NSDate variable or string variable.
Ghass.64
  • 365
  • 1
  • 4
  • 8
32
votes
14 answers

Fuzzy date algorithm

I'm looking for a fuzzy date algorithm. I just started writing one and realised what a tedious task it is. It quickly degenerated into a lot of horrid code to cope with special cases like the difference between "yesterday", "last week" and "late…
Rog
  • 17,070
  • 9
  • 50
  • 73
32
votes
2 answers

Converting a date format in objective C

How can i convert the following date "Sun Jul 17 07:48:34 +0000 2011" to the following format "2011-07-17 07:48:34"? I used NSDateFormatter as shown below but it didnt work. It gives null as a result. NSDateFormatter *objDateFormatter =…
farajnew
  • 926
  • 2
  • 9
  • 15