Need information on printing [NSDate date] - ios

When I am printing the date
//getting the current date and time
self.date = [NSDate date];
NSLog(#"%#",date);
The date which I am getting is correct, but there is a delay in time by 6 hrs. My system time is correct.

try this
NSLocale* currentLoc = [NSLocale currentLocale];
NSLog(#"%#",[[NSDate date] descriptionWithLocale:currentLoc]);

Make use of NSDateFormatter
NSDate *today = [NSDate date];
//Create the dateformatter object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//Set the required date format
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//Get the string date
NSString *dateString = [dateFormatter stringFromDate:today];
//Display on the console
NSLog(dateString);

Logging an NSDate in the debugger is somewhat misleading as it gives you a calendar day and time for a particular time zone - UTC / GMT. However, NSDate has no inherent time zone or any inherent relationship to how humans perceive and think about dates at all. Instead, it is a timestamp. Classes like NSDateComponents, NSTimeZone, NSDateFormatter, and so on all exist to provide human context and formatting.
So what you see is the timestamp formatted to that particular format and UTC time zone, which is how NSDate will always appear when printed in the debugger or the console. If you were to calculate the time zone offset between UTC and your own time zone, you'd find that the date represents the time stamp you gave it, and not one however many hours off.

you can set current time zone for customizing your date format.
This link can help:
https://stackoverflow.com/a/7213629/456471

The default date string representation is probably formatting the date as UTC, rather than your local time zone (the exact format that it will use is not defined, and may change from release to release, so you shouldn't rely on it). You should use the NSDateFormatter class if you need to format a date in a particular format (or with a particular time zone, including the local time zone); see the Data Formatting Guide and the NSDateFormatter Class Reference for more information.

Related

Conversion: NSString to NSDate

This is a really common question, but mine might be unique since I have long decimal places for the seconds.
NSString *timestamp = #"2015-11-06 15:27:34.0000000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss zzz"];
NSDate *capturedStartDate = [dateFormatter dateFromString: timestamp];
My capturedStartDate is null.
For the date format, I've tried replacing zzz with Z, a, and removing it completely. I've also tried with and without the 'T'. Does someone know the correct format to retrieve this date from the string?
Your date string doesn't have a time zone in it, so you should remove that Z. Also, you have fractions of a second, too. And there's no T in the date string. So you want
yyyy-MM-dd HH:mm:ss.SSSSSS
This does beg the question as to what time zone that string represents. If it is UTC, you'll want to set the time zone of the formatter, accordingly.
Likewise, you might want to be careful about users with non-Gregorian calendars. See Apple Technical Q&A 1480 regarding setting the locale to en_US_POSIX.

Converting ISO8601 Date Format to local time (iOS)

So there is a section on web application that users can enter events into and the web service sends those events to the mobile app in the following format:
"yyyy-MM-dd'T'HH:mm:ssZZZZZ"
I'm having issues trying to convert the string into a date so I can get just the time from the event (formatted in the correct timezone as well), So for example here's one that comes over "2015-03-20T20:00:00-07:00", which when I pull the time should be 1PM Pacific Time. But instead I either get 8PM or 3AM (depending on whether I add UTC abbreviation to the date formatter).
Here's what I have so far, I know I'm missing something here & maybe there's another date formatter that needs to be used but so far I can't figure out where I'm going wrong.
NSString *datePattern = #"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:datePattern];
NSString *sString = [valueDict valueForKey:#"start_date"];
NSDate *startDate = [dateFormatter dateFromString:sString];
NSDateFormatter *timeFormatter = [NSDateFormatter new];
[timeFormatter setDateFormat:#"hh:mm a"];
[timeFormatter setLocale:[NSLocale systemLocale]];
NSString *timeString = [timeFormatter stringFromDate:startDate];
2015-03-20T20:00:00-07:00 is 8pm Pacific Daylight Time.
If you're representing 1pm PDT, that's either
2015-03-20T13:00:00-07:00
or represent that in "Zulu" (i.e. GMT/UTC)
2015-03-20T20:00:00Z
When working with a web service, the latter is the common convention for ISO 8601 dates. Then, when you present it to the user, you present it to them in their local timezone (using a NSDateFormatter with its default timeZone setting.
Note, when using NSDateFormatter to prepare ISO 8601 dates, you will want to ensure that you specify a locale of en_US_POSIX as outlined in Technical Q&A QA1480. When designing app for US audience this isn't critical, but it's best practice in case the user is not using a gregorian calendar on their device.

Date conversion from string to NSDate in ios

I have searched over internet for a long time to get this but I can't find the solution. I have received a date string from web services as "22 May 2014", I have to convert into NSDate format for check it with current date. And I have to find out the date from web service is in future or in past time.
The actual problem is that when I convert this using
NSDate *date;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMMM YYYY"];
date = [dateFormatter dateFromString:dateString];
But I get an entirely Different Date, Sample Input dateString:22 June 2014 and Output I get is 2013-12-21 18:30:00+0000
Please suggest any solutions.
Thanks in advance. :)
You're using YYYY, which doesn't mean what you think it means. From the TR35-31 documentation, Y is the symbol for "year in week-of-year calendars".
You want dd MMMM yyyy instead as your format string. Mixing week-of-year-based fields and regular day/month/year fields is a recipe for odd problems.
Additionally, you may well want to set the time zone in your formatter - if you're just parsing a date, then you should consider using UTC, and make sure that all your calculations and formatting/parsing use UTC.
(I suspect the issue here is that week-of-year hasn't been set, so is assumed to be 1... and the week-year 2014 started on December 30th. Then the day-of-month is set to 22 by the dd part, and then your time zone offset of UTC+05:30 is taken into account.)

Convert NSString containing AM/PM to NSDate

Maybe it's an old question and I've searched on this site and found some similar questions, but I still cannot solve my problem. I have a NSString named gameDateTimeStr: "11/12/2013-10:00 PM" and I want to convert it to NSDate. I used the following code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yyyy-HH:mm a"];
[formatter setLocale:[NSLocale currentLocale]];
NSDate *gameDateTime = [formatter dateFromString:gameDateTimeStr];
However, when printed on consolte, its description is: 2013-11-12 05:00:00 +0000
As you can see, the date is correct, but the time is wrong.
Please help me fix it.
There are one issues with you date formatter, first you are using 24 hour format, HH for the hours in the date format. But you example uses 12 hour format and hh should be used.
[formatter setDateFormat:#"MM/dd/yyyy-hh:mm a"];
Depending on you timezone offset the date is parsed correctly, since date printedby NSLog is is represented in GMT.
When you create a string form the date object use NSDateFormatter you timezone is used to calculate the correct time offset.

Remove time from date

I'm having a problem with NSDate instance.
For the date I'm receiving 2012-08-16 00:00:00 +0000.
But I need to remove from the 00:00:00 +0000.
Is there any way to do that?
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease];
[formatter setDateFormat:#"yyyy-MM-dd"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSDate *eventDate = [formatter dateFromString:currentDate]; // current Tile
NSLog(#"date %#",eventDate);
Use
NSLog(#"date %#",[formatter stringFromDate:eventDate]);
A NSDate is just a single point in time. It does not know how it's value should be formatted, that's what NSDateFormatter is for.
NSLog(#"date %#",eventDate); prints the default string representation of the date.
A date formatter converts strings into dates and dates into strings. An NSDate represents an point in time, regardless how you created it. In your code you seem to be thinking that the NSDate will "remember" that the formatter it came from didn't specify a time of day; it won't. When you call dateFromString: it will simply pick 00:00:00 (midnight) as the time.
If you need to work with calendar dates regardless of the time, you can either:
Use NSDate objects and ignore the time component. You will need to be wary of time zones, since midnight on August 15th in one time zone can be 11pm on August 14th in another.
Use a different data structure to store year, month, and day. NSDateComponents is a good candidate, or you could create your own.

Resources