如何在 Objective-C 中格式化日期,类似于 jquery.timeago 库?

发布于 2024-12-10 17:26:40 字数 652 浏览 4 评论 0原文

我有一个在表格单元格中显示的项目提要,其中一部分是过去的日期/时间戳。

在 Objective-C 中,如何以与网络上的 jquery.timeago 插件 相同的方式完成它们的格式化?

也就是说,接受一个日期并输出如下内容:

  • “刚刚”
  • “2 分钟前”
  • “24 天前”
  • “一个月前”

我看到有一个 这里是 NSDate 扩展类,其中包含 dateWithDaysBeforeNow、dateWithMinutesBeforeNow 等方法,等等,但如果有一个图书馆已经做到了这一点,我会使用它。

编辑:此外,如果有人编写一个方法(获取日期,返回一个字符串)来完成此操作,无论是使用此链接的扩展库还是其他方法,我都会奖励他们答案。

编辑 2 赏金奖励给任何能够在 Objective-C 中编写模糊日期算法的人。

I have a feed of items displayed in table cells, part of which is a date / timestamp in the past.

In Objective-C, how can I accomplish formatting them in the same manner as the jquery.timeago plugin on the web?

That is, taking in a date and outputting things like:

  • 'just now'
  • '2 minutes ago'
  • '24 days ago'
  • 'a month ago'

I see there is an NSDate extension class here with methods such as dateWithDaysBeforeNow, dateWithMinutesBeforeNow, etc, but if there is a library out there that has already done this I will use it.

EDIT: Further to this, if someone composes a method (takes a date, returns a string) that accomplishes this, either with this linked extensions library or another method, I will award them the answer.

EDIT 2 Bounty goes to whoever can write the fuzzy date algorithm in Objective-C.

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

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

发布评论

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

