una tiempo segundos persona online minutos meses horas fechas entre edad dias contador como calcular calculadora años iphone ios

iphone - segundos - Cómo calcular el tiempo en horas entre dos fechas en iOS



como calcular la edad de una persona en años meses y dias (5)

La función NSDate timeIntervalSinceDate: le dará la diferencia de dos fechas en segundos.

NSDate* date1 = someDate; NSDate* date2 = someOtherDate; NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2]; double secondsInAnHour = 3600; NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;

Vea, la biblioteca de referencia de apple http://developer.apple.com/library/mac/navigation/ o si está usando Xcode simplemente seleccione help / documentation del menú.

Ver: how-to-convert-an-nstimeinterval-seconds-into-minutes

--edit: consulta la respuesta de ÐąrέÐέvil a continuación para un manejo correcto del horario de verano / segundos de salto

¿Cómo puedo calcular el tiempo transcurrido en horas entre dos veces (posiblemente en días diferentes) en iOS?


Pago: se ocupa del horario de verano, año bisiesto, ya que usó el calendario de iOS para calcular. Puede cambiar la cadena y las condiciones para incluir minutos con horas y días.

+(NSString*)remaningTime:(NSDate*)startDate endDate:(NSDate*)endDate { NSDateComponents *components; NSInteger days; NSInteger hour; NSInteger minutes; NSString *durationString; components = [[NSCalendar currentCalendar] components: NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute fromDate: startDate toDate: endDate options: 0]; days = [components day]; hour = [components hour]; minutes = [components minute]; if (days > 0) { if (days > 1) { durationString = [NSString stringWithFormat:@"%d days", days]; } else { durationString = [NSString stringWithFormat:@"%d day", days]; } return durationString; } if (hour > 0) { if (hour > 1) { durationString = [NSString stringWithFormat:@"%d hours", hour]; } else { durationString = [NSString stringWithFormat:@"%d hour", hour]; } return durationString; } if (minutes > 0) { if (minutes > 1) { durationString = [NSString stringWithFormat:@"%d minutes", minutes]; } else { durationString = [NSString stringWithFormat:@"%d minute", minutes]; } return durationString; } return @""; }


Para aquellos que usan swift 3 y superior,

let dateString1 = "2018-03-15T14:20:00.000Z" let dateString2 = "2018-03-20T14:20:00.000Z" let Dateformatter = DateFormatter() Dateformatter.dateFormat = "yyyy-MM-dd''T''HH:mm:ss.SSSZ" let date1 = Dateformatter.date(from: dateString1) let date2 = Dateformatter.date(from: dateString2) let distanceBetweenDates: TimeInterval? = date2?.timeIntervalSince(date1!) let secondsInAnHour: Double = 3600 let secondsInDays: Double = 86400 let secondsInWeek: Double = 604800 let hoursBetweenDates = Int((distanceBetweenDates! / secondsInAnHour)) let daysBetweenDates = Int((distanceBetweenDates! / secondsInDays)) let weekBetweenDates = Int((distanceBetweenDates! / secondsInWeek)) print(weekBetweenDates,"weeks")//0 weeks print(daysBetweenDates,"days")//5 days print(hoursBetweenDates,"hours")//120 hours


-(NSMutableString*) timeLeftSinceDate: (NSDate *) dateT { NSMutableString *timeLeft = [[NSMutableString alloc]init]; NSDate *today10am =[NSDate date]; NSInteger seconds = [today10am timeIntervalSinceDate:dateT]; NSInteger days = (int) (floor(seconds / (3600 * 24))); if(days) seconds -= days * 3600 * 24; NSInteger hours = (int) (floor(seconds / 3600)); if(hours) seconds -= hours * 3600; NSInteger minutes = (int) (floor(seconds / 60)); if(minutes) seconds -= minutes * 60; if(days) { [timeLeft appendString:[NSString stringWithFormat:@"%ld Days", (long)days*-1]]; } if(hours) { [timeLeft appendString:[NSString stringWithFormat: @"%ld H", (long)hours*-1]]; } if(minutes) { [timeLeft appendString: [NSString stringWithFormat: @"%ld M",(long)minutes*-1]]; } if(seconds) { [timeLeft appendString:[NSString stringWithFormat: @"%lds", (long)seconds*-1]]; } return timeLeft; }


NSCalendar *c = [NSCalendar currentCalendar]; NSDate *d1 = [NSDate date]; NSDate *d2 = [NSDate dateWithTimeIntervalSince1970:1340323201];//2012-06-22 NSDateComponents *components = [c components:NSHourCalendarUnit fromDate:d2 toDate:d1 options:0]; NSInteger diff = components.minute; NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit

Cambie los componentes necesarios por día, hora o minuto, qué diferencia desea. Si se selecciona NSDayCalendarUnit , devolverá el número de días entre dos fechas de forma similar para NSHourCalendarUnit y NSMinuteCalendarUnit

Swift 4 versión

let cal = Calendar.current let d1 = Date() let d2 = Date.init(timeIntervalSince1970: 1524787200) // April 27, 2018 12:00:00 AM let components = cal.dateComponents([.hour], from: d2, to: d1) let diff = components.hour!