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
0 answers

date(byAdding: ) is returning the incorrect value in Swift 4

I have the following code: let defaults = UserDefaults.standard if let dateOpened: Date = defaults.object(forKey: "lastOpened") as? Date { let (intHour, intMinute, time1) = getStuffFromTime(time: alarms[i].time, amOrPm: alarms[i].amOrPm) //…
LFHS
  • 295
  • 1
  • 2
  • 15
1
vote
0 answers

Date Formatter failing on a correct date

I have a function that parses a String to Date. The function is working correctly on all dates except "26-3-2017". If I manipulate the date and make it "26-3-2016" everything works correctly. Even more, I tried the function on an array of dates it…
Tarek A.
  • 218
  • 2
  • 12
1
vote
2 answers

NSDateformatter stringFromDate returns wrong time string in iOS 10.3.1simulator

I have this method that converts date to string: - (NSString*)getDateTimeBasedOnDeviceTimeFormat:(NSDate *)theDate{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; NSDate *dateSource; NSString *dateStr; …
Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109
1
vote
1 answer

dateFromString returns nil on iOS 4.2, works fine on 3.2!

I am converting a string returned from server to NSDate object for further use. Here is the sample string from server: 2011-01-14T16:05:48.555+05:00 And, I use [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; I am able to successfully convert…
Imran Raheem
  • 890
  • 8
  • 25
1
vote
2 answers

How to convert date "MM-dd-yyyy hh:mm:ss a" into text format?

I am trying to create a function which will convert date into text format like "Just now, 2mins, 1hour, 1day, oct 10". Here is my sample code where I am getting an error at: let components = cal.components([.Day,.Hour,.Minute], fromDate: date,…
When Key
  • 87
  • 3
  • 7
1
vote
0 answers

Select remaining week dates with selected date using Swift3

How to select remaining week dates when user select any date. Below image shows what I want to achieve I am using JTCalendar to show calendar values. Note: I can't show any code because I don't know how to code this problem.
Usman Javed
  • 2,437
  • 1
  • 17
  • 26
1
vote
3 answers

Date since 1600 to NSDate?

I have a date that's stored as a number of days since January 1, 1600 that I need to deal with. This is a legacy date format that I need to read many, many times in my application. Previously, I'd been creating a calendar, empty date components and…
Steven Fisher
  • 44,462
  • 20
  • 138
  • 192
1
vote
1 answer

NSDate current date returning nil

In some scenario, I am getting currentDateString as nil.My app is in live so i can't find out exact scenario. NSDate *currentDate = [NSDate date]; NSCalendar *myCalendar = [[NSCalendar alloc]…
divya d
  • 169
  • 7
1
vote
1 answer

Realm filter firstDate older then newDate

I am trying to get the objects from realm, where newDate is later then firstDate. So if the date from firstDate is 05.10.2017, it will get objects after that date, example 06.10.2017, but not 04.10.2017. This is how I am storing the date: class…
Anton Diablo
  • 23
  • 1
  • 5
1
vote
2 answers

NSDateFormatter dateFromString working on iOS 10 but returning nil on iOS 11

My in house app which is working for last 2 years have messages dates in different format 1* Sep 20, 2017 at 8:09:39 PM 2* Feb 3, 2017, 5:49:42 PM I am converting these string to date like this NSDateFormatter *df = [[NSDateFormatter…
S.J
  • 3,063
  • 3
  • 33
  • 66
1
vote
1 answer

NSDateFormatter MMM conversion

As before XCode 9 I had no issue regarding DateFormatter. With the update I have a issue I do not seem to be able to bypass. I am busy converting dates from NSString to NSDate, however my code seems to not be able to initialize the NSDate. My…
Burnie777
  • 395
  • 5
  • 18
1
vote
0 answers

UiCollectionView show previous/Next month with paging

I need a little help ... I have a custom UIVIew Class where inside there is a collectionView showing a calendar with every day of the month. My collectionView works perfectly, at the top of the collectionView there are two UIButtons that allow the…
kAiN
  • 2,559
  • 1
  • 26
  • 54
1
vote
2 answers

Working with NSDate and Comparing with Dates in Objective C

"31-Dec-2010 9:00AM to 1:00PM" Take the above NSString for example, I need to convert it to 2 NSDates e.g. 31-Dec-2010 9:00AM AND 31-Dec-2010 1:00PM Then compare it with the current Date to see if the current date falls within the given dates. So…
David van Dugteren
  • 3,879
  • 9
  • 33
  • 48
1
vote
2 answers

Check if two dates are one month apart

I'm trying to check if two dates are 1 month apart. I read that I should use this method components:fromDate:toDate:options: from NSCalendar My code #define FORMAT_DATE_TIME @"dd-MM-yyyy HH:mm" NSDate *updateDate = [Utils…
bruno
  • 2,154
  • 5
  • 39
  • 64
1
vote
1 answer

How to set the first date of current week to Wednesday in Swift?

How to set the first date of the week to Wednesday and show the first and last date of week on yellow bar? Because the schedule will update every Wednesday.
Lester
  • 701
  • 1
  • 9
  • 47
1 2 3
99
100