将包含日期的 NSString 转换为天前

发布于 2024-12-09 16:56:25 字数 465 浏览 1 评论 0原文

可能的重复:
Objective-C 中的模糊日期算法

我有一个 NSString 从包含日期的 xml 中解析,例如

5 月 10 日星期一 23:26:22 +0000 2010

我很难将 NSString“日期”转换为更有效的内容,例如

5小时前

昨天

一周前

如果有人能指出我如何尝试此操作的正确方向,我将不胜感激。

Possible Duplicate:
Fuzzy Date algorithm in Objective-C

I have a NSString parsed from a xml that contains a date such as

Mon May 10 23:26:22 +0000 2010

I am having a difficult time converting that NSString "date" to something more efficient, such as

5hrs ago

Yesterday

a Week ago

If anyone can point me in the right direction as how to attempt this it would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

樱花细雨 2024-12-16 16:56:25

我完全明白你的意思。试试这个,我一直在用这个&它非常适合我 -

用法 - [Utils humanDates:timeSinceEpoch];

+ (NSString *)humanDates:(NSString *)origDateInEpochTime
{
    if([origDateInEpochTime length]<= 0)
        return @"never";

    NSDate *convertedDate = [NSDate dateWithTimeIntervalSince1970:[origDateInEpochTime doubleValue]/1000];
    NSDate *todayDate     = [NSDate date];
    double ti             = [convertedDate timeIntervalSinceDate:todayDate];
    ti = ti * -1;
    if(ti < 1)
    {
        return @"never";
    }
    else if(ti < 60)
    {
        return @"less than a minute ago";
    }
    else if(ti < 3600)
    {
        int diff = round(ti/60);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d minute ago", diff];
        else
            return [NSString stringWithFormat:@"%d minutes ago", diff];
    }
    else if(ti < 86400)
    {
        int diff = round(ti/60/60);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d hour ago", diff];
        else
            return [NSString stringWithFormat:@"%d hours ago", diff];
    }
    else if(ti < 2629743)
    {
        int diff = round(ti/60/60/24);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d day ago", diff];
        else
            return [NSString stringWithFormat:@"%d days ago", diff];
    }
    else if(ti < 31104000)
    {
        int diff = round(ti/60/60/24/30);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d month ago", diff];
        else
            return [NSString stringWithFormat:@"%d months ago", diff];
    }
    else if(ti < 311040000)
    {
        int diff = round(ti/60/60/24/30/12);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d year ago", diff];
        else
            return [NSString stringWithFormat:@"%d years ago", diff];
    }
    else
    {
        int diff = round(ti/60/60/24/30/12/10);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d decade ago", diff];
        else
            return [NSString stringWithFormat:@"%d decades ago", diff];
    }
}

I know exactly what you mean. Try this, I have been using this & it works perfectly for me -

Usage - [Utils humanDates:timeSinceEpoch];

+ (NSString *)humanDates:(NSString *)origDateInEpochTime
{
    if([origDateInEpochTime length]<= 0)
        return @"never";

    NSDate *convertedDate = [NSDate dateWithTimeIntervalSince1970:[origDateInEpochTime doubleValue]/1000];
    NSDate *todayDate     = [NSDate date];
    double ti             = [convertedDate timeIntervalSinceDate:todayDate];
    ti = ti * -1;
    if(ti < 1)
    {
        return @"never";
    }
    else if(ti < 60)
    {
        return @"less than a minute ago";
    }
    else if(ti < 3600)
    {
        int diff = round(ti/60);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d minute ago", diff];
        else
            return [NSString stringWithFormat:@"%d minutes ago", diff];
    }
    else if(ti < 86400)
    {
        int diff = round(ti/60/60);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d hour ago", diff];
        else
            return [NSString stringWithFormat:@"%d hours ago", diff];
    }
    else if(ti < 2629743)
    {
        int diff = round(ti/60/60/24);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d day ago", diff];
        else
            return [NSString stringWithFormat:@"%d days ago", diff];
    }
    else if(ti < 31104000)
    {
        int diff = round(ti/60/60/24/30);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d month ago", diff];
        else
            return [NSString stringWithFormat:@"%d months ago", diff];
    }
    else if(ti < 311040000)
    {
        int diff = round(ti/60/60/24/30/12);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d year ago", diff];
        else
            return [NSString stringWithFormat:@"%d years ago", diff];
    }
    else
    {
        int diff = round(ti/60/60/24/30/12/10);
        if(diff == 1)
            return [NSString stringWithFormat:@"%d decade ago", diff];
        else
            return [NSString stringWithFormat:@"%d decades ago", diff];
    }
}
逐鹿 2024-12-16 16:56:25