评论(9

黯然#的苍凉 2024-12-17 17:26:40

创建静态类方法:

+ (NSString *)stringForTimeIntervalSinceCreated:(NSDate *)dateTime serverTime:(NSDate *)serverDateTime{
    NSInteger MinInterval;
    NSInteger HourInterval;
    NSInteger DayInterval;
    NSInteger DayModules;   

    NSInteger interval = abs((NSInteger)[dateTime timeIntervalSinceDate:serverDateTime]);
    if(interval >= 86400)    
    {
        DayInterval  = interval/86400;
        DayModules = interval%86400;
        if(DayModules!=0)
        {
            if(DayModules>=3600){
                //HourInterval=DayModules/3600;
                return [NSString stringWithFormat:@"%i days", DayInterval];
            }
            else {
                if(DayModules>=60){
                    //MinInterval=DayModules/60;
                    return [NSString stringWithFormat:@"%i days", DayInterval];
                }
                else {
                    return [NSString stringWithFormat:@"%i days", DayInterval];
                }
            }
        }
        else 
        {
        return [NSString stringWithFormat:@"%i days", DayInterval];
        }

    }

    else{

        if(interval>=3600)
        {

            HourInterval= interval/3600;
            return [NSString stringWithFormat:@"%i hours", HourInterval];

        }

        else if(interval>=60){

            MinInterval = interval/60;

            return [NSString stringWithFormat:@"%i minutes", MinInterval];
        }
        else{
            return [NSString stringWithFormat:@"%i Sec", interval];
        }

    }

}

Create Static Class Method:

+ (NSString *)stringForTimeIntervalSinceCreated:(NSDate *)dateTime serverTime:(NSDate *)serverDateTime{
    NSInteger MinInterval;
    NSInteger HourInterval;
    NSInteger DayInterval;
    NSInteger DayModules;   

    NSInteger interval = abs((NSInteger)[dateTime timeIntervalSinceDate:serverDateTime]);
    if(interval >= 86400)    
    {
        DayInterval  = interval/86400;
        DayModules = interval%86400;
        if(DayModules!=0)
        {
            if(DayModules>=3600){
                //HourInterval=DayModules/3600;
                return [NSString stringWithFormat:@"%i days", DayInterval];
            }
            else {
                if(DayModules>=60){
                    //MinInterval=DayModules/60;
                    return [NSString stringWithFormat:@"%i days", DayInterval];
                }
                else {
                    return [NSString stringWithFormat:@"%i days", DayInterval];
                }
            }
        }
        else 
        {
        return [NSString stringWithFormat:@"%i days", DayInterval];
        }

    }

    else{

        if(interval>=3600)
        {

            HourInterval= interval/3600;
            return [NSString stringWithFormat:@"%i hours", HourInterval];

        }

        else if(interval>=60){

            MinInterval = interval/60;

            return [NSString stringWithFormat:@"%i minutes", MinInterval];
        }
        else{
            return [NSString stringWithFormat:@"%i Sec", interval];
        }

    }

}
好倦 2024-12-17 17:26:40

好吧,我昨晚睡不着——我想我会咬人的。这是 jQuery timeAgo 的逐行移植,注释中附有原始 JavaScript 源代码以供参考。使用标准 ObjC/AppKit 机制进行用户默认设置和本地化。与 Web 相关的内容,例如更新现有 DOM 元素,显然被省略了。还省略了可以在设置中放置函数的机制,因为这是 JavaScript 特定的。话不多说:

头文件:

//
//  NSDate+JQTimeAgoAdditions.h
//  JQTimeAgo
//

#import <Foundation/Foundation.h>

@interface NSDate (JQTimeAgoAdditions)

- (NSString*)timeAgo;

@end

extern NSString* const kJQTimeAgoAllowFutureKey;
extern NSString* const kJQTimeAgoStringsPrefixAgoKey;
extern NSString* const kJQTimeAgoStringsPrefixFromNowKey;
extern NSString* const kJQTimeAgoStringsSuffixAgoKey;
extern NSString* const kJQTimeAgoStringsSuffixFromNowKey;
extern NSString* const kJQTimeAgoStringsSecondsKey;
extern NSString* const kJQTimeAgoStringsMinuteKey;
extern NSString* const kJQTimeAgoStringsMinutesKey;
extern NSString* const kJQTimeAgoStringsHourKey;
extern NSString* const kJQTimeAgoStringsHoursKey;
extern NSString* const kJQTimeAgoStringsDayKey;
extern NSString* const kJQTimeAgoStringsDaysKey;
extern NSString* const kJQTimeAgoStringsMonthKey;
extern NSString* const kJQTimeAgoStringsMonthsKey;
extern NSString* const kJQTimeAgoStringsYearKey;
extern NSString* const kJQTimeAgoStringsYearsKey;
extern NSString* const kJQTimeAgoStringsNumbersKey;

和实现文件:

//
//  NSDate+JQTimeAgoAdditions.m
//  JQTimeAgo
//

#import "NSDate+JQTimeAgoAdditions.h"

@implementation NSDate (JQTimeAgoAdditions)

- (NSString*)timeAgo
{
    NSTimeInterval distanceMillis = -1000.0 * [self timeIntervalSinceNow];
//        inWords: function(distanceMillis) {
//            var $l = this.settings.strings;
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
//            var prefix = $l.prefixAgo;
    NSString* prefix = [defs stringForKey: kJQTimeAgoStringsPrefixAgoKey];
//            var suffix = $l.suffixAgo;
    NSString* suffix = [defs stringForKey: kJQTimeAgoStringsSuffixAgoKey];
//            if (this.settings.allowFuture) {
    if ([defs boolForKey: kJQTimeAgoAllowFutureKey])
    {
//                if (distanceMillis < 0) {
        if (distanceMillis < 0.0) 
        {
//                    prefix = $l.prefixFromNow;
            prefix = [defs stringForKey: kJQTimeAgoStringsPrefixFromNowKey];
//                    suffix = $l.suffixFromNow;
            suffix = [defs stringForKey: kJQTimeAgoStringsSuffixFromNowKey];
//                }
        }
//                distanceMillis = Math.abs(distanceMillis);
        distanceMillis = fabs(distanceMillis);            
//            }
    }
//            
//            var seconds = distanceMillis / 1000;
    const double seconds = distanceMillis / 1000.0;
//            var minutes = seconds / 60;
    const double minutes = seconds / 60.0;
//            var hours = minutes / 60;
    const double hours = minutes / 60.0;
//            var days = hours / 24;
    const double days = hours / 24.0;
//            var years = days / 365;
    const double years = days / 365.0;
//            
//            function substitute(stringOrFunction, number) { ... }
    // Use stringWithFormat, etc.
//            
//            var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
    NSString* words = nil;
    if (seconds < 45)
        words = [[defs stringForKey: kJQTimeAgoStringsSecondsKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)round(seconds)]];
//            seconds < 90 && substitute($l.minute, 1) ||
    else if (seconds < 90)
        words = [[defs stringForKey: kJQTimeAgoStringsMinuteKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)1]];

//            minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
    else if (minutes < 45)
        words = [[defs stringForKey: kJQTimeAgoStringsMinutesKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)round(minutes)]];

//            minutes < 90 && substitute($l.hour, 1) ||
    else if (minutes < 90)
        words = [[defs stringForKey: kJQTimeAgoStringsHourKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)1]];

