我试图将NSDate的hh,mm,ss组件改为另一个NSDate的组件 - 但出于某种原因,由于某种原因,时间设置为午夜.我无法弄清楚为什么!
这是代码;
+ (NSDate *) fixTime:(NSDate*)fromDate toDate:(NSDate *) toDate {
NSCalendar *cal = [NSCalendar currentCalendar];
[cal setTimeZone:[NSTimeZone localTimeZone]];
[cal setLocale:[NSLocale currentLocale]];
NSLog(@"fromDate=<%@>", [fromDate descriptionWithLocale:[NSLocale currentLocale]]);
NSLog(@"toDate= <%@>", [toDate descriptionWithLocale:[NSLocale currentLocale]]);
NSDateComponents *fromTimeComponents = [cal components:( NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit ) fromDate:fromDate];
NSDateComponents *toDateComponents = [cal components:( NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit ) fromDate:toDate];
NSDateComponents *toTimeComponents = [cal components:( NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit ) fromDate:toDate];
[toTimeComponents setHour:[fromTimeComponents hour]];
[toTimeComponents setMinute:[fromTimeComponents minute]];
[toTimeComponents setSecond:[fromTimeComponents second]];
NSLog(@"toDateComponents year = …Run Code Online (Sandbox Code Playgroud) 我一直在使用Swift中的时钟应用程序和Xcode 6.3.2以下代码构建并运行得很好.
// Get current time
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitSecond | .CalendarUnitNanosecond, fromDate: date)
let hour = components.hour % 12
let minute = components.minute
let second = components.second
let nanosecond = components.nanosecond
Run Code Online (Sandbox Code Playgroud)
但是,当我在Xcode 7.0 beta中加载相同的项目并且不做任何更改时,我在calendar.components行上出现错误.
Could not find member 'CalendarUnitHour'
Run Code Online (Sandbox Code Playgroud)
我查看了文档,并且不推荐使用所有NSCalendarUnit常量(在iOS 8.0中说),但组件方法的方法描述仍然表示要使用它们.
我已经玩过其他NSCalendarUnit自动完成值,但没有产生工作代码,我在网上找不到任何最近的例子,也许是因为这是全新的.
有人知道新的正确方法吗?
是date1 == date2不是比较有效的方法是什么?如果没有,那么正确的选择是什么?
这是我的代码:
- (NSDate*) dateWithNoTime {
unsigned int flags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:flags fromDate:self];
NSDate* dateOnly = [calendar dateFromComponents:components];
return dateOnly;
}
- (BOOL) sameDayAsDate:(NSDate*)dateToCompare {
NSDate *date1 = [self dateWithNoTime];
NSDate *date2 = [dateToCompare dateWithNoTime];
return date1 == date2; // HERE IS WHERE THINGS SEEM TO FAIL
}
Run Code Online (Sandbox Code Playgroud) 您好我设置firstWeekDay有问题,这是我做的:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:targetDate];
[comp setWeekday:2];
NSDate *firstWeekDay = [gregorian dateFromComponents:comp];
Run Code Online (Sandbox Code Playgroud)
如果2011-01-01周六是targetDate,我日历中的第一周周日似乎是2011-12-26周一,但实际上应该是2010-12-26周一.我怎么能做对的?
我查看了iOS 10 Home应用程序.屏幕截图仅从Home应用程序中捕获.
自从过去两天以来,我一直在尝试实现HMTimerTrigger重复功能.我的要求是我必须在每个星期一,星期二和星期五重复触发.我发现的是我只能添加一天(周一或周二......但不是周一和周二),如下所示.
unsigned flags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitWeekOfYear | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute;
NSDate *fireDate = [NSDate date];
NSDateComponents *recurrenceComponents = [[NSDateComponents alloc] init];
recurrenceComponents.weekday = 2; // For monday
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:flags fromDate:fireDate];
fireDate = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
HMTimerTrigger *trigger = [[HMTimerTrigger alloc] initWithName:triggerName.text
fireDate:fireDate
timeZone:nil
recurrence:recurrenceComponents
recurrenceCalendar:[NSCalendar currentCalendar]];
Run Code Online (Sandbox Code Playgroud)
感谢您阅读我的帖子.任何想法/建议都会非常有帮助.
我想弄清楚任何特定日期(即2009年6月27日)的哪一天(即周一,周五......)
谢谢.
我需要从[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的日期吗?
我想打印,例如,值1.5为"1.5秒",就像Safari的时间轴一样,我正在尝试使用DateComponentsFormatter.
不幸的是,它.allowedUnits只能归结为.second(即使枚举有.nanosecond).我尝试过设置.allowsFractionalUnits = true,但我仍然得到"0秒".
有没有办法从DateComponentsFormatter中获得小数秒?
我有一个日期,我需要拆分一些组件.例如
let components = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour
let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil)
var dateToPrint = "\(date.day) days \(date.hour) hours"
Run Code Online (Sandbox Code Playgroud)
dateToPrint将是从aDate到现在的天数和小时数.但如果我想要几周而不是几天
let components = NSCalendarUnit.CalendarUnitWeek | NSCalendarUnit.CalendarUnitHour
let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil)
var dateToPrint = "\(date.week) weeks \(date.hour) hours"
Run Code Online (Sandbox Code Playgroud)
date.week不存在.那我怎么解决这个问题呢?
我在iOS 8上使用NSDateComponentsFormatter,我注意到该stringFromTimeInterval:方法的实现可能存在错误,但我不能肯定地说.
这就是我设置格式化程序的方法:
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.zeroFormattingBehavior = NSDateComponentsFormatterZeroFormattingBehaviorDefault;
formatter.allowedUnits = NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
formatter.unitsStyle = NSDateComponentsFormatterUnitsStylePositional;
Run Code Online (Sandbox Code Playgroud)
现在,当我向stringFromTimeInterval:方法传递少于60秒的任何内容时,格式化程序返回1或者01,这显然是错误的,它应该返回类似的东西0:XX.
我还注意到,通过更改unitsStyle为任何东西,但NSDateComponentsFormatterUnitsStylePositional方法返回正确的字符串!
例:
用unitsStyle = NSDateComponentsFormatterUnitsStylePositional:
(lldb) po [formatter stringFromTimeInterval:12]
= "1"
Run Code Online (Sandbox Code Playgroud)
=>应该返回"0:12"!!
用unitsStyle = NSDateComponentsFormatterUnitsStyleAbbreviated:
(lldb) po [formatter stringFromTimeInterval:12]
= "12 s"
Run Code Online (Sandbox Code Playgroud)
=>正确!
这让我觉得这是Apple实现API的一个错误,还是我错过了什么?
nsdatecomponents ×10
nsdate ×6
objective-c ×6
nscalendar ×4
ios ×3
iphone ×3
cocoa ×2
macos ×2
swift ×2
cocoa-touch ×1
foundation ×1
homekit ×1
swift2 ×1