Is this reference or code in mistake or bug? - ios

I copied some text from NSDate Reference as below, please check Return Value,
it is said the format will be in YYYY-MM-DD HH:MM:SS ±HHMM, but I got as below
in my app, so the reference is mistake? or code in mistake?
Saturday, January 1, 2011 12:00:00 AM Japan Standard Time
or
2011年1月1日土曜日0時00分00秒 日本標準時
descriptionWithLocale:
Returns a string representation of the receiver using the given locale.
- (NSString *)descriptionWithLocale:(id)locale
Parameters
locale
An NSLocale object.
If you pass nil, NSDate formats the date in the same way as the description
method.
On Mac OS X v10.4 and earlier, this parameter was an NSDictionary object.
If you pass in an NSDictionary object on Mac OS X v10.5, NSDate uses the
default user locale—the same as if you passed in [NSLocale currentLocale].
Return Value
A string representation of the receiver, using the given locale, or if the
locale argument is nil, 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”)

If you're looking for a date in the YYYY-MM-DD HH:MM:SS ±HHMM format, pass in 'nil' for the locale; otherwise, pass in your locale.

Related

Getting next date when trying to convert NSDate to String

I have this date in actuall
2016-09-03 19:00:00 +0000
Now I am trying to convert it to String using a specific format like below
But what I am getting in return is not as desired. the formatter is adding on day to the given date like below
Is this standard behaviour ?
This is not standard behaviour. This happen because of the time zone difference. Set time zone proper
Set the timezone.
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation: #"GMT"];
When you hover over the date, you can see that it is showing UTC, whereas the formatter is automatically converting this to a local date. If your timezone is 5 hours ahead of UTC, then it will be the next day locally from that time.

iOS NSDateFormatter needs NSLocale even it's UTC

I have a doubt that I cannot understand why is the way it is and I appeal to the Gods of this site :)
I have a date coming like this:
"1982-01-01T00:00:00Z"
As I'm displaying whatever the server sends (I know, customer requirement, not good practice...), I'm forcing the device to have that TimeZone with the following method, simplified without error checking, not optimized, and all that kind of things:
+ (NSString *) yearStringFromDate: (NSDate *) date
{
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"YYYY"];
[formatter setTimeZone:[self timezoneForSIHF]];
return [formatter stringFromDate:date];
}
This should be UTC, BUT if I don't set the locale I'm getting, and JUST sometimes the incorrect year. So by adding this I get the year correct for all cases:
[formatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
For further info, I'm testing in a real device and simulator with different languages (en, de, es).
Why is this?
Why does the locale affect the date even though the timeZone is correct?
Why sometimes is working with some dates and sometimes it's not?
For example, 1982 is returning without setting the locale, 1981 and if I set it 1982. This doesn't happen with 1980, returning in both cases 1980 (or 1987, or ...)
Thanks in advance for all your replies :D
Cheers!
When converting ISO 8601/RFC 3339 date string to NSDate object, one uses the en_US_POSIX locale in case the user is not using a Gregorian calendar. See Technical Q&A 1480.
In your case, though, you are trying to get year string representation from date object. In that case you might not need en_US_POSIX. That's only necessary if you need to use Gregorian calendar regardless of what sort of calendar the device might currently be using.
As noted by others, though, you should be using yyyy and not YYYY. The former returns the calendar year. The latter returns the year, in "Week of Year" based calendars, which may not always be the same value as calendar year.
See the date formatting patterns for a discussion contrasting y and Y.
By the way, if you really need the year component of the date, you can also use the NSDateComponents related methods of NSCalendar.
Use yyyy instead of YYYY.
Reference.

Force region when serializing dates to JSON from NSDicitionary?

I am initializing an AFJSONRequestOperation (using the AFNetworking libraries) with a request that contains date fields. When the endpoint receives the request, the date is formatted in the region of the device. For example:
"last_update" = "dom dic 1 10:57:52 -0500 2013";
I would like to force the formatting to a region such as Canada or the US.
Is this possible as our endpoint is choking on dates that are not formatted with English such as:
"last_update" = "Wed Dec 1 10:57:52 -0500 2013";
If your API requires this, check out NSDateFormatter, which creates string representations of NSDate objects in any way you want. Just set the locale like this:
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:usLocale];
Here's a good intro to this class at NSHipster.
You may find it simpler, though, to change your API to accept the number of seconds since 1 January 1970, GMT as the date. This is a common way to express date and time when transferring data between platforms. You can call the timeIntervalSince1970 method on your NSDate object to retrieve this data. Then the API can translate it to a locale, or a time zone, if it needs to.

iOS Converting milliseconds to date returns wrong time

I am trying to convert milliseconds into date. Below shown is the code i am using.
double startDateDb=1380275880000;
NSDate *date=[NSDate dateWithTimeIntervalSince1970:(startDateDb/1000.0)];
NSLog(#"date---%#",date);
My log gives date as 2013-09-27 09:58:00 +0000
When i use online tool to convert i am getting "9/27/13 5:58 AM" which is correct.
Please help me to fix the issue.
NSLog used your timezone when displaying dates. TO get the date in UTC use the NSDate methods and specify the tie zone. All NSDates are UTC timezone based.
Use NSDateFormatter to display the date/time in another timezone.
Your NSLog put the timezone in GMT, the timezone you're looking for is GMT-4.

MagicalRecord date parsing

I've got a date in the following format:
2013-05-04T05:07:09+00:00
I'm using MagicalRecord to map the NSDate automatically. As far as I can see the above date format should comply with MagicalRecord's default date format: yyyy-MM-dd'T'HH:mm:ss'Z'.
I have tried with a custom dateFormat entry in the attribute's user info (see this article):
yyyy-MM-ddTHH:mm:ss+Z, yyyy-MM-dd T HH:mm:ss Z, yyyy-MM-dd'T'HH:mm:ss'+'Z
but none of them work in order to have it parse the date properly and it always returns nil regardless of setting a custom dateFormat or using MagicalRecord's default format.
Let's look at your string:
2013-05-04T05:07:09+00:00
This is:
four digit year
hyphen
zero-padded month
hyphen
zero-padded day of month
'T' character
zero-padded hour
':' character
zero-padded minute
':' character
zero-padded second
timezone (with direction from GMT and a separating colon)
Thus, according to the date format specifiers documentation, the pattern you'd want is:
yyyy-MM-dd'T'HH:mm:ssZZZZZ
Also, be sure to use the en_US_POSIX locale with the NSDateFormatter.

Resources