//            hours < 24 && substitute($l.hours, Math.round(hours)) ||
    else if (hours < 24)
        words = [[defs stringForKey: kJQTimeAgoStringsHoursKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)round(hours)]];

//            hours < 48 && substitute($l.day, 1) ||
    else if (hours < 48)
        words = [[defs stringForKey: kJQTimeAgoStringsDayKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)1]];
//            days < 30 && substitute($l.days, Math.floor(days)) ||
    else if (days < 30)
        words = [[defs stringForKey: kJQTimeAgoStringsDaysKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)floor(days)]];

//            days < 60 && substitute($l.month, 1) ||
    else if (days < 60)
        words = [[defs stringForKey: kJQTimeAgoStringsMonthKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)1]];

//            days < 365 && substitute($l.months, Math.floor(days / 30)) ||
    else if (days < 365)
        words = [[defs stringForKey: kJQTimeAgoStringsMonthsKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)floor(days/30.0)]];

//            years < 2 && substitute($l.year, 1) ||
    else if (years < 2)
        words = [[defs stringForKey: kJQTimeAgoStringsYearKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)1]];
//            substitute($l.years, Math.floor(years));
    else
        words = [[defs stringForKey: kJQTimeAgoStringsYearsKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)floor(years)]];
//            
//            return $.trim([prefix, words, suffix].join(" "));
    NSString* retVal = [[NSString stringWithFormat: @"%@ %@ %@", 
                         (prefix ? prefix : @""),
                         (words ? words : @""),
                         (suffix ? suffix : @"")] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return retVal;
//        },
}

// Load settings
+ (void)load
{    
    // Frameworks are guaranteed to be loaded by now so we can use NSDictionary, etc...
    // See here for details: http://www.mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        @autoreleasepool 
        {
            NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [NSNumber numberWithBool: NO], kJQTimeAgoAllowFutureKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsPrefixAgoKey, nil, [NSBundle mainBundle], @" ", @"kJQTimeAgoStringsPrefixAgoKey"), kJQTimeAgoStringsPrefixAgoKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsPrefixFromNowKey, nil, [NSBundle mainBundle], @" ", @"kJQTimeAgoStringsPrefixFromNowKey"), kJQTimeAgoStringsPrefixFromNowKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsSuffixAgoKey, nil, [NSBundle mainBundle], @"ago", @"kJQTimeAgoStringsSuffixAgoKey"), kJQTimeAgoStringsSuffixAgoKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsSuffixFromNowKey, nil, [NSBundle mainBundle], @"from now", @"kJQTimeAgoStringsSuffixFromNowKey"), kJQTimeAgoStringsSuffixFromNowKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsSecondsKey, nil, [NSBundle mainBundle], @"less than a minute", @"kJQTimeAgoStringsSecondsKey"), kJQTimeAgoStringsSecondsKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsMinuteKey, nil, [NSBundle mainBundle], @"about a minute", @"kJQTimeAgoStringsMinuteKey"), kJQTimeAgoStringsMinuteKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsMinutesKey, nil, [NSBundle mainBundle], @"%d minutes", @"kJQTimeAgoStringsMinutesKey"), kJQTimeAgoStringsMinutesKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsHourKey, nil, [NSBundle mainBundle], @"about an hour", @"kJQTimeAgoStringsHourKey"), kJQTimeAgoStringsHourKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsHoursKey, nil, [NSBundle mainBundle], @"about %d hours", @"kJQTimeAgoStringsHoursKey"), kJQTimeAgoStringsHoursKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsDayKey, nil, [NSBundle mainBundle], @"about a day", @"kJQTimeAgoStringsDayKey"), kJQTimeAgoStringsDayKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsDaysKey, nil, [NSBundle mainBundle], @"%d days", @"kJQTimeAgoStringsDaysKey"), kJQTimeAgoStringsDaysKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsMonthKey, nil, [NSBundle mainBundle], @"about a month", @"kJQTimeAgoStringsMonthKey"), kJQTimeAgoStringsMonthKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsMonthsKey, nil, [NSBundle mainBundle], @"%d months", @"kJQTimeAgoStringsMonthsKey"), kJQTimeAgoStringsMonthsKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsYearKey, nil, [NSBundle mainBundle], @"about a year", @"kJQTimeAgoStringsYearKey"), kJQTimeAgoStringsYearKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsYearsKey, nil, [NSBundle mainBundle], @"%d years", @"kJQTimeAgoStringsYearsKey"), kJQTimeAgoStringsYearsKey,
                                      nil];


            [[NSUserDefaults standardUserDefaults] registerDefaults: settings];
        }
    });
}

