日期和时间比较,包括iphone sdk中的AM/PM
我需要比较两个日期和时间(上午/下午)。我使用了以下代码日期和时间比较,包括iphone sdk中的AM/PM,iphone,compare,nsdate,nsdateformatter,Iphone,Compare,Nsdate,Nsdateformatter,我需要比较两个日期和时间(上午/下午)。我使用了以下代码 -(NSDate*)dateFromString:(NSString*)dateString{ // Convert string to date object NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd H:mm:ss"]; NSDate *date
-(NSDate*)dateFromString:(NSString*)dateString{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd H:mm:ss"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat release];
return date;
}
#define kMinute 60
#define kHour (60*60)
#define kDay (60*60*24)
-(NSString*)textFromSeconds:(double)seconds{
if(seconds < kMinute)
return [NSString stringWithFormat:@"%0.0f seconds",seconds];
if(seconds < kHour)
return [NSString stringWithFormat:@"%0.0f minutes",seconds/kMinute];
if(seconds < kDay)
return [NSString stringWithFormat:@"%0.0f hours",seconds/kHour];
return [NSString stringWithFormat:@"%0.0f days", seconds/kDay];
}
-(NSString*)textFromDateString:(NSString*)dateString{
NSDate *date =[self dateFromString:dateString];
double seconds = [[NSDate date] timeIntervalSinceDate:date];
NSString *text = [self textFromSeconds:seconds];
NSLog(@"text is: %@", text);
return text;
}
-(void)test{
[self textFromDateString:@"2011-02-20 17:19:25"];
[self textFromDateString:@"2011-02-20 17:10:25"];
[self textFromDateString:@"2011-02-20 14:04:25"];
[self textFromDateString:@"2011-02-19 14:04:25"];
}
-(NSDate*)dateFromString:(NSString*)dateString{
//将字符串转换为日期对象
NSDateFormatter*dateFormat=[[NSDateFormatter alloc]init];
[日期格式setDateFormat:@“yyyy-MM-dd H:MM:ss”];
NSDate*date=[dateFormat dateFromString:dateString];
[日期格式发布];
返回日期;
}
#定义kMinute 60
#定义kHour(60*60)
#定义工作日(60*60*24)
-(NSString*)textFromSeconds:(双)秒{
如果(秒<公里分钟)
返回[NSString stringWithFormat:@“%0.0f秒”,秒];
如果(秒
它工作得很好。但我需要把它转换成12小时的时钟。所以我需要比较上午和下午的两个日期。我应该在上面的代码中做什么修改来实现这个目标
我将dateformat更改为@“yyyy-MM-dd HH:MM a”,并尝试使用相同格式的date,但它不起作用
谢谢。尝试将日期格式更改为@“yyyy-MM-dd hh:MM a”