In my app I would like to allow the user to enter a value only once a day.
How do I make sure the the stored NSDate is not within the current day? Can I compare the day value of two NSDate while ignoring the time?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Dates are just wrappers around NSTimeIntervals. They have no concept of local time, so you can’t get a day value out of them directly.
You could use NSDateFormatter, like this:
Date formatters output the date using your local timezone, so you can guarantee that the day will be correct for the user.
If you actually want to ensure that the events are more than 24 hours apart (to prevent cheating) it’s much easier, just do this:
UPDATE: As pointed out by abbood in the comments, it can be expensive/slow to create NSDateFormatter objects, so you should hang onto them if they will be reused.
It isn’t a good idea to store them in a static variable or singleton because they are not thread-safe, but you can store them in the current thread’s threadDictionary, like this: