0

I have a very annoying challenge in my app. The client wants to display Days, Hours and Minutes in 3 separate UIPickers - these values together represent a recurring event in time.

So I need to translate these values into an NSDate which I can then later use to set alerts and calculate number of days until this event etc.

Any ideas how to convert and combine these values into the correct NSDate.

e.g. @"Tues" + "15" + "00" would be tomorrow (6 Sept 2011 at 3PM) etc.

TheLearner
  • 19,387
  • 35
  • 95
  • 163

1 Answers1

-1

Combine each value into one string using: [NSString stringWithFormat:@"%s%d%d", dayString, monthInt, yearInt]

Then use NSDateFormatter.. Like in this post: convert string to date


So your problem is that you have no data for MONTH and YEAR (or even a precice day). You could GUESS that the user means some day this month and this year, but it will not be correct in all situations.

Correct NSDate needs (day,) month, year and timezone - in addition to what you now get from the UIPickers.

e.g. @"Tues" + "15" + "00" would be tomorrow (6 Sept 2011 at 3PM)

It can also be 13 Sept 2011 at 3PM, or 7 Sept 2021 at 3PM etc..

Unless you are sure its always the next possible day, it won't be the correct date!

On the first day in a month you select a monday - then there can be 4 or 5 possible mondays. First month of year - 12 possible months. Then years..

But if the next possible day is "correct enough": Get todays date and get a string with what you need from it (month, year, timezone).

Loop from today until a matching dayname is found, and also check that you are in the same year.

So if you select a dayname+time at 23:59 the last day of the year - the next possibly correct date is next year. (NSDateComponents can be useful to get valid dates and add +1 year etc..).

Not always possible to convince clients - but I would try to ask if they could live with different pickers so they can get a correct date..

Community
  • 1
  • 1
EO2
  • 364
  • 1
  • 7