nsdatecomponents

NSDateComponents of an NSDate

自作多情 提交于 2019-11-27 23:01:25
问题 How do I get the NSDateComponents of a single NSDate? I don't want the components of the difference between 2 dates, just the seconds, minutes, hours, day, month and year of a NSDate? 回答1: There's an example in the Apple docs, Listing 3: Getting a date’s components : NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit)

I want to get a quarter value in NSDateComponents class

跟風遠走 提交于 2019-11-27 16:12:47
I want to get a quarter value from specified date. (*date variable) So. I have made following code and run. but, quarter value is zero. Why quarter value is zero? where's problem? Please advice from advanced man. NSDateComponents *comp1 = [[NSDateComponents alloc] init]; [comp1 setYear:2012]; [comp1 setMonth:6]; [comp1 setDay:1]; NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate *date = [gregorian dateFromComponents:comp1]; <== want to get a quarter value. unsigned int unitFlags = NSYearCalendarUnit | NSQuarterCalendarUnit | NSMonthCalendarUnit

How to set repeat frequency in User Notification [duplicate]

≯℡__Kan透↙ 提交于 2019-11-27 14:05:52
问题 This question already has answers here : How do I set an NSCalendarUnitMinute repeatInterval on iOS 10 UserNotifications? (3 answers) Closed 2 years ago . Till iOS 9 we write local notifications like this UILocalNotification* localNotification = [[UILocalNotification alloc] init]; localNotification.fireDate = pickerDate; localNotification.alertBody = self.textField.text; localNotification.timeZone = [NSTimeZone defaultTimeZone]; localNotification.repeatInterval = NSCalendarUnitMinute;

iOS时间那点事--NSDate分类

我的未来我决定 提交于 2019-11-27 12:06:01
#iOS 时间那点事 ##NSDate分类(Catogery) ###分类 类,是面向对象语言的基本;类,是具有相同属性和行为的一类事物的概括,这是一种抽象;但是,事物总是多方面的,如此之多以至于我们每每看到深不见底的头文件就恐高,于是乎我们使用了继承,鉴于管理和使用的高额成本,我们创造了“抽象工厂模式”,Perfect!但我们怎么有时心里就是高兴不起来呢?因为有些时候可以不用继承,但是又没有其他办法,只好用继承,并且不得不承担使用继承的带来的后果。Objective-C就在这时来到了你的身边。[单继承!!??什么东西?(C++)] [还不是跟我们一样~(Java)]。关于单继承和多继承的问题,作为一介平民,不做过多讨论,用着用着就习惯了…… 全世界针对这个问题给出了同样的答案:接口(和Objective-C中的协议一样的东西)。这就结束了吗??!!类还是深不见底啊!那就彻底一次吧,不要磨磨唧唧的了,一个类可以分开定义和实现,放到多个的文件里,这样把基本的东西留在原来的文件中,其他比较特定的东西放到其他文件里面,搞个合理的命名规则,就叫“分类”吧。 分类,通过分离特定功能的代码,解决大型代码框架的维护的问题。任何东西的出现都是有其一定的原因的,就像面向对象语言的出现一样,我们现在只不过是在不断地完善面向对象语言而已,可见前方的路还很远。 NSDate是一个时间类

ios时间那点事--NSCalendar NSDateComponents

混江龙づ霸主 提交于 2019-11-27 12:05:51
#iOS时间那点事 ##NSCalendar + NSDateComponents 历法能使人类确定每一日再无限的时间中的确切位置并记录历史。 日历,历法,一般历法都是遵循固定的规则的,具有周期性。日历都是已知的或可预测的。 任何一种具体的历法,首先必须明确规定起始点,即开始计算的年代,这叫“纪元”;以及规定一年的开端,这叫“岁首”。此外,还要规定每年所含的日数,如何划分月份,每月有多少天等等。 NSCalendar对世界上现存的常用的历法进行了封装,既提供了不同历法的时间信息,又支持日历的计算。 NSDateFomatter 表示的时间默认以公历(即阳历)为参考,可以通过设置calendar属性变量获得特定历法下的时间表示。 NSDate 是独立与任何历法的,它只是时间相对于某个时间点的时间差; NSDate 是进行日历计算的基础。 NSDateComponents将时间表示成适合人类阅读和使用的方式,通过NSDateComponents可以快速而简单地获取某个时间点对应的“年”,“月”,“日”,“时”,“分”,“秒”,“周”等信息。当然一旦涉及了年月日时分秒就要和某个历法绑定,因此NSDateComponents必须和NSCalendar一起使用,默认为公历。 NSDateComponents除了像上面说的表示一个时间点外,还可以表示时间段,例如:两周,三个月,20年,7天

NSDate and NSDateComponents return wrong date

随声附和 提交于 2019-11-27 08:37:29
问题 I know that this question was asked a million times, I just can't find a solution. The problem is: I'm modifying an NSDate to return either the date of the first day in the current month or the date of the first day in the next month. It seems to be working fine except for 1 thing: it gives back the time minus 2 or 3 hours. Code: NSCalendar *theCalendar = [NSCalendar currentCalendar]; int comp = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit; NSDateComponents *theComp =

How to print name of the day of the week?

两盒软妹~` 提交于 2019-11-27 07:33:38
问题 Im trying to print out the name of the day of the week i.e Monday, Tuesday, Wednesday. I currently have this bit of code that does just that. I was wondering if there is a way to get rid of my switch statement and make this better. Thanks! func getDayOfWeek(_ today: String) -> String? { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" guard let todayDate = formatter.date(from: today) else { return nil } let myCalendar = Calendar(identifier: .gregorian) let weekDay =

NSDate beginning of day and end of day

安稳与你 提交于 2019-11-27 03:24:55
-(NSDate *)beginningOfDay:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:date]; [components setHour:0]; [components setMinute:0]; [components setSecond:0]; return [cal dateFromComponents:components]; } -(NSDate *)endOfDay:(NSDate *)date { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *components = [cal components:( NSMonthCalendarUnit | NSYearCalendarUnit | NSHourCalendarUnit |

Calculate age from birth date using NSDateComponents in Swift

梦想的初衷 提交于 2019-11-27 01:32:48
I am trying calculate the age from birthdayDate in Swift with this function: var calendar : NSCalendar = NSCalendar.currentCalendar() var dateComponentNow : NSDateComponents = calendar.components( NSCalendarUnit.CalendarUnitYear, fromDate: birthday, toDate: age, options: 0) But I get an error Extra argument toDate in call In objective c this was the code, but I don't know why get this error: NSDate* birthday = ...; NSDate* now = [NSDate date]; NSDateComponents* ageComponents = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:birthday toDate:now options:0]; NSInteger age =

how do I set an existing NSDate's time?

こ雲淡風輕ζ 提交于 2019-11-27 01:17:07
问题 how do I set an existing NSDate 's time? That is I have a date (say current date/time), but then want to set this to a specific time (e.g. 11.23am) say. What is the quickest way to do this in objective C? I'm looking at NSCalendar and see methods such as dateByAddingComponents:toDate:options: , and dateFromComponents: , but these don't seem to quite hit the mark for what I need here. For example: dateFromComponents: - I would have to work out all components for this to work which seems like