草庐IT

nsdatecomponent

全部标签

ios - NSDateComponents weekOfYear 崩溃

我正在尝试使用以下代码从NSDate中获取一年中的星期几:intwhich=NSYearCalendarUnit|NSMonthCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekOfYearCalendarUnit|NSQuarterCalendarUnit;NSDate*date=[NSDatedate];NSDateComponents*comps=[[NSCalendarcurrentCalendar]components:whichfromDate:date];toda

ios - 从 MonoTouch 中的 NSDate(或 DateTime)获取 NSDateComponents 实例

我需要得到一个完全定义的NSDateComponents实例(即设置了日历、时区和实际日期值等)用作StartDateComponents和DueDateComponentsEKReminder的值.有很多answershereonSO解释如何从obj-c中的日历实例执行此操作。和开源项目doingthesamething.但是我似乎无法弄清楚如何在MT中做同样的事情。NSCalendar类不公开任何与components:fromDate:选择器相关的方法。人们如何处理MT中的DateTime/NSDate->NSDateComponents? 最佳答案

ios - NSDateFormatter 和 NSDateComponents 返回不同的周数值

我正在制作一个创建时间表的应用程序。您可以在一年中的每个星期创建一个新的。应用加载完成后,需要加载当前周(例如:如果是1月1日,则需要显示第1周)。我使用NSDateFormatter来确定当前星期几。NSDateFormatter(我在2016年8月9日对此进行了测试)NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init];[dateFormattersetDateFormat:@"ww"];intcurrentWeek=[[timestringFromDate:[NSDatedate]]intValue];我想检查它是否

ios - NSDateComponents 时间配置文件

我的问题是处理下面代码片段中给出的NSDateComponents操作的时间配置文件。我有一个迭代大量NSDate对象的方法该方法的一部分要求我放弃每个日期的小时、分钟和秒数。为此,我有3个步骤:我从我的原始日期创建了一个NSDateComponents对象。我将H:M:S元素设置为0我根据需要将H:M:S设置为0的新日期对象问题:上面的步骤1和3占用了我整个方法执行时间的很大一部分,分别为22.4%和56.8%。我正在寻找有关如何优化我的这部分代码或将H:M:S归零的其他方法的建议,这些方法可能会表现得更好。NSDate*date=[[NSDatealloc]init];//Next

objective-c - NSDateComponents 工作日不显示正确的工作日?

我得到了一个NSDate,例如1/6-12(星期五),并试图找出它是星期几。我的一周从星期一开始,所以星期五应该是工作日5。NSCalendar*calendar=[[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];[calendarsetFirstWeekday:2];NSDateComponents*components=[calendarcomponents:NSWeekdayCalendarUnitfromDate:firstOfJulyDate];NSLog(@"weekday%i",compon

ios - - [NSDateComponents week] 已弃用,哪个枚举提供与以前相同的行为?

documentationforNSDateComponents表示从iOS7开始,week已弃用,改用weekOfDay或weekOfYear。如果我想获得与使用week时相同的逻辑,我应该使用其中的哪一个?我在我的代码中的很多地方都使用过它,所以考虑每个场景都很麻烦。如果有一个枚举映射到那个星期最初做的同一件事,就会节省很多时间。 最佳答案 在文档中没有找到任何内容,但我的代码中有这个:NSDateComponents*todayComps=[calendarcomponents:NSYearCalendarUnit|NSMon

ios - NSDateComponents 周在 iOS8 上的工作方式不同

NSDateComponentsweek自iOS8以来已弃用。Apple建议根据上下文使用weekOfMonth或weekOfYear,但它们的工作方式不同。例如:components1=[calendarcomponents:NSMonthCalendarUnit|NSWeekCalendarUnit|NSDayCalendarUnitfromDate:[selfdate7DaysAgo]toDate:[NSDatedate]options:0];components1返回month=0,week=1,day=0,weekOfMonth和weekOfYear都等于2147483647

ios - Swift 中的 NSDateComponents 不返回工作日

importUIKitletcomponents=NSDateComponents()components.setValue(1,forComponent:NSCalendarUnit.CalendarUnitDay)components.setValue(1,forComponent:NSCalendarUnit.CalendarUnitMonth)components.setValue(2014,forComponent:NSCalendarUnit.CalendarUnitYear)println(components.weekday)//9223372036854775807有

ios - 获取单个 NSDateComponents 的 2 个日期之间的确切差异

如何获得NSDate的两个值之间的确切差异(十进制)。例如。2016年1月15日到2017年7月15日=1.5年。我可以使用类似的东西:NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear,fromDate:date1,toDate:date1,options:nil).year但这给了我绝对值。即对于上面的例子,它会给我1年。是否有可能获得至少精确到小数点后几位的精确值? 最佳答案 您在此处使用的术语具有误导性。当你说“绝对”时,你的意思是“

swift - NSDateComponents 语法的差异?

我一直在使用Swift和Xcode6.3.2开发一个时钟应用程序,以下代码构建和运行得很好。//Getcurrenttimeletdate=NSDate()letcalendar=NSCalendar.currentCalendar()letcomponents=calendar.components(.CalendarUnitHour|.CalendarUnitMinute|.CalendarUnitSecond|.CalendarUnitNanosecond,fromDate:date)lethour=components.hour%12letminute=components.m