Getting number of days between [NSDate date] and string #"2010-11-12" - iphone

I need a function which receives [NSDate date] and a string #"2010-11-12" and returns the amount of days between those two dates.
More explanation:
I need to store a date from a server in the format #"2010-11-12" in my NSUserdefaults. The meaning of this date is the expireDate of a feature in an iPhone App. Every time I press on a button for this feature I need to check if the difference in days between the current time->[NSDate date] and #"2010-11-12" is greater than 0. That means that the feature is disabled.
It's making me crazy, maybe it's dead simple.

Use an NSDateFormatter to convert the string to an NSDate - then compare the date objects.

Related

How calculate time from current time?

I have design an application in which i am have three text field in which i am giving my time like as 10:10 PM. Now problem is that i want to calculate time difference between current time and entered time. How calculate time?
Depending what you mean by "entered time" you will most likely need to look at NSDateFormatter and parse the entered time into a usable form, then get a date using NSDate (i.e. NSDate * now = [NSDate date];) and do whatever math you need using the methods available in `NSDate'.
If you are just determining the difference between entered date and now this method would suit you:
- (NSTimeInterval)timeIntervalSinceNow
you can get the difference in seconds using time intervals (e.g. that of the NSDate and CFAbsoluteTimeGetCurrent).

How to get exactly real current date

I know you guys might be saying that this question is very common but actually I'm referring to the actual current date and time:
Let say today is 1st of August,12pm and no matter how the user change their phone date and time, I will still know today is 1st of August, 12pm...
Anyone know how can I do that?
You can get the real date and time from an NTP server, but this assumes that you have internet access.
If the user changes their phone date and time, then you will get that date and time... since NSDate gets its value from the iOS. If you want it to return the actual time per time zone, you will have to set up a small Web Service or something that returns the time for that particular time zone.
NSDate *currentDate = [NSDate date];
Now currentDate will have the current date & time when this code is executed.

Getting the date for the users current time zone?

When using NSDate to get today's date, I seem to be getting a date that is ahead in time. For example, today is February 19 but NSDate is giving me February 20?
What can I do to get the proper date?
I'm using NSDate *today = [NSDate date]; to get todays date but the problem is that it's not in my current time zone.
This sentence doesn't make sense. An NSDate object represents a single point in time, regardless of time zone. NSDate does not even have a sense of time zones.
To output a date as a string, always create an instance of NSDateFormatter and call its stringFromDate: method. By default, NSDateFormatter uses the current user's time zone.

Does [NSDate date] return the local date and time?

Am I stupid? All this time I thought [NSDate date] returned the local date and time. After having some trouble with NSStringformatter/stringFromdate/dateFromString today I noticed that my [NSDate date] was returning 2011-03-06 11:00:00 +0000. After researching this I see that [NSDate date] returns a raw date which is always GMT.
What purpose does the gmt offset portion serve if it always shows +0000? Also I do not understand [myDate description]. The docs says it is supposed to display gmt offset as well as dst information. I get the same thing as [NSDate date].
Bottom line for me, if I use [NSDate date] to get the current date and it is after 2pm I get tomorrow's date as I am in the -10 time zone. Not to mention the problems I ran into today with NSDateformatter.
Am I seeing this correctly? Funny thing is I seem to remember seeing [NSDate date] returning 2011-03-06 11:00:00 -36000, or did I think I was seeing 2011-03-06 11:00:00 -10000.
I can work with it, but maybe someone can expound on this to help me better understand NSDate.
NSDate returns the current date. Though it can convert to and from string format in different locales/time zones, NSDate has no internal concept of a time zone. NSDates are not bound to any particular region. If you and anyone else in the world asked your devices to [NSDate date] simultaneously, you'd get equal results — not because it always returns in GMT but because it doesn't return in a time zone-specific manner. In Cocoa, a date is a specific moment in the history of the Earth. How you write a date is related to your calendar, your time zone and your locale.
You aren't getting tomorrow's date, you're getting the correct date and then noticing it gives a different day if expressed in GMT. It's the correct date, just written differently from what you'd like.
'description' is a method overridden from NSObject. When you NSLog an object, what happens internally is that the description method is called and the string returned is printed. So you should get identical results from logging object and [object description], since the former calls description and prints that string, the latter calls description then calls description on the resulting string and prints that. NSStrings return themselves as the result of description.
It should be the default behaviour but to get a meaningful description, try:
NSLog(#"%#", [[NSDate date] descriptionWithCalendarFormat:nil
timeZone:[NSTimeZone localTimeZone] locale:[NSLocale currentLocale]]);
If that still logs in GMT then your device believes itself to be in GMT. I've no idea how reliable the simulator is about that sort of thing.
In Swift:
NSDate() gives us an agnostic date's description, it always returns the UTC+0 date time, independently of the device's time zone:
print(NSDate())
.descriptionWithLocale(NSLocale.currentLocale())! gives us a localized date's description UTC+N date time where N represents the offset (positive or negative) between UTC+0 and the current local date time:
print(NSDate().descriptionWithLocale(NSLocale.currentLocale())!)

Calculating times based on generic days of the week using NSDate

I'm trying to figure out how to calculate store opening times based on the way they are normally expressed. For example: Monday - Friday, 9am - 6pm. I want to know two things from this criteria. Is the store open? If it is how long will it be before it closes?
My confusion with the documentation is that NSDate calculations expect unique date data sets to perform calculations not generic as expressed above. For example NSDate expects 2010-07-12T09:00:00Z & 2010-07-12T18:00:00Z to form the start basis of the calculation and like wise for the days in between.
Do I need to create a data set for the year on this basis to calculate the results? Or is there a clever way I can get NSCalendar / NSDateComponents to do all this hard work for me?
There is no simple/automatic way. You have to parse the string yourself, form two, let's say startDate and endDate (or something alike). From them you can extract the day of the week and time (that's where NSCalendar and NSDateComponents come in handy).