简体   繁体   English

计算目标C中的时差

[英]Calculating time difference in Objective C

I have 2 times values in NSString either in 12 hour format(10-09 PM) or 24 hour format (18-12). 我在NSString中有2次以12小时格式(10-09 PM)或24小时格式(18-12)的值。 How to calculate time difference between two? 如何计算两者之间的时差? (two dates will be either in 12 hour or 24 hour format, not mixed format) (两个日期将是12小时或24小时格式,不是混合格式)

NSDateFormatter *df=[[NSDateFormatter alloc] init];
// Set the date format according to your needs
[df setDateFormat:@"MM/dd/YYYY hh:mm a"]; //for 12 hour format
//[df setDateFormat:@"MM/dd/YYYY HH:mm "]  // for 24 hour format
NSDate *date1 = [df dateFromString:firstDateString];
NSDate *date2 = [df dateFromString:secondDatestring];
NSLog(@"%@f is the time difference",[date2 timeIntervalSinceDate:date1]);
[df release];

hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM