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

Combing NSString with the day of week and NSDate with the time into an NSDate object

I have the following two objects: An NSString object with a day of the week, ie Monday, Tuesday, Wednesday, etc. An NSDate object that was saved from a UIDatePicker with UIDatePickerModeTime. I need to create a third object, NSDate that is the…
Travis
  • 5,021
  • 2
  • 28
  • 37
1
vote
2 answers

Not able to get date in string format

I'm storing some dates in coredata in Date format. In another viewcontroller, I want to retrieve those dates and convert them to string. I tried to achieve it like so... if let bday = result.birthday { print(bday) let formatter =…
user8977455
1
vote
1 answer

Timestamp / NSDate for current event start in UIKit

Question: How can I make sure that the code executed due to a runloop event (timer, user interaction, performSelector, etc) have the same concept of "now"? Background: Say that event handler takes 100ms to execute, that means that [NSDate date] will…
1
vote
3 answers

DateString to timeStamp for sorting in Swift

I have an array of object with dateString(of creation) as parametere. I wanna sort the array of objects based on timestamp(of creation). For example, array = ["dateString":"2018-03-06", "dateString":"2018-03-05"]
j.krissa
  • 223
  • 5
  • 21
1
vote
1 answer

Swift 2 for loop low and high date between

I have Array ; var mydates : [String] = [] let startDate = "2018-03-01" let endDate = "2018-03-03" And I have 3 variable , startDate, endDate, dates , i want to append that variables, like; if startDate =…
SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
1
vote
1 answer

iPhone Natural Language processing for dates

I have dates in the format 'May 28, 2011' or 'February 23, 2011'. I want to know what the best way to turn these into NSDate's would be. I am thinking of just writing my own formatter but am curious if there is a built in class to do this.
nazbot
  • 1,527
  • 2
  • 15
  • 23
1
vote
1 answer

iPhone CoreData : Displaying and saving Dates

I have some real issues with display and saving a date. Its setup in coredata as a NSData. I use the following to display the date but fails (datestring is returned as nil). If I make a new date (e.g not from coredata) then its fine. NSDate *date…
Burf2000
  • 5,001
  • 14
  • 58
  • 117
1
vote
1 answer

Is it possible to append Data of audio file in AVAudioPlayer?

i'hv added 10 audio files (1.m4a,2.m4a,...,10.m4a) in my local project and i convert first part into data and append into NSMutableData then load in player. working good. Now i have button, onClick of button i append 3 parts of music files, but its…
Pratik Prajapati
  • 1,137
  • 1
  • 13
  • 26
1
vote
2 answers

Is this possible to assign NSDate variable to a nil?

Suppose if i want to erase a value stored in a NSString variable, i will assign a nil to it. This is possible. But if i want to erase a date variable that already holds some values, what should i do?
Rajkanth
1
vote
1 answer

How to extract today, yesterday from Date() and make it localised like weekday and months?

I want to extract today, yesterday from date so it is localised automatically when I change language like which happens with weekdays and month. Code for localised date that I am using: let dateFormatter =…
Amey
  • 795
  • 8
  • 31
1
vote
1 answer

iPhone - How may I work with Monday as first day of week

I'm working with many dates. For an international work, I'd like to force (and be able to let the user decide) which is the first day of week. But, once this is setted (let's say Minday as first day of week), I'm searching the best way to work in my…
Oliver
  • 23,072
  • 33
  • 138
  • 230
1
vote
1 answer

Converting date string with timezone to seconds in swift

I have a date string: "13 December 2017" a time zone string: "Asia/Kolkata" What is the way to get Epoch timestamp in seconds in Swift 4.0?
Ashok
  • 5,585
  • 5
  • 52
  • 80
1
vote
3 answers

Convert a Date (absolute time) to be sent/received across the network as Data in Swift?

I am looking for a Swifty way to generate a timestamp. My macOS app logs some data and stamps it with the time the data was created. The data will then be sent across the network (as Data) to be reconstructed on an iPad. Is there any Swift class…
MH175
  • 2,234
  • 1
  • 19
  • 35
1
vote
2 answers

Assemble date object from integers?

Given three integers, representing a day, month and year, what code would assemble those integers into a date object?
Moshe
  • 57,511
  • 78
  • 272
  • 425
1
vote
0 answers

Repeat UNCalendarNotificationTrigger

I am making a local notification using UNCalendarNotificationTrigger. My goal is to repeat this notification every day at 8:54 PM. For some reason, it always works the first time. However, when forwarding the system date on my Mac and testing in…
asdasdasd
  • 19
  • 5
1 2 3
99
100