How to get exactly real current date - iphone

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.

Related

need to convert UTC time to current timezone of device

Im using this repo
https://github.com/remirobert/Tempo
Can someone help me understand how to grab the current time zone of the device, and then notify tempo? I am using the timeAgoNow() function of tempo to find display how long ago the post was made, but the timezone difference is messing it up. My datasource is using UTC time.
Cocoa uses UTC internally. for all of its date/time calculations.
If you create an NSDate for now:
NSDate()
You get a date that is the number of elapsed seconds since midnight, 1970 in UTC time.
Dates only have time zones when you display them.
By default logging a date to the console logs it in UTC, which can be confusing.
If I'm working on a project that does a lot of date/time calculations I'll create a debugging method that converts an NSDate to a date/time string in the current locale, which is easier to read/debug without having to mentally convert from UTC back to local time.
I have never used Tempo, so I don't know if it is using date strings, NSDate, or "internet dates" (which are also in UTC, but use a different "zero date" or "epoch date")

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).

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())!)

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

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.

Handling time zones in Cocoa

I just want to clarify if I am understanding how dates & time zones work.
Basically, I have a date string #"2008-07-06 12:08:49" that I want to convert to an NSDate. I want this date and time to be in whatever the current user's time zone is set in. So if they are in GMT or HST, it's still 12:08:49.
If I have date in unix form 1215382129 (UTC) and my time zone is set to London (GMT), the outputted date from NSLog() is:
2008-07-06 12:08:49 +0100
If I then change my time zone to Hawaii (HST) and output the same date, I get:
2008-07-06 12:08:49 -1000
This seems to work fine, but I was under the impression to get the time in Hawaiian, I'd have to physically add the time difference (-10hrs) to the unix time stamp. Is this not required then?
Does that mean, whatever date and time a unix time is pointing to, it always points to the same date and time in whatever time zone a user is in?
Hope this makes sense!
Edit
I've just realised (thanks to Kevin Conner!) that in fact NSDateFormatter is creating different unix timestamps for that date string depending on the current timezone! So I was totally wrong!! :-)
Disclaimer, I'm mostly a Java guy. But Cocoa seems to work like the Java library in this regard: Dates are zoneless timestamps. Time zones are in the domain of formatting dates for display. In other words, the internal format doesn't consider time zones, it's all in UTC. Time zones are relatively a convenience for humans, so they are in the display/parsing side.
I noticed there is a setTimeZone: method on NSDateFormatter. Try calling that on your formatter before performing the format.