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.
Related
I am having a very frustrating timezone issue with UIDatePicker.
When I initialise my date picker I set the time zone to the system time zone to allow the app to be used in multiple countries
[datePicker setTimeZone:[NSTimeZone systemTimeZone]];
Then a user selects a date. On saving the date (saved in CoreData) the date that comes back from the date picker is correct but then I want to just get the date and not the time i.e. with a time of 00:00:00
When I choose a date in GMT (not in BST) the date works fine. If I select date in BST (March - October) I get the date back as 1 hour behind.
This is how I am pulling the date out
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *dateOfBirth = [dateFormatter dateFromString:[dateFormatter stringFromDate: [dobDatePicker date]]];
I checked and the system time is definitly GMT, so then why does the value of dateOfBirth come back wrong
i.e
[dobDatePicker date] = "1991-05-30 18:58:16 +0000"
dateOfBirth = "1991-05-29 23:00:00 +0000"
Any suggestions? I dont want to force the timezone as I want it to run off the phones time zone.
Reason I am not interested in the time is I am storing this date as a date of birth and use it to calculate values like age and number of seconds until your birthday so dont want the current time from the date picker in the date I store in CoreData
Thanks
Use the following when fetching the time instead and let me know if it works for you.
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
This will return the phone's time zone always. I think systemTimeZone might be bugged. Cheers.
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.
How can I find the current time and it's region if i have current location, latitude and longitude?
I can get any city of any country as current location and having latitude and longitude of it and have to get current time and region of it. Then how to find current time and region based on that.
You will need a service such as Geonames timesezone and then adjust the NSDate to that timezone offset. The method dateWithCalendarFormat:timeZone: can be used with the date returned from [NSDate date].
NSDate Class Reference
Edit:
That webservice provides timezone offsets. dstOffset would be best in most situations since it accounts for daylight savings time otherwise use gmtOffset. NSTimeZone object can be created by using the static method + (id)timeZoneForSecondsFromGMT:(NSInteger)seconds and converting the offset from hours to seconds.
[NSDate date] will return a NSDate object with the current 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())!)
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.