如何在iPhone目标c中显示昨天(相对于日期,但不是24小时的时间间隔)访问过的项目

发布于 2024-12-26 10:25:33 字数 1093 浏览 3 评论 0原文

我有一个带有联系人的表格视图。 因为当我选择联系人时,必须将该联系人添加到另一个表:RecentsTable 中。作为最近访问过的项目。为此,我使用了这样的代码,它可以工作,但是。 就昨天而言,它考虑的是 24 小时。但我想在日期更改时显示昨天的项目,然后自动将其添加到昨天的列表中。请指导我。

     NSDate *date = self.lastviewed;
                double time = [date timeIntervalSinceNow];

                NSString *tmpValue = nil;
                time *= -1;


                if(time < 3600*24) {
                    tmpValue = @"Today";
                }

                else {
                    int div = round(time / 60 / 60 / 24);

             // I want solution wth respect to yesterday's date date but not as follows
                     if(div == 1)
                          tmpvalue = @ "Yester Day";


                if (div >1 && div <7)
                        //tmpValue= [NSString stringWithFormat:@"%d days ago", div];
                        tmpValue = @"This Week";

                    else if(div <30)
                        tmpValue = @"This Month";
                    else 
                        tmpValue = @"Earlier"; 
                } 

提前致谢..

I have a table view with Contacts.
in that when i select the contact that contact has to be added in to another Table:RecentsTable. as recently visited items. for this purposs i have used code like this, it's working but.
in case of yesterday it cosidered 24 hours. but iwant to display yesterday items when ever date changed then automatically it was added in to yesterday's list. please guide me.

     NSDate *date = self.lastviewed;
                double time = [date timeIntervalSinceNow];

                NSString *tmpValue = nil;
                time *= -1;


                if(time < 3600*24) {
                    tmpValue = @"Today";
                }

                else {
                    int div = round(time / 60 / 60 / 24);

             // I want solution wth respect to yesterday's date date but not as follows
                     if(div == 1)
                          tmpvalue = @ "Yester Day";


                if (div >1 && div <7)
                        //tmpValue= [NSString stringWithFormat:@"%d days ago", div];
                        tmpValue = @"This Week";

                    else if(div <30)
                        tmpValue = @"This Month";
                    else 
                        tmpValue = @"Earlier"; 
                } 

thanks in advance..

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

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

发布评论

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

评论(3

蓝戈者 2025-01-02 10:25:33

您应该使用 NSDateComponents< /a> 处理日期时。它将处理夏令时等问题。NSDate 上的 StackOverflow 上有很多问题,这个 例如,可以通过将日期部分更改为 -1 天来解决您的问题。

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *today = [NSDate date];

NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;
NSDate *yesterday = [gregorian dateByAddingComponents:dayComponent 
                                               toDate:today 
                                              options:0];

You should use NSDateComponents when working with dates. It will handle things like day light savings etc. There are plenty of questions on StackOverflow on NSDate, this one for example can be used to solve your problem by changing the date component to -1 day.

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *today = [NSDate date];

NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = -1;
NSDate *yesterday = [gregorian dateByAddingComponents:dayComponent 
                                               toDate:today 
                                              options:0];
月下凄凉 2025-01-02 10:25:33

我认为你不仅要改变“昨天”的情况,还要改变月、年和周的其他计算。

NSDate *date = self.lastviewed;
// double time = [date timeIntervalSinceNow];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents * componentsOfDate = [currentCalendar 
                                        components: (NSYearCalendarUnit|NSMonthCalendarUnit|
                                                   NSDayCalendarUnit|NSWeekCalendarUnit)
                                       fromDate:date];
NSDateComponents * componentsOfToday = [currentCalendar 
                                         components:(NSYearCalendarUnit|NSMonthCalendarUnit|
                                         NSDayCalendarUnit|NSWeekCalendarUnit) 
                                       fromDate:[NSDate date]];

if ([componensOfDate year] == [componentsOfToday year])
    if ([componentsOfDate month] == [componentsOfToday month])
        if([componentsOfDate day] == [componentsOfToday day])
            tmpvalue = @"Today" ;
         else
             tmpvalue = @"This Month" ;
    else
        tmpvalue = @"This Year" ;

// This week calculation, the easy way.
if([componentsOfDate week] == [componentsOfToday week] && 
   abs([date timeIntervalSinceNow]) <= (7*24*60*60))
    tmpvalue = @"This Week" ;