@end

NSString* const kJQTimeAgoAllowFutureKey = @"kJQTimeAgoAllowFutureKey";
NSString* const kJQTimeAgoStringsPrefixAgoKey = @"kJQTimeAgoStringsPrefixAgoKey";
NSString* const kJQTimeAgoStringsPrefixFromNowKey = @"kJQTimeAgoStringsPrefixFromNowKey";
NSString* const kJQTimeAgoStringsSuffixAgoKey = @"kJQTimeAgoStringsSuffixAgoKey";
NSString* const kJQTimeAgoStringsSuffixFromNowKey = @"kJQTimeAgoStringsSuffixFromNowKey";
NSString* const kJQTimeAgoStringsSecondsKey = @"kJQTimeAgoStringsSecondsKey";
NSString* const kJQTimeAgoStringsMinuteKey = @"kJQTimeAgoStringsMinuteKey";
NSString* const kJQTimeAgoStringsMinutesKey = @"kJQTimeAgoStringsMinutesKey";
NSString* const kJQTimeAgoStringsHourKey = @"kJQTimeAgoStringsHourKey";
NSString* const kJQTimeAgoStringsHoursKey = @"kJQTimeAgoStringsHoursKey";
NSString* const kJQTimeAgoStringsDayKey = @"kJQTimeAgoStringsDayKey";
NSString* const kJQTimeAgoStringsDaysKey = @"kJQTimeAgoStringsDaysKey";
NSString* const kJQTimeAgoStringsMonthKey = @"kJQTimeAgoStringsMonthKey";
NSString* const kJQTimeAgoStringsMonthsKey = @"kJQTimeAgoStringsMonthsKey";
NSString* const kJQTimeAgoStringsYearKey = @"kJQTimeAgoStringsYearKey";
NSString* const kJQTimeAgoStringsYearsKey = @"kJQTimeAgoStringsYearsKey";
NSString* const kJQTimeAgoStringsNumbersKey = @"kJQTimeAgoStringsNumbersKey";

OK, I couldn't sleep last night -- guess I'll bite. Here's a line-for-line port of jQuery's timeAgo, with the original JavaScript source in comments for reference. Uses standard ObjC/AppKit mechanisms for user defaults and localization. Web-related stuff, like updating an existing DOM element is obviously omitted. Also omits the mechanism where you can put a function in the settings, because that's sort of JavaScript specific. Without further ado:

Header file:

//
//  NSDate+JQTimeAgoAdditions.h
//  JQTimeAgo
//

#import <Foundation/Foundation.h>

@interface NSDate (JQTimeAgoAdditions)

- (NSString*)timeAgo;

@end

extern NSString* const kJQTimeAgoAllowFutureKey;
extern NSString* const kJQTimeAgoStringsPrefixAgoKey;
extern NSString* const kJQTimeAgoStringsPrefixFromNowKey;
extern NSString* const kJQTimeAgoStringsSuffixAgoKey;
extern NSString* const kJQTimeAgoStringsSuffixFromNowKey;
extern NSString* const kJQTimeAgoStringsSecondsKey;
extern NSString* const kJQTimeAgoStringsMinuteKey;
extern NSString* const kJQTimeAgoStringsMinutesKey;
extern NSString* const kJQTimeAgoStringsHourKey;
extern NSString* const kJQTimeAgoStringsHoursKey;
extern NSString* const kJQTimeAgoStringsDayKey;
extern NSString* const kJQTimeAgoStringsDaysKey;
extern NSString* const kJQTimeAgoStringsMonthKey;
extern NSString* const kJQTimeAgoStringsMonthsKey;
extern NSString* const kJQTimeAgoStringsYearKey;
extern NSString* const kJQTimeAgoStringsYearsKey;
extern NSString* const kJQTimeAgoStringsNumbersKey;

And the implementation file:

//
//  NSDate+JQTimeAgoAdditions.m
//  JQTimeAgo
//

#import "NSDate+JQTimeAgoAdditions.h"

@implementation NSDate (JQTimeAgoAdditions)

