使用 NSDate 获取日期

发布于 2024-09-15 01:41:49 字数 666 浏览 7 评论 0原文

我尝试了很多方法,但始终失败,希望你能帮助我实现我想做的事。

我正在制作一个 iPad 应用程序,我将在一个视图中有五个表格,每个表格都有一个日期作为这种格式的标题/标题,例如星期一 20

这五个表格将是星期一到星期五。这是我做不到的一点。我想计算出当前日期,然后突出显示今天的表格,显然每天都在变化。

举例来说,假设今天是 9 号星期四。星期四表格会突出显示,然后自动设置星期四左右其他表格的日期。

想想学校的时间表/计划表/日记。周一到周五,每个都标有日期。

编辑:那如果我这样做怎么办?如果我将其添加到您给我的代码中,如果为 TRUE(按下按钮)则添加 7 天,就像 Apple 的示例一样。然而我的问题是,什么是公历?我用什么来代替它?我看到它在苹果的日历样本中大量使用。

if (tableView == monTable){
        if(next == TRUE){
            [comps setDay:7];
            NSDate *date = [gregorian dateByAddingComponents:comps toDate:curDate  options:0];
        }
        else{
            [comps setWeekday:2];
        }
    }

I have attempted this is many ways but failed consistently, hopefully you can help me achieve the what I want to do.

I am making an iPad app, I will have five tables in a single view and each table will have a date as a header/title in this format, e.g. Monday 20

These five tables will be Monday to Friday. This is the bit I can't do. I want to work out the current date and then highlight the table which is today, obviously changes everyday.

So for example, lets say today is Thursday 9th. Thursday table is highlighted and then is automatically sets the date of the other tables around Thursday.

Think of a school timetable/planner/diary. Monday to Friday, each labelled with their dates.

EDIT: So what if I did it like this? If I add this into the code you gave me, if TRUE (button pressed) add seven days, done just like in the example form Apple. However my issue, what is gregorian? what do I replace it with? I have seen it used lots on calendar samples from Apple.

if (tableView == monTable){
        if(next == TRUE){
            [comps setDay:7];
            NSDate *date = [gregorian dateByAddingComponents:comps toDate:curDate  options:0];
        }
        else{
            [comps setWeekday:2];
        }
    }

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

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

发布评论

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

评论(2

翻了热茶 2024-09-22 01:41:49

您可以通过以下方式获取周一到周五的日期:

NSDate* curDate = [NSDate date]; // Get current date
NSCalendar* calendar = [NSCalendar currentCalendar];// Init calendar
NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components

    // Week days change from 1 to 7, Sunday is the 1st, Saturday - the last one.
for (int i = 2; i <= 6; i++){ 
    [comps setWeekday:i];
    NSDate *tDate = [calendar dateFromComponents:comps];
    NSLog(@"%@", tDate);
}

要确定要突出显示的日期(当前日期),您只需检查日期的工作日部分。

编辑: titleForHeaderInSection 方法可能如下所示:

- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section{
    NSDate* curDate = [NSDate date]; // Get current date
    NSCalendar* calendar = [NSCalendar currentCalendar];// Init calendar
    NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components

    // Week days change from 1 to 7, Sunday is the 1st, Saturday - the last one.
    if (tableView == monTable)
        [comps setWeekday:2];
    if (tableView == tueTable)
        [comps setWeekday:3];
    if (tableView == wedTable)
        [comps setWeekday:4];
    if (tableView == thuTable)
        [comps setWeekday:5];
    if (tableView == friTable)
        [comps setWeekday:6];

    NSDate *tDate = [calendar dateFromComponents:comps];
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"EEE, MMM d"];


    return [formatter stringFromDate:tDate];
}

You can get Monday to Friday dates this way:

NSDate* curDate = [NSDate date]; // Get current date
NSCalendar* calendar = [NSCalendar currentCalendar];// Init calendar
NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components

    // Week days change from 1 to 7, Sunday is the 1st, Saturday - the last one.
for (int i = 2; i <= 6; i++){ 
    [comps setWeekday:i];
    NSDate *tDate = [calendar dateFromComponents:comps];
    NSLog(@"%@", tDate);
}

To determine which date to highlight (current date) you just need to check date's weekday component.

Edit: titleForHeaderInSection method may look like:

- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(int)section{
    NSDate* curDate = [NSDate date]; // Get current date
    NSCalendar* calendar = [NSCalendar currentCalendar];// Init calendar
    NSDateComponents* comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate]; // Get necessary date components

    // Week days change from 1 to 7, Sunday is the 1st, Saturday - the last one.
    if (tableView == monTable)
        [comps setWeekday:2];
    if (tableView == tueTable)
        [comps setWeekday:3];
    if (tableView == wedTable)
        [comps setWeekday:4];
    if (tableView == thuTable)
        [comps setWeekday:5];
    if (tableView == friTable)
        [comps setWeekday:6];

    NSDate *tDate = [calendar dateFromComponents:comps];
    NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
    [formatter setDateFormat:@"EEE, MMM d"];


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