日期和时间比较,包括 iphone sdk 中的 AM/PM

发布于 2024-12-27 22:33:24 字数 1513 浏览 2 评论 0原文

我需要比较两个日期与时间(上午/下午)。我使用了以下代码。

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾北清歌寒 2025-01-03 22:33:24

尝试将日期格式更改为@“yyyy-MM-dd hh:mm a”

Try changing the dateformat to @"yyyy-MM-dd hh:mm a"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文