计算本周的第一天和最后一天

发布于 2024-08-18 08:07:24 字数 389 浏览 13 评论 0 原文

参考预先回答的问题:获取本周的第一天和最后一天

上面的链接中有两个答案。其中一个是理论上的,另一个是名为 PyObjC(Python-Objective C 桥接语言)的语言,快速的 google 搜索确认 PyObjC 不适用于 iPhone SDK。

那么关于这个问题,如何才能将 PyObjC 代码转换为与 iPhone SDK 兼容。

目标:假设今天(周二)19号,周日。是 17 日(本周开始)和周六。 23日是周末。我想要得到像 19/01 - 23/01 这样的字符串 [即周开始(连字符)周结束]

Reference to pre-answered question at: Getting first and last days of current week

There are two answers in the above link. One of them is theoretical and the other is in a language called as PyObjC (Python-Objective C bridge language), and a quick google search confirms that PyObjC does not work with iPhone SDK.

So regarding the question, how is it possible to get the PyObjC code translated to be compatible with iPhone SDK.

Target: Supposing today (Tue.) is 19th, and Sun. was 17th (start of week) and Sat. 23rd is end of week. I want to get a string like 19/01 - 23/01 [i.e. The start of week (hypen) end of week]

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

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

发布评论

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

评论(4

笑脸一如从前 2024-08-25 08:07:24

如果您有 NSDate,则可以使用当前的 NSCalendar 来检索该日期的 NSDateComponents。将NSDateComponentsweekday设置为1(一周的第一天),创建一个副本,并将副本的weekday设置为7(一周的最后一天)。然后使用 NSCalendar 将两个 NSDateComponents 转换回各自的 NSDate 对象,之后您可以使用 NSDateFormatter > 创建您的字符串表示形式。

此链接有一个有关如何获取日期的工作日的示例:https://stackoverflow.com/questions/1057349#1057405

If you have an NSDate, you can use the current NSCalendar to retrieve that date's NSDateComponents. Set the NSDateComponents's weekday to 1 (the first day of the week), create a copy, and set the copy's weekday to 7 (the last day of the week). Then use the NSCalendar to convert both the NSDateComponents back to their respective NSDate objects, after which you can use an NSDateFormatter to create your string representation.

This link has an example about how to get a date's weekday: https://stackoverflow.com/questions/1057349#1057405

守护在此方 2024-08-25 08:07:24

这是一些代码,它还检查一周的开始于上个月开始的边缘情况:

// Finds the date for the first day of the week
- (NSDate *)getFirstDayOfTheWeekFromDate:(NSDate *)givenDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    // Edge case where beginning of week starts in the prior month
    NSDateComponents *edgeCase = [[NSDateComponents alloc] init];
    [edgeCase setMonth:2];
    [edgeCase setDay:1];
    [edgeCase setYear:2013];
    NSDate *edgeCaseDate = [calendar dateFromComponents:edgeCase];

    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:edgeCaseDate];
    [components setWeekday:1]; // 1 == Sunday, 7 == Saturday
    [components setWeek:[components week]];

    NSLog(@"Edge case date is %@ and beginning of that week is %@", edgeCaseDate , [calendar dateFromComponents:components]);

    // Find Sunday for the given date
    components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
    [components setWeekday:1]; // 1 == Sunday, 7 == Saturday
    [components setWeek:[components week]];

    NSLog(@"Original date is %@ and beginning of week is %@", givenDate , [calendar dateFromComponents:components]);

    return [calendar dateFromComponents:components];
}

编辑:上面的相关代码

- (NSDate *)firstDayOfWeekFrom:(NSDate *)givenDate {
  NSCalendar *calendar = [NSCalendar currentCalendar];

  NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
  [components setWeekday:1]; // 1 == Sunday, 7 == Saturday
  [components setWeekOfYear:[components weekOfYear]];

  return [calendar dateFromComponents:components];
}

Here's some code and it also checks an edge case where the beginning of the week starts in the prior month:

// Finds the date for the first day of the week
- (NSDate *)getFirstDayOfTheWeekFromDate:(NSDate *)givenDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    // Edge case where beginning of week starts in the prior month
    NSDateComponents *edgeCase = [[NSDateComponents alloc] init];
    [edgeCase setMonth:2];
    [edgeCase setDay:1];
    [edgeCase setYear:2013];
    NSDate *edgeCaseDate = [calendar dateFromComponents:edgeCase];

    NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:edgeCaseDate];
    [components setWeekday:1]; // 1 == Sunday, 7 == Saturday
    [components setWeek:[components week]];

    NSLog(@"Edge case date is %@ and beginning of that week is %@", edgeCaseDate , [calendar dateFromComponents:components]);

    // Find Sunday for the given date
    components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
    [components setWeekday:1]; // 1 == Sunday, 7 == Saturday
    [components setWeek:[components week]];

    NSLog(@"Original date is %@ and beginning of week is %@", givenDate , [calendar dateFromComponents:components]);

    return [calendar dateFromComponents:components];
}

Edit: Relevant code from above

- (NSDate *)firstDayOfWeekFrom:(NSDate *)givenDate {
  NSCalendar *calendar = [NSCalendar currentCalendar];

  NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
  [components setWeekday:1]; // 1 == Sunday, 7 == Saturday
  [components setWeekOfYear:[components weekOfYear]];

  return [calendar dateFromComponents:components];
}
黑白记忆 2024-08-25 08:07:24

这是我几天前一直在寻找的东西,终于找到了。 NSDate 中有一个方法 rangeOfUnit:startDate:interval:forDate: ,并以简单的方式执行此操作。您可以在以下位置查看详细信息:
当前周开始和结束日期

This is what I was looking for days ago and finally found. There is a method rangeOfUnit:startDate:interval:forDate: in NSDate and do that in a simple way. You can see detail at:
Current Week Start and End Date

一瞬间的火花 2024-08-25 08:07:24
NSDate *weekDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *currentComps = [myCalendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:weekDate];
int ff = currentComps.weekOfYear;
NSLog(@"1  %d", ff);

[currentComps setWeekday:1]; // 1: sunday
NSDate *firstDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:7]; // 7: saturday
NSDate *lastDayOfTheWeek = [myCalendar dateFromComponents:currentComps];

NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
myDateFormatter.dateFormat = @"dd EEEE";
NSString *firstStr = [myDateFormatter stringFromDate:firstDayOfTheWeek];
NSString *secondStr = [myDateFormatter stringFromDate:lastDayOfTheWeek];

NSLog(@"first - %@ \nlast - %@", firstStr, secondStr);
NSDate *weekDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *currentComps = [myCalendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekOfYearCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:weekDate];
int ff = currentComps.weekOfYear;
NSLog(@"1  %d", ff);

[currentComps setWeekday:1]; // 1: sunday
NSDate *firstDayOfTheWeek = [myCalendar dateFromComponents:currentComps];
[currentComps setWeekday:7]; // 7: saturday
NSDate *lastDayOfTheWeek = [myCalendar dateFromComponents:currentComps];

NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
myDateFormatter.dateFormat = @"dd EEEE";
NSString *firstStr = [myDateFormatter stringFromDate:firstDayOfTheWeek];
NSString *secondStr = [myDateFormatter stringFromDate:lastDayOfTheWeek];

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