我过去使用以下函数来使用NSDateComponents现有日期添加特定时间间隔.
(NSDate *)dateByAddingComponents:(NSDateComponents *)comps
toDate:(NSDate *)date
options:(NSCalendarOptions)opts
Run Code Online (Sandbox Code Playgroud)
从iOS8开始,不推荐使用周值NSDateComponents,这意味着我无法实现我想要做的事情:NSDate通过向给定数量添加特定周数来生成新值NSDate.
任何帮助将不胜感激.
我有这个功能:
private func getCurrentDateComponent() -> NSDateComponents {
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: date)
return components
}
Run Code Online (Sandbox Code Playgroud)
当我打电话给我并使用该功能 date
var d = this.getCurrentDateComponent().date
Run Code Online (Sandbox Code Playgroud)
我不知道为什么变量d是零...
谢谢您的帮助
Ysee
我正试图在不列颠哥伦比亚省时代建立一个约会,但是很难失败.以下返回'4713'作为年份,而不是'-4712':
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [NSDateComponents new];
[components setYear: -4712];
NSDate *date = [calendar dateFromComponents:components];
NSLog(@"%d", [[calendar components:NSYearCalendarUnit fromDate: date] year]);
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
更新:工作代码
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [NSDateComponents new];
[components setYear: -4712];
NSDate *date = [calendar dateFromComponents:components];
NSDateComponents *newComponents = [calendar components:NSEraCalendarUnit|NSYearCalendarUnit fromDate:date];
NSLog(@"Era: %d, year %d", [newComponents era], [newComponents year]);
Run Code Online (Sandbox Code Playgroud)
正如本解释的那样,这为时代打印0.
今天我正在玩NSCalendar和NSDateComponents,我看到了像NSDayCalendarUnit和NSEraCalendarUnit这样的常量.如果今天是12月4日,则NSDayCalendarUnit返回4.NSEraCalendarUnit返回发布时显然为1的时代.有人在乎解释什么是时代吗?
我想确定iOS中当前日期的日,月和年.我在网上搜索后发现了这个:
NSDate *date = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:(NSDayCalendarUnit) fromDate:date];
NSInteger Day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
Run Code Online (Sandbox Code Playgroud)
此代码显示当天正确,但年份和月份未显示应该如此.它显示了一些数字,如2147483647.我找不到解决方案,所以请帮我纠正我找到的代码,或者写一些新的代码来完成我需要的工作.
我想将NSDate从一个时区转换为另一个时区.
这是我的代码:
NSDate* convertTime(NSDate* fromDate, NSTimeZone* fromTimeZone, NSTimeZone* toTimeZone) {
if (fromTimeZone == toTimeZone) {
return fromDate;
}
NSCalendarUnit val = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour| NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitTimeZone;
NSCalendar* dupCal = [[NSCalendar currentCalendar] copy];
[dupCal setTimeZone:toTimeZone];
NSDateComponents *dupComponents = [dupCal components:val fromDate:fromDate];
return [dupComponents date]; // <- Why return nil?
}
int testTimeZone() {
NSDate* res = convertTime([NSDate date], [NSTimeZone defaultTimeZone], [NSTimeZone timeZoneWithName:@"America/New_York"]);
NSLog(@"convertTime: %@", res);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在输出窗口中,我可以看到这个打印输出:
2014-02-15 11:15:18.040 MyApp[28742:70b] convertTime: (null)
Run Code Online (Sandbox Code Playgroud)
由于某种原因 …
NSDateComponents week从iOS8开始不推荐使用.Apple建议使用weekOfMonth或weekOfYear取而代之,取决于上下文,但它们以不同的方式工作.例如:
components1 = [calendar components:NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit
fromDate:[self date7DaysAgo]
toDate:[NSDate date]
options:0];
Run Code Online (Sandbox Code Playgroud)
components1返回month = 0,week = 1,day = 0,weekOfMonth并且weekOfYear都等于2147483647(整数最大值).
components2 = [calendar components:NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit
fromDate:[self date30DaysAgo]
toDate:[NSDate date]
options:0];
Run Code Online (Sandbox Code Playgroud)
components2返回month = 1(我知道它取决于,但它只是一个例子),week = 0,day = 0,weekOfMonth并且weekOfYear都等于2147483647(整数最大值).
Week返回某段时间内的周数(如果是一周或多数),它与月或一周中的周数完全不同.苹果为什么建议使用它呢?
为什么是string零?
let formatter = NSDateComponentsFormatter()
let referenceDate = NSDate(timeIntervalSinceReferenceDate: 0)
let intervalDate = NSDate(timeInterval: 3628810, sinceDate: referenceDate)
let string = formatter.stringFromDate(referenceDate, toDate: intervalDate)
Run Code Online (Sandbox Code Playgroud)
我期待返回像"6w 10s"这样的字符串.
(6周是3,628,800秒.)
要进行故障排除,我尝试设置allowedUnits:
formatter.allowedUnits = .YearCalendarUnit | .MonthCalendarUnit | .WeekCalendarUnit | .DayCalendarUnit | .HourCalendarUnit | .MinuteCalendarUnit | .SecondCalendarUnit
Run Code Online (Sandbox Code Playgroud)
导致此错误:
"NSInvalidArgumentException","指定带间隙的位置单位是不明确的,因此不受支持"
我不知道什么是"位置单位"(在足球之外),我认为我没有指出任何差距.
我没有使用,stringFromTimeInterval()因为根据系统的当前日期,它将返回不同的结果.(即,二月只有一个月的28/29天.)我想计算从NSDate参考日期开始的时间间隔.
我正在使用Xcode 6.1.1(6A2008a).这是一个Playground屏幕截图,如果有帮助:

nsdate nsdateformatter nsdatecomponents swift nsdatecomponentsformatter
假设今天的日期是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
nsdatecomponents ×10
nsdate ×7
ios ×6
swift ×4
objective-c ×3
cocoa ×2
ios8 ×2
datepicker ×1
int ×1
iphone ×1
nscalendar ×1
nstimezone ×1
uidatepicker ×1