标签: nscalendar

Swift DateComponents weekOfMonth 返回错误值

我正在开发一个显示当前月份的日历视图(Cocoa macOS 应用程序)。当我尝试计算给定日期的周数时,我根据日历和区域设置得到不同的结果。

2021 年 1 月 1 日,一些调用返回week 0,其他调用返回week 1

    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy-MM-dd"

    let jan2021 = formatter.date(from: "2021-01-01")!

    var deCalendar = Calendar(identifier: .gregorian)
    deCalendar.locale = Locale(identifier: "de_de")
    var usCalendar = Calendar(identifier: .gregorian)
    usCalendar.locale = Locale(identifier: "en_us")
    var deIsoCalendar = Calendar(identifier: .iso8601)
    deIsoCalendar.locale = Locale(identifier: "de_de")
    var usIso = Calendar(identifier: .iso8601)
    usIso.locale = Locale(identifier: "en_us")

    deCalendar.component(.weekOfMonth, from: jan2021)    // 0 
    deIsoCalendar.component(.weekOfMonth, from: jan2021) // 0
    usCalendar.component(.weekOfMonth, from: jan2021)    // 1 ??
    usIso.component(.weekOfMonth, …
Run Code Online (Sandbox Code Playgroud)

cocoa nscalendar nsdatecomponents swift

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

创建1月1日和2日的NSDate时的奇怪行为

我正在创建这样的日期:

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

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYY"];
NSInteger year = [[dateFormatter stringFromDate:[NSDate date]] intValue];
NSLog(@"NSInteger YEAR : %d\n", year);

//minutes, hours, day and month are initialized elsewhere...

NSDateComponents *comps = [[NSDateComponents alloc] init];      
[comps setYear:year];
[comps setMonth:month];
[comps setDay:day];
[comps setHour:hours];
[comps setMinute:minutes];
NSDate *sampleDate = [gregorian dateFromComponents:comps];

NSLog(@"sampleDate YEAR : %d\n", [[dateFormatter stringFromDate:sampleDate] intValue]);
Run Code Online (Sandbox Code Playgroud)

我的问题是,当日期是1月1日或1月2日,这一年是不正确的..这应该是2010年(因为我今天检索),但实际上是2009年......我不明白为什么!这只发生在那两天......

你知道为什么会这样吗?先感谢您.

iphone objective-c nsdate nscalendar nsdatecomponents

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

iPhone SDK,如何获得周五20:00的NSDate对象?

有谁知道如何获得即将到来的星期五20:00的NSDate?

iphone cocoa nsdate nscalendar ios

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

我应该保留NSCalendar对象以供持久使用吗?

在类似日历的应用程序或应用程序的一部分中,我需要在许多例程中使用(格里高利)日历,这意味着我需要多次使用alloc-init并释放相同的NSCalendar对象.我应该@property(nonatomic,retain)在我的长持久控制器实例中保留一个NSCalendar(比如格里高利历)作为ivar()吗?

或者它的实例方法是重入/线程安全的?

cocoa objective-c foundation nscalendar ios

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

NSTimeZone与GMT的区别

我需要获取用户时区与GMT伦敦时间之间的差异信息,以小时或分钟为单位.

有人可以帮忙吗?

我一直在四处寻找,我有点困惑.

谢谢,

iphone nstimezone nscalendar

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

如何在本月下周的第一天获得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
查看次数

可可在一周内获得第一天

如何获得一周的第一天约会

这似乎更容易,因为:

  • 当周从星期日开始,我需要回到星期日
  • 如果从星期一开始,我需要获得星期一的约会

输入日期是一周中的任何日期与时间...我尝试了几种方法,但边缘情况使它难以实现

我做了一个函数,然而它不能100%工作(不确定[components setDay:-weekday + 2];)

- (NSDate *)firstDateOfWeek {

    NSCalendar * calendar = [NSCalendar currentCalendar];

    NSDateComponents *weekdayComponents = [calendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:self];
    // because sunday is 0, we need it to be 6
    NSInteger weekday = (([weekdayComponents weekday] + 6) % 7);

    NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:self];
    [components setDay: -weekday + 2];

    return [calendar dateFromComponents:components];

}
Run Code Online (Sandbox Code Playgroud)

nsdate nscalendar ios

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

NSCalendar月份范围

这段代码

NSDateComponents *component = [[NSDateComponents alloc] init];
NSCalendar       *gregorian = [[NSCalendar alloc]   initWithCalendarIdentifier:NSGregorianCalendar];
[component setMonth:1];
[component setYear:2013];
[component setDay:1];
currentMonth = [gregorian dateFromComponents:component];
NSLog(@"Date in MonthView %@ ",currentMonth);
Run Code Online (Sandbox Code Playgroud)

给我以下日期

Date in MonthView 2012-12-31 23:00:00 +0000
Run Code Online (Sandbox Code Playgroud)

为什么?月份范围应该从0到11?

nscalendar ios

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

已弃用.WeekdayCalendarUnit的替换

几个星期前我更新了Xcode并开始在这条线上发出警告:

let weekdayComponent = currentCalendar?.components(.WeekdayCalendarUnit, fromDate: date)
Run Code Online (Sandbox Code Playgroud)

'WeekdayCalendarUnit'在iOS 8.0版中已弃用:请改用NSCalendarUnitWeekday

但是当我替换.WeekdayCalendarUnit.NSCalendarUnitWeekday,我得到一个"找不到会员.NSCalendarUnitWeekday"的错误.我尝试了其他一些品种,但没有运气.

我们应该使用什么呢?

xcode nscalendar ios swift

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

Swift根据时间戳显示时间或日期

我有一个返回数据的API,包括该记录的时间戳.在swift中我加载了timestamp元素并将其转换为double,然后可以将其转换为时间.如果记录的日期是今天,我希望能够返回时间,如果记录不是今天,我希望返回日期.见下文:

        let unixTimeString:Double = Double(rowData["Timestamp"] as! String)!
        let date = NSDate(timeIntervalSince1970: unixTimeString) // This is on EST time and has not yet been localised.
        var dateFormatter = NSDateFormatter()
        dateFormatter.timeStyle = .ShortStyle
        dateFormatter.doesRelativeDateFormatting = true
        // If the date is today then just display the time, if the date is not today display the date and change the text color to grey.
        var stringTimestampResponse = dateFormatter.stringFromDate(date)
        cell.timestampLabel.text = String(stringTimestampResponse)
Run Code Online (Sandbox Code Playgroud)

我是否使用NSCalendar查看"日期"是否为今天然后做某事?那么你如何本地化时间以使其对用户而不是服务器时间更正?

nsdate nsdateformatter nscalendar swift

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