- (NSString*)timeAgo
{
    NSTimeInterval distanceMillis = -1000.0 * [self timeIntervalSinceNow];
//        inWords: function(distanceMillis) {
//            var $l = this.settings.strings;
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
//            var prefix = $l.prefixAgo;
    NSString* prefix = [defs stringForKey: kJQTimeAgoStringsPrefixAgoKey];
//            var suffix = $l.suffixAgo;
    NSString* suffix = [defs stringForKey: kJQTimeAgoStringsSuffixAgoKey];
//            if (this.settings.allowFuture) {
    if ([defs boolForKey: kJQTimeAgoAllowFutureKey])
    {
//                if (distanceMillis < 0) {
        if (distanceMillis < 0.0) 
        {
//                    prefix = $l.prefixFromNow;
            prefix = [defs stringForKey: kJQTimeAgoStringsPrefixFromNowKey];
//                    suffix = $l.suffixFromNow;
            suffix = [defs stringForKey: kJQTimeAgoStringsSuffixFromNowKey];
//                }
        }
//                distanceMillis = Math.abs(distanceMillis);
        distanceMillis = fabs(distanceMillis);            
//            }
    }
//            
//            var seconds = distanceMillis / 1000;
    const double seconds = distanceMillis / 1000.0;
//            var minutes = seconds / 60;
    const double minutes = seconds / 60.0;
//            var hours = minutes / 60;
    const double hours = minutes / 60.0;
//            var days = hours / 24;
    const double days = hours / 24.0;
//            var years = days / 365;
    const double years = days / 365.0;
//            
//            function substitute(stringOrFunction, number) { ... }
    // Use stringWithFormat, etc.
//            
//            var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
    NSString* words = nil;
    if (seconds < 45)
        words = [[defs stringForKey: kJQTimeAgoStringsSecondsKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)round(seconds)]];
//            seconds < 90 && substitute($l.minute, 1) ||
    else if (seconds < 90)
        words = [[defs stringForKey: kJQTimeAgoStringsMinuteKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)1]];

//            minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
    else if (minutes < 45)
        words = [[defs stringForKey: kJQTimeAgoStringsMinutesKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)round(minutes)]];

//            minutes < 90 && substitute($l.hour, 1) ||
    else if (minutes < 90)
        words = [[defs stringForKey: kJQTimeAgoStringsHourKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)1]];

//            hours < 24 && substitute($l.hours, Math.round(hours)) ||
    else if (hours < 24)
        words = [[defs stringForKey: kJQTimeAgoStringsHoursKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)round(hours)]];

//            hours < 48 && substitute($l.day, 1) ||
    else if (hours < 48)
        words = [[defs stringForKey: kJQTimeAgoStringsDayKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)1]];
//            days < 30 && substitute($l.days, Math.floor(days)) ||
    else if (days < 30)
        words = [[defs stringForKey: kJQTimeAgoStringsDaysKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)floor(days)]];

//            days < 60 && substitute($l.month, 1) ||
    else if (days < 60)
        words = [[defs stringForKey: kJQTimeAgoStringsMonthKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)1]];

//            days < 365 && substitute($l.months, Math.floor(days / 30)) ||
    else if (days < 365)
        words = [[defs stringForKey: kJQTimeAgoStringsMonthsKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)floor(days/30.0)]];

//            years < 2 && substitute($l.year, 1) ||
    else if (years < 2)
        words = [[defs stringForKey: kJQTimeAgoStringsYearKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)1]];
//            substitute($l.years, Math.floor(years));
    else
        words = [[defs stringForKey: kJQTimeAgoStringsYearsKey] stringByReplacingOccurrencesOfString: @"%d" withString: [NSString stringWithFormat: @"%d", (int)floor(years)]];
//            
//            return $.trim([prefix, words, suffix].join(" "));
    NSString* retVal = [[NSString stringWithFormat: @"%@ %@ %@", 
                         (prefix ? prefix : @""),
                         (words ? words : @""),
                         (suffix ? suffix : @"")] stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    return retVal;
//        },
}

