假设今天的日期是2016年5月16日,我想限制用户显示截至2016年6月16日的日期.
我相信这个问题已经被问过好几次了,但没有明确的答案。
有两种方法可以安排临时通知:UNCalendarNotification和 UNTimeIntervalNotificationTrigger。
安排一个特定时间和一周中的某一天的通知很简单,与一个月中的特定一天相同,但安排一个特定的时间,每 n 天就不那么简单了。
例如,每 5 天 11:00。
UNTimeIntervalNotificationTrigger看起来似乎是合适的课程,但在夏令时或时区变化发生时会出现问题。例如,夏令时结束,现在您的通知时间为 10:00,而不是 11:00。
day类上的属性和DateComponents类UNCalendarNotification可能包含解决方案,因为它在文档中说“一天或天数”。我将其解释为“一个月中的特定一天(一天)或n天数(天数)”。
进一步深入研究day属性文档,它显示“此值在使用它的日历的上下文中进行解释”。
如何将该day属性与日历上下文一起使用来计算天数而不是每月的特定天数?
hour此外,和minute属性的文档DateComponents还分别为“一小时或数小时”和“一分钟或数分钟”。那么,即使您要设置day为“天数”,如何正确设置hour和呢?minute
很明显,此功能在 iOS 中是可行的 - 提醒应用程序就是证明。
nsdatecomponents ios uilocalnotification swift unusernotificationcenter
我正在为iPhone编写一个GTD应用程序.对于应有的任务,我希望显示诸如"明天到期"或"到期日"或"到期7月18日"之类的内容.显然,即使任务距离不到24小时,我也需要显示"明天"(例如,用户在星期六晚上11点检查并看到周日早上8点有任务).所以,我写了一个方法来获取两个日期之间的天数.这是代码......
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd-HH-mm"];
NSDate *nowDate = [dateFormatter dateFromString:@"2010-01-01-15-00"];
NSDate *dueDate = [dateFormatter dateFromString:@"2010-01-02-14-00"];
NSLog(@"NSDate *nowDate = %@", nowDate);
NSLog(@"NSDate *dueDate = %@", dueDate);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *differenceComponents = [calendar components:(NSDayCalendarUnit)
fromDate:nowDate
toDate:dueDate
options:0];
NSLog(@"Days between dates: %d", [differenceComponents day]);
Run Code Online (Sandbox Code Playgroud)
......这是输出:
NSDate *nowDate = 2010-01-01 15:00:00 -0700
NSDate *dueDate = 2010-01-02 14:00:00 -0700
Days between dates: 0
Run Code Online (Sandbox Code Playgroud)
如您所见,该方法返回不正确的结果.它应该返回1作为两天之间的天数.我在这做错了什么?
编辑:我写了另一种方法.我没有进行过广泛的单元测试,但到目前为止似乎有效:
+ (NSInteger)daysFromDate:(NSDate *)fromDate inTimeZone:(NSTimeZone *)fromTimeZone untilDate:(NSDate *)toDate inTimeZone:(NSTimeZone *)toTimeZone {
NSCalendar *calendar = …Run Code Online (Sandbox Code Playgroud) 我期待2010-10-11 00:00:00
但相反,我收到2010-10-10 21:00:00 +0000
我知道,我可以添加几个小时并收到所需的日期,但我不明白为什么我前一天有21个小时......
请帮忙!
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:2010];
[comps setMonth:10];
[comps setDay:11];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *retDate = [cal dateFromComponents:comps];
Run Code Online (Sandbox Code Playgroud) 我想把当前的小时和分钟作为整数.所以,如果现在是凌晨3:16,我想得到两个整数:3和16.
但它看起来[NSDate date]会给出自1970年以来的秒数,或者它可以给出一个当前时间表示的字符串,但是没有简单的方法可以将它们作为整数?
我在" 获取当前时间"中看到了一个帖子,但它涉及NSDateComponents到了NSCalendar什么?这太复杂了......所有需要的东西都是这样的
NSDate *date = [NSDate date];
int hour = [date getHour]; // which is not possible
Run Code Online (Sandbox Code Playgroud)
是否有一个比使用3类简单的方法NSDate,NSDateComponents以及NSCalendar得到当前小时为一个整数,或者通常,在Objective-C,将我们通常还是用C语言的localtime,并tm得到小时为一个整数?
一年有多少个星期?因为我在网上搜索过,我发现一年总共52.1周,但如果我在Xcode中这样做:
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setFirstWeekday:2];
NSDate* sourceDate = [NSDate date];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* today = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
NSDateComponents *todaysComponents = [gregorian components:NSWeekCalendarUnit fromDate:today];
NSUInteger todaysWeek = [todaysComponents week];
Run Code Online (Sandbox Code Playgroud)
今日周刊的价值是:53,怎么可能?为什么不是52?
我想找出一年中第一周的日期:
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setYear:2013];
[components setMonth:1];
[components setWeekOfMonth:1];
[components setWeekday:1];
NSDate *newDate = [calendar dateFromComponents:components];
NSLog(@"%@",newDate);
Run Code Online (Sandbox Code Playgroud)
我得到的是:
2012-12-29 23:00:00 +0000
Run Code Online (Sandbox Code Playgroud)
当我与我的 mac 日历进行比较时,我需要得到的是:
2012-12-31 23:00:00 +0000
Run Code Online (Sandbox Code Playgroud)
有什么建议?
我需要得到这个月第一天的工作日.例如,对于2013年9月的当月,第一天是星期日.
我知道如何获得当地的日期和时间,但我想要做的是从不同的地方获取日期和时间.例如,我想知道纽约的时间和日期.我该如何解决这个简单的问题?
这是我的本地日期和时间代码:
let date = NSDate()
let calendar = Calendar.current
let components = calendar.dateComponents([.hour, .minute, .month, .year, .day, .second, .weekOfMonth], from: date as Date)
let currentDate = calendar.date(from: components)
Run Code Online (Sandbox Code Playgroud)
我在这里搜索了一下,但我找不到我想要的东西,而且我还在寻找日期图书馆.如果您知道要重定向我的任何来源或样本,我真的很感激.
我在 Swift 中使用以下 DateComponentsFormatter:
let formatter = DateComponentsFormatter()
formatter.unitsStyle = .positional
formatter.allowedUnits = [.hour, .minute, .second]
formatter.zeroFormattingBehavior = [.default]
Run Code Online (Sandbox Code Playgroud)
这会产生类似 2:41(2 分 41 秒)和 08(8 秒)的结果。但是,我想在 Minutes 位置至少保留一位数字,返回 0:08 而不是 08。这可能与 DateComponentsFormatter 一起使用吗?我更喜欢返回本地化结果的解决方案。
nsdatecomponents ×10
ios ×8
nsdate ×5
swift ×4
iphone ×3
nscalendar ×3
cocoa-touch ×2
date ×2
calendar ×1
cocoa ×1
components ×1
datepicker ×1
objective-c ×1
uidatepicker ×1