nscalendar

Compare two NSDates for same date/time [duplicate]

别等时光非礼了梦想. 提交于 2019-11-28 08:25:29
This question already has an answer here: How to compare two dates in Objective-C 14 answers Is date1 == date2 not a valid way to compare? If not, what is the correct alternative? Here's my code: - (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]

how do I set an existing NSDate's time?

限于喜欢 提交于 2019-11-28 06:50:29
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 overkill dateByAddingComponents:toDate:options: - I don't want to ADD in my case but rather "set" the

Difference between NSCalendarUnitWeekOfYear & NSCalendarUnitWeekOfMonth

孤街醉人 提交于 2019-11-28 02:55:59
问题 I want to make a UILocalNotification repeat every week, before iOS7 I would use localNotification.repeatInterval = NSWeekCalendarUnit - except NSWeekCalendarUnit has been deprecated. The docs say: "Use NSCalendarUnitWeekOfMonth or NSCalendarUnitWeekOfYear, depending on which you mean" But I don't know what the difference is, so I don't know which one I mean. 回答1: These don't appear to be documented but most likely NSCalendarUnitWeekOfYear will be a value from 1-53 (or maybe 0-52) representing

Swift - Get date from day of year

喜欢而已 提交于 2019-11-28 02:05:02
We can get day of year for date using below line. let day = cal.ordinalityOfUnit(.Day, inUnit: .Year, forDate: date) But how can we get the date from day of year? If you know the year you can get DateComponents date property as follow: extension Calendar { static let iso8601 = Calendar(identifier: .iso8601) } let now = Date() let day = Calendar.iso8601.ordinality(of: .day, in: .year, for: now)! // 121 let year = Calendar.iso8601.component(.year, from: now) // 2017 let date = DateComponents(calendar: .iso8601, year: year, day: day).date // "May 1, 2017, 12:00 AM" or using DateFormatter let

Number of days in the current month using iOS?

主宰稳场 提交于 2019-11-27 19:13:02
How can I get the current number of days in the current month using NSDate or something similar in Cocoa touch? You can use the NSDate and NSCalendar classes: NSDate *today = [NSDate date]; //Get a date object for today's date NSCalendar *c = [NSCalendar currentCalendar]; NSRange days = [c rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:today]; today is an NSDate object representing the current date; this can be used to work out the number of days in the current month. An NSCalendar object is then instantiated, which can be used, in conjunction with the NSDate for the current

Get current time as string swift 3.0 [duplicate]

半腔热情 提交于 2019-11-27 17:55:08
问题 This question already has answers here : Converting NSString to NSDate (and back again) (17 answers) Closed 2 years ago . How to get current time as a String (not as Date) date time format. yyyy-MM-dd HH:mm:ss 回答1: This is the way I figure it out. // Get today date as String func getTodayString() -> String{ let date = Date() let calender = Calendar.current let components = calender.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date) let year = components.year let month =

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

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天

iPhone OS: How do I create an NSDate for a specific date?

旧城冷巷雨未停 提交于 2019-11-27 09:23:05
问题 Seems like a simple thing but I can't seem to find a way to do it. It would be great to see a couple different methods. 回答1: @Chuck's answer is correct, and lead me to the following code. Thought I'd share: NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:10]; [comps setMonth:10]; [comps setYear:2010]; NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps]; 回答2: You can use this NSString *dateString = @"03-Sep-11"; NSDateFormatter *dateFormatter = [