2011-08-25 10 views
5

come posso ottenere l'NSDate di ieri in iOS?Come posso ottenere l'NSDate di ieri

Grazie

+0

http://stackoverflow.com/questions/1889164/get-nsdate-today-yesterday-this-week-last-week-this-month-last-month-vari –

risposta

15

Ecco un modo per farlo:

NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; 
[components setDay:-1]; 

NSDate *yesterday = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:[NSDate date] options:0]; 

Date un'occhiata al Date and Time programming guide.

+0

Non sottrarre un numero negativo lo rende aggiungendo 1 giorno? – honcheng

+2

Il metodo dateByAddingComponents * aggiunge * – progrmr

+0

@progrmr, Grazie mille. –

Problemi correlati