// Yesterday calculation
NSDate * yesterday = [NSDate dateWithTimeInterval:(-24*7*60*60) sinceDate:[NSDate date]];
NSComponents *componentsOfYesterday = [currentCalendar 
                                        components: (NSYearCalendarUnit|NSMonthCalendarUnit|
                                                   NSDayCalendarUnit)
                                       fromDate:yesterday];

// check this, as I'm not sure you can use compare: on components object
if([componentsOfDate compare:componentsOfYesterday] == NSOrderedSame)
  tmpvalue = @"Yesterday";

I think you have to change not only the "Yesterday" case but also the other calculations for month, year and week.

NSDate *date = self.lastviewed;
// double time = [date timeIntervalSinceNow];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents * componentsOfDate = [currentCalendar 
                                        components: (NSYearCalendarUnit|NSMonthCalendarUnit|
                                                   NSDayCalendarUnit|NSWeekCalendarUnit)
                                       fromDate:date];
NSDateComponents * componentsOfToday = [currentCalendar 
                                         components:(NSYearCalendarUnit|NSMonthCalendarUnit|
                                         NSDayCalendarUnit|NSWeekCalendarUnit) 
                                       fromDate:[NSDate date]];

if ([componensOfDate year] == [componentsOfToday year])
    if ([componentsOfDate month] == [componentsOfToday month])
        if([componentsOfDate day] == [componentsOfToday day])
            tmpvalue = @"Today" ;
         else
             tmpvalue = @"This Month" ;
    else
        tmpvalue = @"This Year" ;

// This week calculation, the easy way.
if([componentsOfDate week] == [componentsOfToday week] && 
   abs([date timeIntervalSinceNow]) <= (7*24*60*60))
    tmpvalue = @"This Week" ;

// Yesterday calculation
NSDate * yesterday = [NSDate dateWithTimeInterval:(-24*7*60*60) sinceDate:[NSDate date]];
NSComponents *componentsOfYesterday = [currentCalendar 
                                        components: (NSYearCalendarUnit|NSMonthCalendarUnit|
                                                   NSDayCalendarUnit)
                                       fromDate:yesterday];

// check this, as I'm not sure you can use compare: on components object
if([componentsOfDate compare:componentsOfYesterday] == NSOrderedSame)
  tmpvalue = @"Yesterday";
江心雾 2025-01-02 10:25:33

如果您使用 NSCalendar 获取日和月单位。

请参阅这个一些例子。

如果您获得当天和给定的最后查看日期的日、月单位,您可以通过比较两者找到所需的值。

编辑:

NSDate *todate = [NSDate date];
NSDate *lastViewedDate = self.lastviewed; 

NSCalendar *gregorian = [[NSCalendar alloc] 
                         initWithCalendarIdentifier:NSGregorianCalendar]; 

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit; 

NSDateComponents *components = [gregorian components:unitFlags 
                                            fromDate:lastViewedDate 
                                              toDate:todate options:0]; 
NSInteger months = [components month]; 
NSInteger days = [components day]; 

NSString *tmpValue = nil;

if ( days == 0 ) {
    tmpValue = @"Today";
}
else
{
    if(days == 1)
        tmpValue = @"Yesterday";

    else if (days >1 && days <7)
        //tmpValue= [NSString stringWithFormat:@"%d days ago", div];
        tmpValue = @"This Week";

    else if(days <30)
        tmpValue = @"This Month";
    else 
        tmpValue = @"Earlier";
}

It would be more easy if you use NSCalendar to get the day and month units.

Refer this and this for some examples.

If you get the day, month units for current day and the given lastViewed day, you can find the required values by comparing the both.

Edit:

NSDate *todate = [NSDate date];
NSDate *lastViewedDate = self.lastviewed; 

NSCalendar *gregorian = [[NSCalendar alloc] 
                         initWithCalendarIdentifier:NSGregorianCalendar]; 

NSUInteger unitFlags = NSMonthCalendarUnit | NSDayCalendarUnit; 

NSDateComponents *components = [gregorian components:unitFlags 
                                            fromDate:lastViewedDate 
                                              toDate:todate options:0]; 
NSInteger months = [components month]; 
NSInteger days = [components day]; 

NSString *tmpValue = nil;

if ( days == 0 ) {
    tmpValue = @"Today";
}
else
{
    if(days == 1)
        tmpValue = @"Yesterday";

    else if (days >1 && days <7)
        //tmpValue= [NSString stringWithFormat:@"%d days ago", div];
        tmpValue = @"This Week";

    else if(days <30)
        tmpValue = @"This Month";
    else 
        tmpValue = @"Earlier";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文