// Load settings
+ (void)load
{    
    // Frameworks are guaranteed to be loaded by now so we can use NSDictionary, etc...
    // See here for details: http://www.mikeash.com/pyblog/friday-qa-2009-05-22-objective-c-class-loading-and-initialization.html
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        @autoreleasepool 
        {
            NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [NSNumber numberWithBool: NO], kJQTimeAgoAllowFutureKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsPrefixAgoKey, nil, [NSBundle mainBundle], @" ", @"kJQTimeAgoStringsPrefixAgoKey"), kJQTimeAgoStringsPrefixAgoKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsPrefixFromNowKey, nil, [NSBundle mainBundle], @" ", @"kJQTimeAgoStringsPrefixFromNowKey"), kJQTimeAgoStringsPrefixFromNowKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsSuffixAgoKey, nil, [NSBundle mainBundle], @"ago", @"kJQTimeAgoStringsSuffixAgoKey"), kJQTimeAgoStringsSuffixAgoKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsSuffixFromNowKey, nil, [NSBundle mainBundle], @"from now", @"kJQTimeAgoStringsSuffixFromNowKey"), kJQTimeAgoStringsSuffixFromNowKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsSecondsKey, nil, [NSBundle mainBundle], @"less than a minute", @"kJQTimeAgoStringsSecondsKey"), kJQTimeAgoStringsSecondsKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsMinuteKey, nil, [NSBundle mainBundle], @"about a minute", @"kJQTimeAgoStringsMinuteKey"), kJQTimeAgoStringsMinuteKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsMinutesKey, nil, [NSBundle mainBundle], @"%d minutes", @"kJQTimeAgoStringsMinutesKey"), kJQTimeAgoStringsMinutesKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsHourKey, nil, [NSBundle mainBundle], @"about an hour", @"kJQTimeAgoStringsHourKey"), kJQTimeAgoStringsHourKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsHoursKey, nil, [NSBundle mainBundle], @"about %d hours", @"kJQTimeAgoStringsHoursKey"), kJQTimeAgoStringsHoursKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsDayKey, nil, [NSBundle mainBundle], @"about a day", @"kJQTimeAgoStringsDayKey"), kJQTimeAgoStringsDayKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsDaysKey, nil, [NSBundle mainBundle], @"%d days", @"kJQTimeAgoStringsDaysKey"), kJQTimeAgoStringsDaysKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsMonthKey, nil, [NSBundle mainBundle], @"about a month", @"kJQTimeAgoStringsMonthKey"), kJQTimeAgoStringsMonthKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsMonthsKey, nil, [NSBundle mainBundle], @"%d months", @"kJQTimeAgoStringsMonthsKey"), kJQTimeAgoStringsMonthsKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsYearKey, nil, [NSBundle mainBundle], @"about a year", @"kJQTimeAgoStringsYearKey"), kJQTimeAgoStringsYearKey,
                                      NSLocalizedStringWithDefaultValue(kJQTimeAgoStringsYearsKey, nil, [NSBundle mainBundle], @"%d years", @"kJQTimeAgoStringsYearsKey"), kJQTimeAgoStringsYearsKey,
                                      nil];


            [[NSUserDefaults standardUserDefaults] registerDefaults: settings];
        }
    });
}

@end

NSString* const kJQTimeAgoAllowFutureKey = @"kJQTimeAgoAllowFutureKey";
NSString* const kJQTimeAgoStringsPrefixAgoKey = @"kJQTimeAgoStringsPrefixAgoKey";
NSString* const kJQTimeAgoStringsPrefixFromNowKey = @"kJQTimeAgoStringsPrefixFromNowKey";
NSString* const kJQTimeAgoStringsSuffixAgoKey = @"kJQTimeAgoStringsSuffixAgoKey";
NSString* const kJQTimeAgoStringsSuffixFromNowKey = @"kJQTimeAgoStringsSuffixFromNowKey";
NSString* const kJQTimeAgoStringsSecondsKey = @"kJQTimeAgoStringsSecondsKey";
NSString* const kJQTimeAgoStringsMinuteKey = @"kJQTimeAgoStringsMinuteKey";
NSString* const kJQTimeAgoStringsMinutesKey = @"kJQTimeAgoStringsMinutesKey";
NSString* const kJQTimeAgoStringsHourKey = @"kJQTimeAgoStringsHourKey";
NSString* const kJQTimeAgoStringsHoursKey = @"kJQTimeAgoStringsHoursKey";
NSString* const kJQTimeAgoStringsDayKey = @"kJQTimeAgoStringsDayKey";
NSString* const kJQTimeAgoStringsDaysKey = @"kJQTimeAgoStringsDaysKey";
NSString* const kJQTimeAgoStringsMonthKey = @"kJQTimeAgoStringsMonthKey";
NSString* const kJQTimeAgoStringsMonthsKey = @"kJQTimeAgoStringsMonthsKey";
NSString* const kJQTimeAgoStringsYearKey = @"kJQTimeAgoStringsYearKey";
NSString* const kJQTimeAgoStringsYearsKey = @"kJQTimeAgoStringsYearsKey";
NSString* const kJQTimeAgoStringsNumbersKey = @"kJQTimeAgoStringsNumbersKey";
暮凉 2024-12-17 17:26:40

我编写了一个 NSDate 类别,它以人类可读的格式返回时间增量(例如,“4 周,2 天”)。您可以在此处下载并根据您的需要进行调整:https://github.com/chriscdn/RHTools (寻找 NSDate+timesince)。

I wrote an NSDate category that returns time deltas in a human readable format (e.g., "4 weeks, 2 days"). You can download it here and tweak it to your needs: https://github.com/chriscdn/RHTools (look for NSDate+timesince).

