日期和时间比较,包括 iphone sdk 中的 AM/PM
我需要比较两个日期与时间(上午/下午)。我使用了以下代码。
-(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"];
}
它工作完美。但我需要将其转换为 12 小时制。所以我需要比较两个日期的上午或下午。我应该对上面的代码进行哪些修改才能达到目标?
我将日期格式更改为 @"yyyy-MM-dd HH:mm a" 并尝试使用相同格式的日期,但它不起作用。
谢谢。
I need to compare two dates with time (am/pm). I used the following code.
-(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"];
}
Its working perfect. But I need to convert this into 12 hour clock. So that I need to compare two dates with AM or PM. What modifications should I do in the above code to achieve the goal?
I changed the dateformat to @"yyyy-MM-dd HH:mm a" and tried with date in the same format, But its not working.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将日期格式更改为@“yyyy-MM-dd hh:mm a”
Try changing the dateformat to @"yyyy-MM-dd hh:mm a"