반응형
iOS에서 두 날짜 사이의 시간(시간)을 계산하는 방법
iOS에서 두 번(다른 날에 발생할 수 있음) 사이의 시간을 어떻게 계산할 수 있습니까?
NSDate 함수 시간IntervalSinceDate: 두 날짜의 차이를 초 단위로 표시합니다.
NSDate* date1 = someDate;
NSDate* date2 = someOtherDate;
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
Apple 참조 라이브러리 http://developer.apple.com/library/mac/navigation/ 을 참조하거나 Xcode를 사용하는 경우 메뉴에서 도움말/설명을 선택하십시오.
참조: n회 간격(초)을 분으로 변환하는 방법
--편집: 서머타임/리프초를 올바르게 처리하는 방법은 아래 드뢰빌의 답변을 참조하십시오.
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
필요한 구성 요소를 원하는 날짜, 시간 또는 분 단위로 변경합니다.한다면NSDayCalendarUnit
선택한 다음 두 날짜 사이의 일 수를 반환합니다.NSHourCalendarUnit
그리고.NSMinuteCalendarUnit
스위프트 4 버전
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!
-(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;
}
체크아웃 : iOS 달력을 사용하여 계산한 것처럼 일광 절약, 윤년을 처리합니다.시간과 일이 포함된 분을 포함하도록 문자열과 조건을 변경할 수 있습니다.
+(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 @"";
}
swift 3 이상을 사용하시는 분들은
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
언급URL : https://stackoverflow.com/questions/4084341/how-to-calculate-time-in-hours-between-two-dates-in-ios
반응형
'sourcecode' 카테고리의 다른 글
Gem native 확장을 빌드하지 못했습니다(Compass 설치). (0) | 2023.06.02 |
---|---|
div 내에서 텍스트 수직 정렬 (0) | 2023.06.02 |
목표 C 문자열에서 정수 구문 분석 (0) | 2023.06.02 |
디렉터리 구조를 복사하지만 특정 파일만 포함하는 방법(Windows 배치 파일 사용) (0) | 2023.06.02 |
SQL Server에서 시작/끝 블록 및 Go 키워드를 사용하시겠습니까? (0) | 2023.06.02 |