-3

i build my app with home page input a date-picker i get the date but i want get the date before 1 or 2 day from the date original please help me . i set the date original in label. but in label2 i want set date before one day

this code is :

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"DD-MM-YYYY"];
NSString *value = [dateFormatter stringFromDate:date];
self.labelDateSelected.text = value;
benvd
  • 5,776
  • 5
  • 39
  • 59
  • 2
    What code have you tried to use to do the date calculation? All you are showing is date formatter code. – Abizern Feb 17 '14 at 13:11
  • 1
    Wow, you just repeated the question (with less information than the original one). – Martin R Feb 17 '14 at 13:16
  • Try Google search for NSDateComponents. It should help you. After spending considerate time and research, try SO again with some research to show your effort. – Puneet Sharma Feb 17 '14 at 13:17

2 Answers2

1
NSDate *today = [NSDate date]; // This is 'now'

NSDateComponents *components = [NSDateComponents new];

[components setDay:-1];

NSDate *yesterday = [[NSCalendar currentCalendar] dateByAddingComponents:components
                                                                  toDate:today
                                                                 options:kNilOptions];

yesterday will now be an NSDate object that is 1 day before now.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • i no want work with nsdate i want to substract one day from the date getted form date-picker – user3309691 Feb 17 '14 at 13:19
  • If you're not going to work with NSDates - how do you expect to perform calculations. You can get the date from the date picker and use the same method to get a date before or after that date. – Abizern Feb 17 '14 at 13:21
  • what is this some method ? – user3309691 Feb 17 '14 at 13:23
  • I've just shown you a way of subtracting days from a date. If you can't understand this, then I think you need to build up your understanding of Cocoa. – Abizern Feb 17 '14 at 13:25
-1

[NSDate dateWithTimeIntervalSinceNow:-24*60*60]; will return yesterday's date, at the same time as today.

Guilherme
  • 7,839
  • 9
  • 56
  • 99
  • This is (sorry!) a bad answer. A day does not always have 86400 seconds. – Martin R Feb 17 '14 at 13:13
  • and how i can set this in label ?? thank you – user3309691 Feb 17 '14 at 13:13
  • It's a really bad idea to perform date calculation with seconds. – Abizern Feb 17 '14 at 13:14
  • no i have a date-picker in my app and i get the date from date-picker and make in label but i have a seconde label i want set the day ago from the day original – user3309691 Feb 17 '14 at 13:16
  • @MartinR I understand and agree that Abizem's answers is better, but why do you say "A day does not always have 86400 seconds."? – Guilherme Feb 17 '14 at 13:27
  • 1
    @Guilherme: Think of [Daylight savings time](http://en.wikipedia.org/wiki/Daylight_saving_time). When you switch from wintertime to summertime, the clock is adjusted one hour forward, so that *that day* has only 23 hours. When you switch back from summertime to wintertime, you have one day with 25 hours. – Martin R Feb 17 '14 at 13:50
  • Leap years, Daylight savings time, leap seconds… – Abizern Feb 17 '14 at 13:50
  • @Abizern: Leap seconds do not play a role in Unix time calculations. See http://en.wikipedia.org/wiki/Unix_time: *"The Unix time number increases by exactly 86400 each day, regardless of how long the day is."* (Which is a bad formulation due to daylight saving time.) – Martin R Feb 17 '14 at 13:51
  • Good point - Neither does daylight savings time - which is why NSDate and NSLocale and NSCalendar need to be used together for real world calendrical calculations. My point is (and I think you also agree) that adding and subtracting seconds is not a good way to perform time calculations. – Abizern Feb 17 '14 at 13:52
  • @Abizern: I totally agree (for all calculations involving days and larger units). My (minor) point was only that leap seconds are *not* an argument to use NSCalendar etc. – Martin R Feb 17 '14 at 13:57