我这样做

首先我写下这样的功能

NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];

[dateForm setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *dateSelected = [dateForm dateFromString:[NSString stringWithFormat:@"%@",[arr objectAtIndex:0]]];

NSInteger nDiffDate = [self howManyDaysHavePast:dateSelected today:[NSDate date]];

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

 [dateFormatter setDateFormat:@"EEEE"];

 NSString *dayName = [dateFormatter stringFromDate:dateSelected];

[dateFormatter release];
[dateForm release];

            if (nDiffDate ==0) {
                dateLbl.text   = [[NSString stringWithFormat:@"%@",[recentHisotryArr objectAtIndex:0]]substringFromIndex:11];

            }
            else if(nDiffDate==1)
            {
            dateLbl.text = @"Yesterday";

            }

            else if(nDiffDate==2)
            {

                dateLbl.text = dayName;

            } 
            else if(nDiffDate==3)
            {
                dateLbl.text = dayName;                
            } 
            else if(nDiffDate==4)
            {
                dateLbl.text = dayName;                
            } 
            else if(nDiffDate==5)
            {
                dateLbl.text = dayName;                
            } 
            else if(nDiffDate==6)
            {
                dateLbl.text = dayName;                
            } 

-(int)howManyDaysHavePast:(NSDate*)lastDate today:(NSDate*)today 
{
    NSDate *startDate = lastDate;
    NSDate *endDate = today;
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned int unitFlags = NSDayCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags
                                                fromDate:startDate
                                                  toDate:endDate options:0];
    int days = [components day];
    return days;
}

,它对我有用,就像iPhone上的拨号应用程序上的日期提醒一样

i do in this way

first i write down function like this

NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];

[dateForm setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *dateSelected = [dateForm dateFromString:[NSString stringWithFormat:@"%@",[arr objectAtIndex:0]]];

NSInteger nDiffDate = [self howManyDaysHavePast:dateSelected today:[NSDate date]];

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

 [dateFormatter setDateFormat:@"EEEE"];

 NSString *dayName = [dateFormatter stringFromDate:dateSelected];

[dateFormatter release];
[dateForm release];

            if (nDiffDate ==0) {
                dateLbl.text   = [[NSString stringWithFormat:@"%@",[recentHisotryArr objectAtIndex:0]]substringFromIndex:11];

            }
            else if(nDiffDate==1)
            {
            dateLbl.text = @"Yesterday";

            }

            else if(nDiffDate==2)
            {

                dateLbl.text = dayName;

            } 
            else if(nDiffDate==3)
            {
                dateLbl.text = dayName;                
            } 
            else if(nDiffDate==4)
            {
                dateLbl.text = dayName;                
            } 
            else if(nDiffDate==5)
            {
                dateLbl.text = dayName;                
            } 
            else if(nDiffDate==6)
            {
                dateLbl.text = dayName;                
            } 

-(int)howManyDaysHavePast:(NSDate*)lastDate today:(NSDate*)today 
{
    NSDate *startDate = lastDate;
    NSDate *endDate = today;
    NSCalendar *gregorian = [[NSCalendar alloc]
                             initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned int unitFlags = NSDayCalendarUnit;
    NSDateComponents *components = [gregorian components:unitFlags
                                                fromDate:startDate
                                                  toDate:endDate options:0];
    int days = [components day];
    return days;
}

and it work for me like the day reminder on the dial app on the iphone

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