南薇 2024-12-17 17:26:40

我知道这已经很旧了,但这就是我所做的,相当简单......

NSDate *placeDate = [object createdAt];
NSTimeInterval timeSince = [placeDate timeIntervalSinceNow];
NSLog(@"%f", timeSince);

if (timeSince > -60) {
cell.date.text = [NSString stringWithFormat:@"%f seconds ago", -timeSince];
}
else if (timeSince <= -60 && timeSince > -3600){
    cell.date.text = [NSString stringWithFormat:@"%.0f minutes ago", -timeSince/60];
}
else if (timeSince <= -3600 && timeSince > -86400){
    cell.date.text = [NSString stringWithFormat:@"%.0f hours ago", -timeSince/60/60];
}
else if (timeSince <= -86400 && timeSince > -604800){
    cell.date.text = [NSString stringWithFormat:@"%.0f days ago", -timeSince/24/60/60];
}
else if (timeSince <= -604800 && timeSince > -2592000){
    cell.date.text = [NSString stringWithFormat:@"%.0f weeks ago", -timeSince/7/24/60/60];
}
else if (timeSince <= -2592000 && timeSince > -31536000){
    cell.date.text = [NSString stringWithFormat:@"%.0f months ago", -timeSince/30/24/60/60];
}
else {
    cell.date.text = [NSString stringWithFormat:@"%.1f years ago", -timeSince/365/24/60/60];
}

I know this is old but this is what I did which is fairly simple...

NSDate *placeDate = [object createdAt];
NSTimeInterval timeSince = [placeDate timeIntervalSinceNow];
NSLog(@"%f", timeSince);

if (timeSince > -60) {
cell.date.text = [NSString stringWithFormat:@"%f seconds ago", -timeSince];
}
else if (timeSince <= -60 && timeSince > -3600){
    cell.date.text = [NSString stringWithFormat:@"%.0f minutes ago", -timeSince/60];
}
else if (timeSince <= -3600 && timeSince > -86400){
    cell.date.text = [NSString stringWithFormat:@"%.0f hours ago", -timeSince/60/60];
}
else if (timeSince <= -86400 && timeSince > -604800){
    cell.date.text = [NSString stringWithFormat:@"%.0f days ago", -timeSince/24/60/60];
}
else if (timeSince <= -604800 && timeSince > -2592000){
    cell.date.text = [NSString stringWithFormat:@"%.0f weeks ago", -timeSince/7/24/60/60];
}
else if (timeSince <= -2592000 && timeSince > -31536000){
    cell.date.text = [NSString stringWithFormat:@"%.0f months ago", -timeSince/30/24/60/60];
}
else {
    cell.date.text = [NSString stringWithFormat:@"%.1f years ago", -timeSince/365/24/60/60];
}
煮酒 2024-12-17 17:26:40

您可以尝试使用 NSDateFormater 并自行编写。

如果值小于一分钟,则立即写入,否则为分钟、小时、天等。

You can try with NSDateFormater and write it your self.

If value is under one minute then write just now, otherwise minutes, hours, days, etc.

半窗疏影 2024-12-17 17:26:40

https://stackoverflow.com/a/8268903/1915957

↑ 转换 swift 代码。

class func stringForTimeIntervalSinceCreated(dateTime :NSDate, serverTime serverDateTime:NSDate) -> String {
    var MinInterval  :Int = 0
    var HourInterval :Int = 0
    var DayInterval  :Int = 0
    var DayModules   :Int = 0

    let interval = abs(Int(dateTime.timeIntervalSinceDate(serverDateTime)))
    if (interval >= 86400) {
        DayInterval = interval/86400
        DayModules = interval%86400
        if (DayModules != 0) {
            if (DayModules>=3600) {
                //HourInterval=DayModules/3600;
                return String(DayInterval) + " days"
            } else {
                if (DayModules >= 60) {
                    //MinInterval=DayModules/60;
                    return String(DayInterval) + " days"
                } else {
                    return String(DayInterval) + " days"
                }
            }
        } else {
            return String(DayInterval) + " days"
        }
    } else {
        if (interval >= 3600) {
            HourInterval = interval/3600
            return String(HourInterval) + " hours"
        } else if (interval >= 60) {
            MinInterval = interval/60
            return String(MinInterval) + " minutes"
        } else {
            return String(interval) + " Sec"
        }
    }
}

https://stackoverflow.com/a/8268903/1915957

↑ convert swift code.

