nscalendar

Number of days in the current month using iOS?

半腔热情 提交于 2019-11-27 04:21:33
问题 How can I get the current number of days in the current month using NSDate or something similar in Cocoa touch? 回答1: 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.

How can I set an NSDate object to midnight?

霸气de小男生 提交于 2019-11-27 03:37:26
I have an NSDate object and I want to set it to an arbitrary time (say, midnight) so that I can use the timeIntervalSince1970 function to retrieve data consistently without worrying about the time when the object is created. I've tried using an NSCalendar and modifying its components by using some Objective-C methods, like this: let date: NSDate = NSDate() let cal: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)! let components: NSDateComponents = cal.components(NSCalendarUnit./* a unit of time */CalendarUnit, fromDate: date) let newDate: NSDate = cal.dateFromComponents

How to get Monday's date of the current week in swift

谁都会走 提交于 2019-11-27 01:37:31
I'm trying to get Monday's date of the current week. This is treated as the first day of the week in my table view. I also need to get Sunday's of the current week. This is treated as the last day of the week in my table view. Current attempt: let date = NSDate() let calendar = NSCalendar.currentCalendar() calendar.firstWeekday = 1 //attempt to changefirstday let dateFormatter = NSDateFormatter() let theDateFormat = NSDateFormatterStyle.ShortStyle let theTimeFormat = NSDateFormatterStyle.ShortStyle dateFormatter.dateStyle = theDateFormat dateFormatter.timeStyle = theTimeFormat let

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

Swift - Get date from day of year

大城市里の小女人 提交于 2019-11-26 23:37:22
问题 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? 回答1: 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 =

Get day of week using NSDate

北城余情 提交于 2019-11-26 19:42:09
I created a method that is supposed to take in a string in "YYYY-MM-DD" form and spit out an int that represents the dates position in relation to the week it is in (regardless if it overlaps between months). So e.g sunday=1 monday=2 and so on. Here is my code: func getDayOfWeek(today:String)->Int{ var formatter:NSDateFormatter = NSDateFormatter() formatter.dateFormat = "YYYY-MM-DD" var todayDate:NSDate = formatter.dateFromString(today)! var myCalendar:NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar) var myComponents = myCalendar.components(NSCalendarUnit

I want to get a quarter value in NSDateComponents class

房东的猫 提交于 2019-11-26 18:34:38
问题 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

I want to get a quarter value in NSDateComponents class

谁说我不能喝 提交于 2019-11-26 17:26:05
问题 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

Differences in NSDateComponents syntax?

雨燕双飞 提交于 2019-11-26 15:32:06
I have been working on a clock app in Swift and with Xcode 6.3.2 the following code builds and runs just fine. // 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 However when I load the same project up in Xcode 7.0 beta and make no changes whatsoever, I get an error on the calendar

All dates between two Date objects (Swift)

∥☆過路亽.° 提交于 2019-11-26 14:39:30
I’m creating a date using NSDateComponents() . let startDate = NSDateComponents() startDate.year = 2015 startDate.month = 9 startDate.day = 1 let calendar = NSCalendar.currentCalendar() let startDateNSDate = calendar.dateFromComponents(startDate)! ... now I want to print all dates since the startDate until today, NSDate() . I’ve already tried playing with NSCalendarUnit , but it only outputs the whole difference, not the single dates between. let unit: NSCalendarUnit = [.Year, .Month, .Day, .Hour, .Minute, .Second] let diff = NSCalendar.currentCalendar().components(unit, fromDate: