标签: nscalendar

如何使用Cocoa查找任何给定日期的星期几

我想弄清楚任何特定日期(即2009年6月27日)的哪一天(即周一,周五......)

谢谢.

cocoa objective-c nsdate nscalendar nsdatecomponents

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

自定义报警的UILocalNotification重复间隔(太阳,周一,周二,周三,星期四,星期五,坐着)

UILocalNotification用于报警目的.我有一个基于工作日重复的自定义选项(sun,mon,tue,wed,thu,fri,sat).许多应用程序都执行了此过程.我最好的尝试了我的水平.但我无法让它发挥作用.请各位帮帮我....

iphone nscalendar ipad ios uilocalnotification

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

如何在[NSDate日期]当天00:00获得NSDate?

我需要从[NSDate日期] 00:00(当天开始)获取NSDate对象,假设当前是上午11:30(由[NSDate日期]返回),在01/06/2012,现在我需要在01/06/2012 00:00 am获得NSDate.

我没试过这个,但我的想法是:

NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:now];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSDate *morningStart = [calendar dateFromComponents:components];
Run Code Online (Sandbox Code Playgroud)

所以我首先获得当前日期(比如说它是01/06/2012),并为日期构建一个NSDateComponent,然后我将小时/分钟/秒设置为0,并且不应更改年/月/日(即01)/06/2012)然后我为这个组件设置创建一个NSDate,我可以得到00:00:00 01/06/2012的日期吗?

iphone nsdate nscalendar nsdatecomponents

12
推荐指数
2
解决办法
6604
查看次数

如何减去日期组件?

就像今天一样Friday,这是6根据NSCalendar.我可以通过使用以下内容得到这个

Calendar.current.component(.weekday, from: Date())
Run Code Online (Sandbox Code Playgroud)

我怎么得到Saturday上周的工作日组件,应该是7哪个?

如果我这样做 Calendar.current.component(.weekday, from: Date()) - 6.我得到0哪个不是有效的组件.

nscalendar ios swift

12
推荐指数
2
解决办法
6281
查看次数

以字符串swift 3.0获取当前时间

如何将当前时间作为字符串(而不是日期)

日期时间格式.

yyyy-MM-dd HH:mm:ss

time nscalendar swift swift3

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

iOS创建日期,忽略夏令时

我正在尝试使用日期并在将来创建日期,但夏令时不断妨碍我的时间.

这是我的代码,以移动到下个月的第一天的午夜约会:

+ (NSDate *)firstDayOfNextMonthForDate:(NSDate*)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    calendar.locale = [NSLocale currentLocale];

    NSDate *currentDate = [NSDate dateByAddingMonths:1 toDate:date];
    NSDateComponents *components = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit
                                                    fromDate:currentDate];

    [components setDay:1];
    [components setHour:0];
    [components setMinute:0];
    [components setSecond:0];

    return [calendar dateFromComponents:components];
}

+ (NSDate *) dateByAddingMonths: (NSInteger) monthsToAdd toDate:(NSDate*)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    calendar.timeZone = [NSTimeZone systemTimeZone];
    calendar.locale = [NSLocale currentLocale];

    NSDateComponents * months = [[NSDateComponents alloc] init];
    [months setMonth: monthsToAdd]; …
Run Code Online (Sandbox Code Playgroud)

nsdate nstimezone nscalendar ios

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

如何确定iOS中的第一周工作日?

我在我的应用程序中使用Tapku的日历,我想确定一周应该在周日或周一开始,具体取决于用户的设置.我打电话给第一周,但由于某种原因,它会在一个设备上返回1(星期日),其中内置日历在星期一开始几周(因此它应该返回2):

NSCalendar* cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[cal firstWeekday];
Run Code Online (Sandbox Code Playgroud)

关于我可能缺少什么的任何建议?

locale objective-c nscalendar ios

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

将年份组件添加到伊斯兰日历失败

以下代码显示了问题:从1435年的第一天开始整整一年不会导致1436年的第一天.

我缺少什么想法?

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:1];
[components setMonth:1];
[components setYear:1435];


NSCalendar *islamic = [[NSCalendar alloc] initWithCalendarIdentifier:NSIslamicCalendar];
NSDate *date = [islamic dateFromComponents:components];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setCalendar:islamic];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

NSLog(@"%@", [dateFormatter stringFromDate:date]);  // -> 01.01.1435

NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setYear:1];

NSDate *dateWithOffset = [islamic dateByAddingComponents:offsetComponents toDate:date options:0];

NSLog(@"%@", [dateFormatter stringFromDate:dateWithOffset]); 
// -> 30.12.1435 ... WHY NOT 01.01.1436 ????
Run Code Online (Sandbox Code Playgroud)

objective-c nscalendar nsdatecomponents ios

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

什么是iOS 8.0+ NSCalendarUnits?

我注意到从iOS 8.0开始,所有NSCalendarUnits(例如NSDayCalendarUnit)都被正式弃用.

什么是替代品?

nscalendar nsdatecomponents ios8

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

dateByAddingComponents:toDate:options什么时候返回nil?

我一直在使用dateByAddingComponents:toDate:options:dateByAddingUnit:value:toDate:options:使用可选绑定来获取日期.像这样:

guard let startOfNextMonth = calendar.dateByAddingComponents(oneMonthComponent, toDate: startOfThisMonth, options: []) else {
    return nil
}
Run Code Online (Sandbox Code Playgroud)

其中oneMonthComponent只是一个月份值设置为1的NSDateComponent.

当我读到它时,在文档中他们都说了如下:

如果日期超出接收器的定义范围或者无法执行计算,则返回nil.

而且我一直在想,究竟是什么时候造成的.如果我只是添加一个月的日期,有没有办法可以是零?这是因为在其他日历系统中存在一些差异,其中添加一些单位没有意义吗?

我已经四处寻找并且无法找到一个返回nil的实例.

是否有任何可以使上面的代码为零?添加组件可能为零的示例是什么?

nsdate nscalendar nsdatecomponents ios swift

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