class func stringForTimeIntervalSinceCreated(dateTime :NSDate, serverTime serverDateTime:NSDate) -> String {
    var MinInterval  :Int = 0
    var HourInterval :Int = 0
    var DayInterval  :Int = 0
    var DayModules   :Int = 0

    let interval = abs(Int(dateTime.timeIntervalSinceDate(serverDateTime)))
    if (interval >= 86400) {
        DayInterval = interval/86400
        DayModules = interval%86400
        if (DayModules != 0) {
            if (DayModules>=3600) {
                //HourInterval=DayModules/3600;
                return String(DayInterval) + " days"
            } else {
                if (DayModules >= 60) {
                    //MinInterval=DayModules/60;
                    return String(DayInterval) + " days"
                } else {
                    return String(DayInterval) + " days"
                }
            }
        } else {
            return String(DayInterval) + " days"
        }
    } else {
        if (interval >= 3600) {
            HourInterval = interval/3600
            return String(HourInterval) + " hours"
        } else if (interval >= 60) {
            MinInterval = interval/60
            return String(MinInterval) + " minutes"
        } else {
            return String(interval) + " Sec"
        }
    }
}
jJeQQOZ5 2024-12-17 17:26:40
+ (NSString *)changeTime:(NSString *)serverDateTime{
    NSInteger MinInterval;
    NSInteger HourInterval;
    NSInteger DayInterval;
    //NSInteger DayModules;
    NSTimeInterval _interval=[serverDateTime doubleValue];
    NSDate *Serverdate = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSInteger interval = (long)((NSInteger)[[NSDate date] timeIntervalSinceDate:Serverdate]);
    if(interval >= 86400)
    {
        DayInterval  = interval/86400;// no. of days
        if (DayInterval > 14){
            return [NSString stringWithFormat:@"w+"];
        }
        if (DayInterval >= 7 && DayInterval <= 14){
            int diff =  (int)DayInterval / 7;
            return [NSString stringWithFormat:@"%iw",diff];
        }
        return [NSString stringWithFormat:@"%id",(int)DayInterval];
    }
    else{

        if(interval>=3600)
        {

            HourInterval= interval/3600;
            return [NSString stringWithFormat:@"%lih", (long)HourInterval];

        }
        else if(interval>=60){

            MinInterval = interval/60;

            return [NSString stringWithFormat:@"%lim", (long)MinInterval];
        }
        else{
            return [NSString stringWithFormat:@"%lis", (long)interval];
        }
        return @"now";
    }

}
+ (NSString *)changeTime:(NSString *)serverDateTime{
    NSInteger MinInterval;
    NSInteger HourInterval;
    NSInteger DayInterval;
    //NSInteger DayModules;
    NSTimeInterval _interval=[serverDateTime doubleValue];
    NSDate *Serverdate = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSInteger interval = (long)((NSInteger)[[NSDate date] timeIntervalSinceDate:Serverdate]);
    if(interval >= 86400)
    {
        DayInterval  = interval/86400;// no. of days
        if (DayInterval > 14){
            return [NSString stringWithFormat:@"w+"];
        }
        if (DayInterval >= 7 && DayInterval <= 14){
            int diff =  (int)DayInterval / 7;
            return [NSString stringWithFormat:@"%iw",diff];
        }
        return [NSString stringWithFormat:@"%id",(int)DayInterval];
    }
    else{

        if(interval>=3600)
        {

            HourInterval= interval/3600;
            return [NSString stringWithFormat:@"%lih", (long)HourInterval];

        }
        else if(interval>=60){

            MinInterval = interval/60;

            return [NSString stringWithFormat:@"%lim", (long)MinInterval];
        }
        else{
            return [NSString stringWithFormat:@"%lis", (long)interval];
        }
        return @"now";
    }

}
ゝ杯具 2024-12-17 17:26:40

如果您对日期和时间有疑问。
你应该想到这个库

https://github.com/MatthewYork/DateTools

它总是有帮助!

if you have a problem with date and time.
you should think to this library

https://github.com/MatthewYork/DateTools

It always help!

温柔一刀 2024-12-17 17:26:40

您可以使用NSDate-TimeAgo。可以使用 CocoaPods 将其安装在您的项目中。

  1. 将文件添加到您的项目中 - 手动或通过 Cocoapods (pod 'NSDate+TimeAgo')
  2. 使用 #import "NSDate+TimeAgo.h" 导入标头
  3. 使用库方法 timeAgo。

You can use NSDate-TimeAgo. It can be installed in your project using CocoaPods.

  1. Add the files to your project - manually or via Cocoapods (pod 'NSDate+TimeAgo')
  2. Import the header using #import "NSDate+TimeAgo.h"
  3. Use the library method timeAgo.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文