1
votes

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.

  1. 10:00 - 11:00AM Monday 25th March, 2013

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

1
Sometimes you have to write your own code. - Hot Licks

1 Answers

2
votes

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.