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

Getting the dates of a week

I need to get all the dates of a week for the current month only when I pressed a specific date. For example, if I pressed Sept 28, the result will be Sept 23-28 dates but if I pressed Sept 30, the result will be Sept 30 ONLY because the dates after…
jay123456
  • 131
  • 1
  • 1
  • 10
1
vote
3 answers

Why date formatter crashes?

My below code is working fine. I don't know why it crashes sometimes(Mostly on application launch. 1 case out of ~100). extension Formatter { static let enUSPOSIX: DateFormatter = { let formatter = DateFormatter() …
amey rane
  • 167
  • 1
  • 8
1
vote
1 answer

Memory problem in NSXMLParser (iPhone)

Hi I'm trying to parse an xml and use the currentElementValue inside a code to get an expiredate. This is the code. if([elementName isEqualToString:@"utlop"]) { NSDate *now = [NSDate dateWithTimeIntervalSinceNow:0]; NSDateFormatter…
Leif
  • 11
  • 1
1
vote
3 answers

how to determine when two NSDates are 1 week apart?

I'm my app I have an NSMutableArray, and each entry has an NSMutableDictionary with several Key-Value pairs. One of these pairs contains the date of the entry. For one of the functions of my app I need to determine which entries are exactly 1 week…
Christian Gossain
  • 5,942
  • 12
  • 53
  • 85
1
vote
1 answer

Constructing date from Int the right way

So i get from the backend this answer : { "SERVERDateTimeGet": { "Return Code" : 0, "Return String" : "No Error", "Year" : 2018, "Month" : 8, "Day" : 29, "Hour" : 14, "Minute" : 16, "Second" : 20, …
ironRoei
  • 2,049
  • 24
  • 45
1
vote
1 answer

Get time difference in iOS when in offline mode

I have an app which works both offline and online. I have a request from the client to embed a functionality which will disallow the user from using certain functionality after some days, 7 days, for example. If online I can easily achieve this…
skJosh
  • 631
  • 1
  • 8
  • 16
1
vote
1 answer

Get date in current time zone in objective - c?

NSArray *fromD = [setFreeHour valueForKey:@"fromDate"]; NSString *fromdate = fromD[0]; NSDateFormatter *fromDateFormatter = [[NSDateFormatter alloc]init]; [fromDateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; NSDate *fromDate =…
Mukie
  • 129
  • 1
  • 2
  • 9
1
vote
2 answers

Most efficient way to convert a NSDate object with a given time to one with the same time and the current date

I'm currently creating a scheduling application that deals with schools schedules. Each schedule has an array of period objects that contains both a start and an end time. Currently I have these times stored as NSDate objects, where only the time is…
ecbtln
  • 2,646
  • 5
  • 27
  • 35
1
vote
1 answer

iphone dateFromString giving warning message 'NSDate' may not respond to '+ variable name'

warning: 'NSDate' may not respond to '+currDate' Above is a warning message when I compile the following code: -(IBAction)getDate:(id)sender { currDate = [NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; […
cruzzzin
  • 33
  • 1
  • 3
1
vote
1 answer

How can I use the Date() function and time stamps from when the app is in the foreground and background to get elapsed time?

I was thinking about using DataFormatter.localizedString... and setting the value to 2 different variables, one for when the app leaves the foreground and when the app comes back to the foreground, and then subtracting them to get the elapsed time…
YungGoat
  • 123
  • 1
  • 9
1
vote
1 answer

Convert NSString Value into Date Returns nil

After converting NSString into Date returns nil. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MM-dd-yyyy hh:mm:ss"]; strDate = @"05-18-2017 23:03:24"; NSDate…
kiran
  • 4,285
  • 7
  • 53
  • 98
1
vote
1 answer

Date not getting the current time of the phone Swift 4

I have this function that calculate the amount of time from the current minute of the phone. I am trying to have a timer go off after this specific amount of time. The way this function is called is through a switch that the user flips in the app…
Via
  • 13
  • 4
1
vote
1 answer

In Objective-C how to get NSDate with milliseconds? dateFromString: method is ignoring milliseconds

I have used following code: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.timeZone = [NSTimeZone localTimeZone]; dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]; dateFormatter.dateFormat =…
1
vote
1 answer

NSDate won't load/display correctly

I'm not sure what's going on with this. I'm trying to load an NSString* object from a file, convert it to an NSDate* with a date formatter, and then convert the hour and minute components back to NSString so I can display a time in Interface…
Nicholas1024
  • 880
  • 1
  • 9
  • 15
1
vote
1 answer

when to set the NSLocale for the NSDateFormatter ?

In my app , Im using following code to convert string to date before inserting the date into the database. However this code fails for the users in UK, they have the Region set to UK, and Timezone set to London. This works for the users in the US…
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109