How to show human readable NSDate using NSDateFormatter - objective-c

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.

Related

Converting NSString to NSDate adds one year in some cases

I have some issues converting an NSString to NSDate since the end of the year. The code have always worked great before, but it suddenly started to behave wierd...
For example 2013-01-05 becomes 2014-01-05 when converted to NSDate.
Since it's a whole year it doesn't feel like it's the timezone spooking.
It's not doing this with dates from 2012.
Does anybody have an idea of what might be wrong?
Code:
NSString *dateString = postInfo.noteDate;
NSString *newDateString = [dateString substringToIndex:10];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:newDateString];
newDateString returns 2013-01-05
date returns 2014-01-05
From the docs:
Y 1..n 1997 Year (in "Week of Year" based calendars). Normally the length specifies the padding, but for two letters it also specifies the maximum length. This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.
y 1..n 1996 Year. Normally the length specifies the padding, but for two letters it also specifies the maximum length.
So you want 'yyyy'
This 'bug' is also discussed in the fantastic WWDC 2011 Video "Session 117 - Performing Calendar Calculations", a must-see for any iOS/Cocoa-Developer.
Wikipedia article on ISO 8601
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:newDateString];
You create a NSDate and than you create another and overwrite the first one. Just do
NSDate *date = [dateFormat dateFromString:newDateString];
use yyyy for year not YYYY, which gives week year. see the ISO standard.
Use yyyy in small letters, YYYY is another thing:
Year (in "Week of Year" based calendars). This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.
see http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns
I hope this will helps u. Try this
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}

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".

how to know that October 2012 starts with monday in objective c

I'm new to this Objective C. I'm trying to do an Calendar application.
Could anyone please let me know how to know on which day the month starts? (Suppose, for example, October 2012 month starts on Monday and November 2012 starts on Thursday. Like that how to know the start day of September 2014?).
Also please let me know how to get the start day (Monday or tuesday...) of the month based on the month.
You can try with this
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
//-- get our date from a string representation
NSDate *date2 = [formatter dateFromString:#"2014-09-01 00:00:00"];
//-- convert it so to extract the full day name
[formatter setDateFormat:#"cccc"];
NSString *result = [formatter stringFromDate:date2];

How to convert NSString to NSDate?

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.

NSDateFormatter NSString w/ day suffix to NSDdate

I'm trying to use only a NSDateFormatter to format string representation of a date/time to an NSDate object. The issue is that I can't figure out how to allow the ordinal suffixes in the format.
So lets say I have a string
"Wednesday, August 11th 2010 8:00 PM"
What one line NSDate dateFormat should I use?
Like "EEEE, MMM dd'th' yyyy h:mm a" would work, but that will only work for ordinal days ending in 'th', whereas i need a single format that will allow for 1st, 2nd, 3rd, 4th 5th etc.
It seems like a wildcard character in the format string to accomplish this. I've tried a few things like: % * ?
This is apparently not possible with the NSDateFormatter.
You want to use an NSDateFormatter like so:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle: NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
Either NSDateFormatterLongStyle or NSDateFormatterFullStyle should get you the results you're looking for.