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

POST with JSON in format DateTime

Im struggling a lot with how I can format a NSDate to POST it as JSON to REST service .net. The accepted format is: "Date":"\/Date(459842400000+0200)\/" I have no problem with the response and to parse it to NSDate. I use JSONFramework for…
Silversnail
  • 386
  • 1
  • 6
  • 23
1
vote
2 answers

NSDateFormatter return null

I'm trying to format a NSDate to a string with date format of yyyyMMdd, here's the code NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyyMMdd"]; NSDate *date…
Viken Ong
  • 109
  • 2
  • 10
1
vote
6 answers

convert string into NSDate

i have a "DateTimeArray" this array contain DateTimeArray[index 0] = 20.05.2011 12:12:50 DateTimeArray[index 1]= 20.05.2011 12:13:20 DateTimeArray[index 2]= 20.05.2011 12:20:10 all the value are in string,and i want to convert this…
Pooja
  • 2,162
  • 5
  • 33
  • 64
1
vote
3 answers

Getting incorrect date format after converting

I'm getting this date from API in string format: "2020-01-02T00:00:00". Now I wanted to convert this date into Date format. So this is what I did for that... var utcTime = "\(dic["Due_Date"]!)" //Printing `utcTime` gives…
test Name
  • 23
  • 3
1
vote
2 answers

Consistent 1-Second-Long lag at 0:02 during NSTimer in "stopwatch-like" app

I have an app that uses a stopwatch-style count up from 0 in HH:mm:ss format. The code looks pretty straightforward to me, and I can't think of a more efficient way to run it. For some reason, when I run it, there is a very noticeable and…
Adam
  • 11
  • 1
1
vote
1 answer

Inconsistent NSDateFormatter result for identical string format

Why does this conversion work some of the time and not others? When it doesn't work, eDate is null. The original conversion fails.. yet the formats between a working & a non-working date string look identical. I tried adding an extra "d" in both…
quantumpotato
  • 9,637
  • 14
  • 70
  • 146
1
vote
2 answers

iOS: problem with NSDate and NSDateComponent

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setTimeZone:[ NSTimeZone timeZoneForSecondsFromGMT:(+0*3600) ] ] ; [components setYear:2011]; [components setDay:13]; [components setMonth:5]; NSDate *date1 =…
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
1
vote
1 answer

NSFetchRequest predicate to sort by Date, using time component only

Think of the Alarm app on the iPhone, the alarms are sorted by time. Say these alarms were stored in Core Data with the date component being the actual date they were created, but with the time component chosen by a date picker. So when fetching…
tariq
  • 501
  • 2
  • 4
  • 11
1
vote
1 answer

iOS TimeZone get three letter abbreviation

I need to get three/four letter abbreviation for the country called 'Africa/Nairobi', I have tried using print(TimeZone(abbreviation: "SAST")?.identifier) from the NSTimeZone.abbreviationDictionary, results nil. How to get the three/four letter…
1
vote
0 answers

iOS - Date prints the day before

So i am trying to format a date(in string) and get the dat from it. This is my code: var cycleDate = "01.10.19" func getDate(date: String) -> Date { let dateFormatter = DateFormatter() dateFormatter.dateFormat = "dd-MM-yy" let date =…
ironRoei
  • 2,049
  • 24
  • 45
1
vote
3 answers

static var or AppDelegate

I have to use a NSDate var in my program, and that var will be dealloc and realloc (I must add some month and year to this date and there is no other possibilities). That var has to be user in many methods and I want to put this var in global. Is…
clement
  • 4,204
  • 10
  • 65
  • 133
1
vote
1 answer

Subtract date & time

I have two date-time strings, but how can I subtract them? I tried to use the -[NSDate timeIntervalSinceDate:] method, but I can't get it to work. I want the exact hh:mm:ss difference. If the situation is like this: my first time string is 2011/4/28…
HML
  • 103
  • 1
  • 9
1
vote
1 answer

NSDate in specific format

How do I get Todays date in "April 27" this format and compare with same string in iphone sdk.
Arvind
  • 543
  • 1
  • 5
  • 17
1
vote
2 answers

obj-c: how to parse a "human friendly" date?

I've read a many things about parsing date in obj-c, but I can't find anything dealing with dates like "Mon, 25 Apr 2011 11:53 am CDT"... I'd like to convert it to NSDate in a smart way but I'm running out of ideas. Thanks.
Francois
  • 10,730
  • 7
  • 47
  • 80
1
vote
2 answers

How to reorder an array by NSDates and NSStrings

I have an array that contains a custom class of mine called 'Game'. Each 'Game' contains an NSDate. I would like to reorder my Array based on the dates so that they are in order from newest to oldest. How could I do that? Also, my class 'Game'…
Blane Townsend
  • 2,888
  • 5
  • 41
  • 55