[NSDate date] returns GMT on simulator... what time does it return on the device... ? And also how can I get the simulator time programmatically?
It returns the current date and time. NSDate is timezone-independent. The -description of NSDate may be in GMT, but it's not guarenteed.
The representation is not guaranteed to remain constant across different releases of the operating system. To format a date, you should use a date formatter object instead.
If you want to display or parse a date in a different timezone, use NSDateFormatter (and modify its timezone/locale property) or use -descriptionWithLocale:.
NSLocale* currentLocale = [NSLocale currentLocale]
NSLog(#"current locale = %#", [currentLocale localeIdentifier]);
NSLog(#"date = %#", [[NSDate date] descriptionWithLocale:currentLocale];
NSDate is timezone-independent. When you get the description with description it displays a timezone but that's entirely "informational", and, to my knowledge there's no way to set or access that timezone info (other than description). The value stored in the NSDate object is (supposed to be) GMT/UTC (though of course it can't tell if you're feeding it the wrong time).
To get the simulator's understanding of the current time and date use [NSDate date]. Format it (if need be) with NSDateFormatter.
Related
+(NSDate *)DateServerFormatFromString:(NSString *)date
{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init] ;
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
[dateFormatter setLocale:locale];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate* returnDate = [dateFormatter dateFromString:date];
return returnDate;
}
The function returns date in format "2013-05-09 08:06:04 AM +0000". But i want it in 24 hour format. The date being passed to this function is in the exact same format as given in setDateFormat method.
The device's region format is set to "United Kingdom" and time format is set to "12 hour format". This should not be changed. When time format is set to 24 hour format in the device, the function works perfectly. What is wrong with the code. I am using iPad 1 with OS 5.1.1. Setting locale identifer or even timezone didnt make a difference. Thanks in advance.
This is not necessarily an answer, just an explanation, found in the Apple docs (here):
Although in principle a format string specifies a fixed format, by
default NSDateFormatter still takes the user’s preferences (including
the locale setting) into account. You must consider the following
points when using format strings:
NSDateFormatter treats the numbers in a string you parse as if they
were in the user’s chosen calendar. For example, if the user selects
the Buddhist calendar, parsing the year 2010 yields an NSDate object
in 1467 in the Gregorian calendar. (For more about different
calendrical systems and how to use them, see Date and Time Programming
Guide.)
In iOS, the user can override the default AM/PM versus 24-hour
time setting. This may cause NSDateFormatter to rewrite the format
string you set.
The problem here is how you're getting the string. You say you got it
by printing the description of returnDate
However, according to the documentation, the -description method says this:
The representation is useful for debugging only.
There are a number of options to aquire a formated string for a date including: date formatters (see NSDateFormatter and Data Formatting Guide), and the NSDate methods descriptionWithLocale:, dateWithCalendarFormat:timeZone:, and descriptionWithCalendarFormat:timeZone:locale:
What this means is that the value returned by -description does not respect any 12- or 24-hour time settings. It's just a debug version.
If you want to express a date as a human-readable string, you must use an NSDateFormatter.
Again and again:
Did you NSLog() the date? Logging a date is always been done in a normalized standard format. If you want to log a date formatted, log the result of a formatter, not the date directly.
You want 24h format for what? Format is used to convert NSDate to NSString with correct string format. Just make reverse converting when you need it
12 hour format
[dateFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
24 hour format
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
Alright. Our application sends an NSString made out of the current user's username and todays date formatted in yyyyMMddHH. Our server, that is located in sweden, makes the exact same String and compares the two when it gets the call.
Now. We have realized, that if one of our users goes abroad, the timezone will change, resulting in complications.
if the iPhone user were to be in lets say, Seoul, South-korea. His NSString that is sent would be something like:
2011061718
Meanwhile, when our server gets the call, it will recreate its own datestring in this format because it's located in sweden.
2011061711
And therefore deny the user access to the functions on the server side.
To summarize:
How do i programmatically set a default static timezone in my application?
Atm we do this:
NSDate *aDate = [aDateFormatter stringFromDate:[NSDate date]];
And somehow we need the timezone and compare the difference between the user's actual timezone and change it to a swedish-timezone.
Any ideas?
EDIT: Ok. This application is only meant to be released in sweden. And we are using a combination of the user's username and the current date in the format of yyyyMMddHH to make a secure key, the key is meant to update itself whenever a new hour starts. The server, which is located in sweden in the timezone of GMT+1 makes a verification that the user is on an actual device using the application and not someone who has made a client of his own making soap-calls to our service.
Therefore, if one of our users goes outside the timezone, it will reject the user since the strings wont match.
This is why we want to set the default timezone for that function GMT+1 at all times. And this is what we're really looking for.
Thanks. Again.
If you need to make the client format the date, this is how to do it:
NSDate *now = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"Europe/Stockholm"]; // Sets the right time.
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]; // Forces the date formatter to accept any format string.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setCalendar:gregorianCalendar];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:#"yyyyMMddHH"];
[formatter setLocale:locale];
NSString *dateString = [formatter stringFromDate:now];
NSLog(#"Date: %#", dateString);
[locale release];
[gregorianCalendar release];
[formatter release];
The date formatter will do all the work for you, you just need to configure it.
By setting the gregorian calendar, you're using the same calendar as in Sweden.
By setting the timezone, you'll get the time as it would be in Sweden.
By setting the locale to "en_US_POSIX" you make the formatter use the exact format you specify, and not append any AM/PM stuff.
Don't mess about with that. Send complete date time as a string and let the server figure out the times.
NSString *dtString = [[NSDate date] description];
This will create a string with the format
YYYY-MM-DD HH:MM:SS ±HHMM
Use -[NSDateFormatter setTimeZone:]. NSDate has nothing to do with it as it does not care about time zones.
Btw, your code will also fail if the user uses a different calendar.
Also a heads up, if you turn the AM/PM function on the dateformatter will pring out 'AM/PM' even if you dont place it in the format string.
To fix this also add an NSLocale to the DateFormatter.
I'm getting the current date/time using [NSDate date]. The value returned is an hour in the future. I've check my phones location & time settings and they are correct.
I can display the correct date and time as a string using the code below.
[NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle
timeStyle:NSDateFormatterShortStyle]
But I need it to return the correct date/time as a date object as I use it to calculate the estimated time of arrival using -
[date dateByAddingTimeInterval:interval]
I realise my question is similar to this one already asked but none of the answers suit my needs. Thanks in advance!
init] returning date an hour in the past?
Maybe you are confusing the point in time (ie the NSDate object) and the point in time at your location (ie your local time).
If you print a NSDate (like NSLog(#"%#", [NSDate date]); which invokes [date description]) the date representation that is printed is in UTC timezone (+0000) (at least it is on my computer).
So as long as you don't live in an area that uses UTC the date printed by [date description]; is always "wrong". But wrong only means that its representation is not the same representation as the clock in your office. The date (as in point in time) is still correct.
When you use localizedStringFromDate:dateStyle:timeStyle: you are printing the date in your local timezone.
NSDate *date = [NSDate date];
NSLog(#"%#", date);
NSLog(#"%#", [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]);
at my computer this results in:
2011-02-12 08:32:10.658 x[75647:207] Date: 2011-02-12 07:32:10 +0000
2011-02-12 08:32:10.661 x[75647:207] Date: Saturday, February 12, 2011 8:32:10 AM Central European Time
the printed strings are different, but the NSDate object is still the same. That's why you have to use NSDateFormatters when you show a date to the user. Because the same point in time looks different on different places of the world.
But there are only three places where an UTC formatted date would be one hour in the future, so if you don't live in greenland, cape verde or on the azores I might be totally wrong and there is something wrong with your NSDate objects.
Edit: Out of curiosity I read the documentation about [date description] again. And it says
A string representation of the
receiver in the international format
YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM
represents the time zone offset in
hours and minutes from GMT (for
example, “2001-03-24 10:45:32 +0600”).
So I don't know why the date at my computer is printed in GMT timezone. It might be in another timezone at your computer.
But still, it's only the representation, the date is still the same.
In our app, we always need to pick the current Eastern time (EST or EDT).
We set the application timezone as below in the app delegate:
NSTimeZone *ESTTimeZone = [NSTimeZone timeZoneWithName:#"US/Eastern"];
[NSTimeZone setDefaultTimeZone:ESTTimeZone];
Also, for the NSDateFormatter, we set the locale as follows:
NSDateFormatter *formatForFileName = [[NSDateFormatter alloc] init];
[formatForFileName setDateFormat:#"yyyyMMdd"];
NSLocale *uslocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatForFileName setLocale:uslocale];
[uslocale release];
If my iPad is set to correct EST date and time, there is no issue. If I were to log the current date as follows:
NSLog(#"current date : %#",[formatForFileName stringFromDate:[NSDate date]]);
it will display today's date correctly.
However, if I change my iPad date to be the next day's date and log the current date, I would want the current EST date and time to be returned.
However, [NSDate date] returns me the current iPad date (the next day's date) and time and not the EST date and time.
Is there any way I can get the correct EST date and time using [NSDate date] irrespective of what the user has set on his iPad.
Thanks.
I'm not sure if I understand your question. [NSDate date] always creates a date with the date/time the OS thinks is valid at the moment, i.e. the time the device's clock is set to. If you have doubts whether this is indeed the current time, you would need to contact a trustworthy time server on the Internet and get the time from there.
[NSDate date] returns a date set to the current system date and time. So when the user changes to the next day's date on the system, [NSDate date] will return next day's date. NSDate assumes (rightfully so) that the system date is the correct date.
To display the actual EST date, you will need to query a source (a remote source) other than the system for the date that you want.
For PDT, I would want "-0700".
I'm getting a date in the past to determine how long ago something happened.
NSDate *then = [NSDate dateWithString:#"1976-04-01 12:34:56 -0700"]; // Note the hard-coded time zone at the end
I'll be constructing the date string elsewhere but I don't know how to access the local time zone.
I read the Apple Dates and Times Programming Topics for Cocoa as well as the NSTimeZone and NSDate Class References but it's just too hard for me to put the information together. I could really use a few lines of code just to show how it's used.
Update: While struggling with this, I was writing code using a Command Line template so I could try things quickly. I just tried my previous code on iPhone and I'm getting NSDate may not respond to '+dateWithString:' Sorry if that added to the confusion, who knew Apple would change up such a basic class.
Use NSDateFormatter to build NSDate from a string:
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *formatterDate;
formatterDate = [inputFormatter dateFromString:#"1976-04-01 12:34:56 -0700"];
NSString *dateString = [inputFormatter stringFromDate:formatterDate];
NSLog(#"date:%#", dateString);
This way you get the local time from string, for example the date specified by the string:
"1976-04-01 12:34:56 -0700"
is in time zone -0700, (I'm in time zone GMT +1000) so I get:
2009-11-17 22:13:46.480
cmdline[10593:903] date:1976-04-02
05:34:56 +1000
The time zone offset is dependent on the date in much of the world—those parts of it that use Daylight-Saving Time/Summer Time.
The only correct way is to generate the entire string from date and time-zone together. Use NSDateFormatter for this.
The best way is to probably use a simple calendar formatter
NSCalendarDate * date = [NSCalendarDate calendarDate];
[date setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"PDT"]];
NSLog([date descriptionWithCalendarFormat:#"%z"]);
which will output '-0700'
or leave out the second line if you want the current time zone of the system (not sure which you were asking for)