相关疑难解决方法(0)

109
推荐指数
5
解决办法
8万
查看次数

如何将NSDate转换为相对格式为"今天","昨天","一周前","一个月前","一年前"?

我想将nsdate转换为相对格式"Today","Yesterday","a week ago","a month ago","a year ago","date as it is".

我已经为它写了以下方法..但有些如何只是打印,因为它是日期..你能告诉我应该是什么问题吗?

//以下是我的函数,它将日期转换为相对字符串

+(NSString *)getDateDiffrence:(NSDate *)strDate{
    NSDateFormatter *df = [[NSDateFormatter alloc] init];

    df.timeStyle = NSDateFormatterMediumStyle;
    df.dateStyle = NSDateFormatterShortStyle;
    df.doesRelativeDateFormatting = YES;
    NSLog(@"STRING DATEEE : %@ REAL DATE TODAY %@",[df stringFromDate:strDate],[NSDate date]);
      return [df stringFromDate:strDate];

}
Run Code Online (Sandbox Code Playgroud)

我有以下格式的日期字符串 "2013-10-29T09:38:00"

当我试图给出NSDate对象时,它总是返回我的null日期.
所以我试图将该日期转换为yyyy-MM-dd HH:mm:ssZZZZ然后我将此日期传递给函数然后它只是打印整个日期..

如何解决这个问题呢?

//以下是我调用上述函数的代码

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date = [formatter dateFromString:[threadDict objectForKey:@"lastMessageDate"]];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ssZZZZ"];

NSString *date1 = [formatter stringFromDate:date];
NSDate *date_d = …
Run Code Online (Sandbox Code Playgroud)

objective-c nsdate relative-date nsdateformatter ios

39
推荐指数
5
解决办法
3万
查看次数

如何计算当前第一周的NSDate?

即:

  NSDate *firstDayOfWeek = [[NSDate date] firstDayOfWeek];
Run Code Online (Sandbox Code Playgroud)

例如,今天是8月19日,我想为一个NSDate2010-08-15 12:00am从上面的代码行.谢谢!

iphone cocoa-touch nsdate

14
推荐指数
3
解决办法
9972
查看次数

NSCalendar每周的第一天和个人设置

在"邮件,通讯录,日历"部分的系统设置中,用户可以将任何工作日设置为一周的第一天.[[NSCalendar currentCalendar] firstWeekday]不返回此值但返回当前区域设置的值.Apple的日历应用程序响应用户首选项的变化.

如何获得用户个性化的第一天?

nscalendar ios

8
推荐指数
0
解决办法
113
查看次数

NSCalendar,为什么设置第一个周末不影响计算结果?

我需要计算给定日期的工作日,但是,根据日历,一周可以在星期一加星,在星期日看星期日

所以我想设置它,从周一开始,使用

[[NSCalendar currentCalendar] setFirstWeekday:2];
Run Code Online (Sandbox Code Playgroud)

但是,计算结果是一样的

{
    [[NSCalendar currentCalendar] setFirstWeekday:1];
    NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    NSInteger weekday = [weekdayComponents weekday] - 1;
    NSLog(@"%d", weekday);
}
{
    [[NSCalendar currentCalendar] setFirstWeekday:2];
    NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    NSInteger weekday = [weekdayComponents weekday] - 1;
    NSLog(@"%d", weekday);
}
Run Code Online (Sandbox Code Playgroud)

返回相同的数字,但为什么?

cocoa nsdate nscalendar ios

6
推荐指数
2
解决办法
6799
查看次数

一周的开始日存储在NSLocale的哪个地方?

我住在荷兰.我们荷兰人喜欢将周一视为本周的开始.我知道美国人喜欢将星期日视为本周的开始.傻美国人;)

如果我想向全球用户提供周视图,我可以从NSLocale获得一周中首选的开始日期,还是设置面板是唯一可行的方法?

干杯,EP.

objective-c nsdate nscalendar nsdatecomponents eventkit

5
推荐指数
1
解决办法
1871
查看次数

假设格里高利历,为什么将86,400(一天)的时间间隔添加到一个永不答案的日期?

我创建了一种方法来计算一周的第一天和最后一天,方法是将一天的时间间隔(86,400)添加到日期,再乘以所需的天数.

一位同事评论说" 增加86,400的倍数绝不是答案 ".为什么会这样?

这是我创建的类别方法,我该如何改进?

- (NSDate*)firstDayOfWeek
{
    return [self dateWithDaysAddedGivenDayOfWeek:@{
        @1 : @-6,
        @2 : @0,
        @3 : @-1,
        @4 : @-2,
        @5 : @-3,
        @6 : @-4,
        @7 : @-5
    }];
}

- (NSDate*)lastDayOfWeek
{
    return [self dateWithDaysAddedGivenDayOfWeek:@{
        @1 : @0,
        @2 : @6,
        @3 : @5,
        @4 : @4,
        @5 : @3,
        @6 : @2,
        @7 : @1
    }];
}

- (NSDate*)dateWithDaysAddedGivenDayOfWeek:(NSDictionary*)daysToAddGivenDay
{
    NSDateComponents* components = [[NSCalendar currentCalendar]
        components:NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit fromDate:self];

    NSInteger …
Run Code Online (Sandbox Code Playgroud)

cocoa objective-c

4
推荐指数
1
解决办法
392
查看次数

1
推荐指数
1
解决办法
6125
查看次数

如何在本月下周的第一天获得NSDate

给定一个任意日期,我需要找到该月下一周第一天的日期.请注意,它并不像在当前日期添加7天那么简单,因为该月的最后一周可能少于7天.这是我现在使用的代码:

    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSWeekOfMonthCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSDayCalendarUnit fromDate:currentDate];

    NSLog(@"week of month: %ld", [components weekOfMonth]);

    [components setWeekOfMonth:[components weekOfMonth] + 1];
    NSLog(@"new week of month: %ld", [components weekOfMonth]); //week of month is now 2

    [components setWeekday:1];
    NSDate *nextWeek = [calendar dateFromComponents:components];
Run Code Online (Sandbox Code Playgroud)

例如,currentDate设置为2012-10-01.在这个例子中,nextWeek始终是2012-10-01.似乎发送setWeekOfMonth:不会增加NSDateComponents对象中的其他日期组件.我是否配置了错误的日期组件,或者setWeekOfMonth:不应该这样工作?

cocoa nsdate nscalendar nsdatecomponents

1
推荐指数
1
解决办法
2274
查看次数

从iOS sdk中获取日期

在我的应用程序,希望得到日(即Sunday,Monday等)之日起.

我的代码如下:

NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];

    //ask for current week
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:currentDate];
    //create date on week start
    NSDate* weekstart=[calendar dateFromComponents:comps];

    //add 7 days
    NSMutableArray* week=[NSMutableArray arrayWithCapacity:7];
    for (int i=1; i<=7; i++) {
        NSDateComponents *compsToAdd = [[NSDateComponents alloc] init];
        compsToAdd.day=i;
        NSDate *nextDate = [calendar dateByAddingComponents:compsToAdd toDate:weekstart options:0];
        NSLog(@"%@",nextDate);  // date is 2013-06-30 18:30:00 +0000



        NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
        [myFormatter setDateFormat:@"EEEE"]; // day, …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nsdateformatter ios

1
推荐指数
3
解决办法
1万
查看次数