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
72
votes
10 answers

Get UTC time and local time from NSDate object

In objective-c, the following code results in the UTC date time information using the date API. NSDate *currentUTCDate = [NSDate date] In Swift however, let date = NSDate.date() results in local date and time. I have two questions: How can I get…
p0lAris
  • 4,750
  • 8
  • 45
  • 80
72
votes
16 answers

Comparing two NSDates and ignoring the time component

What is the most efficient/recommended way of comparing two NSDates? I would like to be able to see if both dates are on the same day, irrespective of the time and have started writing some code that uses the timeIntervalSinceDate: method within…
Magic Bullet Dave
  • 9,006
  • 10
  • 51
  • 81
71
votes
5 answers

get current date from [NSDate date] but set the time to 10:00 am

How can I reset the current date retrieved from [NSDate date] but then change the time to 10:00 in the morning.
user689751
69
votes
2 answers

How to check if two NSDates are from the same day

I am working on ios development and I find it really hard to check if two NSDates are from the same day. I tried to use this fetchDateList() // Check date let date = NSDate() // setup date formatter let dateFormatter =…
JoshJoshJosh
  • 897
  • 2
  • 11
  • 20
69
votes
6 answers

Converting a string to an NSDate

How is it possible to convert a string to an NSDate on iOS?
hugo411
  • 320
  • 1
  • 12
  • 29
68
votes
8 answers

Swift 3.0 : Convert server UTC time to local time and vice-versa

I want to convert server UTC time to local time and vice-versa. Here is my code.. var isTimeFromServer = true var time:String! var period:String! let timeString = "6:59 AM" //Current UTC time if isTimeFromServer { let index =…
NiravS
  • 1,144
  • 1
  • 12
  • 24
65
votes
1 answer

Get Current date & time with [NSDate date]

My system's date time is 26 May 22:55 but when i get date with [NSDate date] date time is 27 May 02:35 is it because of time zone ? if yes how to solve this problem, that when i get date time, give me the date of my system and doesn't check time…
user437064
64
votes
12 answers

Check if date is before current date (Swift)

I would like to check if a NSDate is before (in the past) by comparing it to the current date. How would I do this? Thanks
Tom Coomer
  • 6,227
  • 12
  • 45
  • 82
63
votes
7 answers

Converting UTC date format to local nsdate

I am getting from my server a string date in UTC time zone and I need to convert it to the local time zone. MY CODE: let utcTime = "2015-04-01T11:42:00.269Z" let dateFormatter = NSDateFormatter() dateFormatter.timeZone = NSTimeZone(name:…
ilan
  • 4,402
  • 6
  • 40
  • 76
63
votes
17 answers

Ordinal Month-day Suffix Option for NSDateFormatter setDateFormat

What setDateFormat option for NSDateFormatter do I use to get a month-day's ordinal suffix? e.g. the snippet below currently produces: 3:11 PM Saturday August 15 What must I change to get: 3:11 PM Saturday August 15th NSDate *date = [NSDate…
Matt Andersen
  • 4,816
  • 3
  • 24
  • 19
62
votes
11 answers

Swift 3 - find number of calendar days between two dates

The way I did this in Swift 2.3 was: let currentDate = NSDate() let currentCalendar = NSCalendar.currentCalendar() var startDate : NSDate? var endDate : NSDate? // The following two lines set the `startDate` and `endDate` to the…
Vinod Vishwanath
  • 5,821
  • 2
  • 26
  • 40
61
votes
12 answers

Minimum and maximum date in UIDatePicker

I want to get the minimum and maximum date from a date picker, but minimum date should be "- 18" of the current date and the maximum date should be "- 100" of current date. Suppose current year is 2018 then I want minimum date 2000 and maximum date…
NSException
  • 1,268
  • 3
  • 14
  • 28
60
votes
10 answers

How can I get an NSDate object for today at midnight?

What is the most efficient way to obtain an NSDate object that represents midnight of the current day?
markdorison
  • 139,374
  • 27
  • 55
  • 71
60
votes
14 answers

How do I get the current date in Cocoa

I'm getting started developing for the iPhone and as such I am looking at different tutorials online as well as trying some different things out myself. Currently, I'm trying to create a countdown until midnight. To get the number of hour, minutes,…
Jason
  • 17,276
  • 23
  • 73
  • 114
59
votes
8 answers

Calculating the number of days between two dates in Objective-C

Possible Duplicate: How can I compare two dates, return a number of days I have two dates (as NSString in the form "yyyy-mm-dd"), for example: NSString *start = "2010-11-01"; NSString *end = "2010-12-01"; I'd like to implement: -…
CodeGuy
  • 28,427
  • 76
  • 200
  • 317