How to convert NSString to NSDate? - objective-c

I want to search my Array of Dictionary for particular date.Ex I want to search my array of dictionary for date "16 Jan 2012" which is in string format but my dictionary item in array contains date and time, say "16 Jan 2012 somehours:somemins:somesecs".I am converting string format date in NSDate format but I am getting date as 2012-01-15 18:30:00 +0000 instead of 2012-01-16.I am using NSPredicate to search for the date which convert date into seconds as follows "Date == CAST(348345000.000000, "NSDate")" and compare so even though my records contain date as "16 Jan 2012 somehours:somemins:somesecs" it will not satisfied the criteria.I want that the records/array containing date as "16 Jan 2012 somehours:somemins:somesecs" should satisfied the search criteria.Please can anyone know how to achieve this?
Thanks in advance!

To rid yourself of NSDateFormatter's automatic adjustment for your time zone use something like this.
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
To elaborate; "16 Jan 2012" Contains no hour, minute, or seconds value, so when parsing it with an NSDateFormatter one would expect an NSDate with a description of "2012-01-16 00:00:00 +000" But that's not what you're getting because the date formatter is adjusting for the 5:30 Hours between GMT and India?(Assuming based on :30 differential). By setting the date formatter's time zone explicitly you avoid this problem.

You need to use NSDateFormatters to convert the date string from one format to another. You can have an inputDateFormatter where you use dateFromString to give you an NSDate, and then feed this into an outputDateFormatter which gives your desired string.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *dateInFormatYouWant = [dateFormatter stringFromDate:yourDate]
I'm not sure if there is a more efficient way of doing it than this.

Related

Make NSDateFormatter emit the string "UTC" instead of the string "GMT"?

I have the following Objective-C code that attempts to generate a string with the current date/time in UTC:
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *utc = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:utc];
[dateFormatter setDateFormat:#"YYYY-MM-dd--HH-mm-ss zzz"];
NSString *utcDateString = [dateFormatter stringFromDate:currentDate];
This code almost generates what I want. The resulting value of utcDateString on my system is:
2019-06-17--22-28-13 GMT
However, my desired output includes the string "UTC", not "GMT", as in:
2019-06-17--22-28-13 UTC
Is there a way to get the NSDateFormatter to emit the string "UTC" for the zzz portion of the date format? I'd prefer not to have to resort to leaving the "zzz" off of my date format string, and then manually append a "UTC" onto the resulting date string.
There is no date format specifier that returns UTC.
Since you are hardcoding the UTC timezone for the formatter, then simply hardcode the string UTC in the date format.
Also note that you want yyyy, not YYYY for the year.
[dateFormatter setDateFormat:#"yyyy-MM-dd--HH-mm-ss 'UTC'"];
If you need to handle other timezones and want GMT to appear as UTC, then use zzz (as you already are) and use string replacement on the result to convert GMT to UTC.
One other possible idea to consider is to use some number of X for the timezone specifier. This will give the timezone offset as numbers but if the timezone offset is 0, then it results in Z (for Zulu).

How to show human readable NSDate using NSDateFormatter

I need to show human readable NSDate localized Format.
Currently, NSDateFormatter can only format one date at a time.
But, I could find any example on how to do two dates format.
Here are some examples of format I need to produce.
10:00 - 11:00AM
Monday 25th March, 2013
10:00AM - 11:00PM
Monday 25th March, 2013
and also these need to be in localized format depend on locale and timezone.
Could any one shade some lights here ? Thank you very much.
Regardless of how you will approach this, you will have to take two strings and combine them together.
When it boils down to it, you ultimately have two dates:
10:00 - 11:00AM Monday 25th March, 2013
^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Date1 Date2
You can use two separate NSDateFormatter objects to give you what you need.
NSDateFormatter *timeOnlyDateFormatter = [[NSDateFormatter alloc] init];
//... set the format for the timeOnlyDateFormatter here
NSDateFormatter *fullDateFormatter = [[NSDateFormatter alloc] init];
//.. set the format for the fullDateFormatter here
With this, you can use them to create both the Date1 string and Date2 string, then combine them.
NSString *firstString = [timeOnlyDateFormatter stringFromDate:firstDate];
NSString *secondString = [fullDateFormatter stringFromDate:secondDate];
NSString *combined = [NSString stringWithFormat:#"%# - %#", firstString, secondString];
There's no real pretty way to do this as far as I know.

NSDate with no time

I have written the following method:
- (NSDate *)stringToDate:(NSString *)dateString
{
// Convert string to date
NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"America/New_York"];
[dateFormat setDateFormat:#"M/d/yyyy HH:mm:ss"];
[dateFormat setTimeZone:tz];
NSDate * date = [dateFormat dateFromString:[NSString stringWithFormat:#"%# 00:00:00", dateString]];
return dateOnly;
}
When I call this with just a date such as 11/1/2013 or 11/13/2013 I get 2013-11-01 04:00:00 +0000 and 2013-11-13 05:00:00 +0000.
If I set a breakpoint on the return the date appears right, but if I break at in the calling function after this call, the date is returned with the time.
How come my time is not always 0. Can anyway tell me what is wrong in my function?
Thank you
UPDATE:
The input string is as follows: 11/1/2013 and 11/13/2013
NSDate is a point in time. It will always have a time component.
And if not printed as a string form a NSDateFormatter, the Date and time will always be the one of UTC/GMT.
The format and the date string must fit.
NSString *dateString = #"11/1/2013";
[dateFormat setDateFormat:#"M/d/yyyy"];
The one hour apart comes from the Daylight saving time. Till November, 3rd 2013 New York has Summer time. http://www.timeanddate.com/worldclock/clockchange.html?n=179
Ok, so can I ignore that? I am trying to compare NSDates when I do my comparison fails because of the time part
You should create dates with with a time during the day — i.e. noon — to be save of DST mess and compare those. Use NSComponents for that.
A must-see for any iOS/Cocoa-Developer: the fantastic WWDC 2011 Video "Session 117 - Performing Calendar Calculations".

Using NSDateFormatter on iOS

I am currently using an NSDateFormatter on my application to show the date in the format that i want. My NSDateFormatter looks like this : [formatter setDateFormat:#"MMM dd, yyyy hh:mm"]; which would give me dates like : Sep 02 , 2012 08:30 .
I have 2 questions. i want to show the time in military time how can i do that with the formatter? I know how to show the AM/PM but i would like the time above to look 20:30. Secondly how can i make the month appear as a number? September would be 09 .
This site has a nice table with the format specifiers you can use, follow up from that.
In this case, you should use [formatter setDateFormat:#"MM dd, yyyy HH:mm"]; To get 09 02, 2012 20:30
Use capital H for the hour and only two M's for the month.
[formatter setDateFormat:#"MM dd, yyyy HH:mm"];

NSDateFormatter problem

I am trying to get NSDate from string but its returning nil.
I tried this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormatter dateFromString:#"2010-05-07T10:12:32UTC"];
NSLog(#"Success date=%#",[date description]);
Thanks
Your date format string expects the date to end with a literal Z. But your date ends with the string UTC. There are several ways to fix this.
Change your date format string to #"yyyy-mm-dd'T'HH:mm:ss'UTC'".
Or, change your date string to #"2010-05-07T10:12:32Z".
Or, you could change your date format string to: #"yyyy-mm-dd'T'HH:mm:ssz". This will recognize some common 3-letter time zone names, such as PDT for Pacific Daylight Time. Unfortunately, however, it will not recognize UTC as a time zone name (you’d have to use “GMT”).
The literal 'Z' won't match the string UTC.