nscalendar

How can I set an NSDate object to midnight?

怎甘沉沦 提交于 2019-11-26 12:39:37
问题 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

How to determine if an NSDate is today?

风流意气都作罢 提交于 2019-11-26 09:15:25
问题 How to check if an NSDate belongs to today? I used to check it using first 10 characters from [aDate description] . [[aDate description] substringToIndex:10] returns string like \"YYYY-MM-DD\" so I compared the string with the string returned by [[[NSDate date] description] substringToIndex:10] . Is there more fast and/or neat way to check? Thanks. 回答1: In macOS 10.9+ & iOS 8+, there's a method on NSCalendar/Calendar that does exactly this! - (BOOL)isDateInToday:(NSDate *)date So you'd simply

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

妖精的绣舞 提交于 2019-11-26 09:09:46
问题 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

first and last day of the current month in swift

落花浮王杯 提交于 2019-11-26 06:28:13
问题 I\'m trying to get the first and last day of the month in swift. So far I have the following: let dateFormatter = NSDateFormatter() let date = NSDate() dateFormatter.dateFormat = \"yyyy-MM-dd\" let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Year, .Month, .Day, .Hour, .Minute, .Second], fromDate: date) let month = components.month let year = components.year let startOfMonth = (\"\\(year)-\\(month)-01\") But I\'m not sure how to get the last date. Is there a

Get day of week using NSDate

扶醉桌前 提交于 2019-11-26 06:27:13
问题 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

Swift 2.0 calendar components error

廉价感情. 提交于 2019-11-26 05:35:56
问题 Hi I get this error in swift 2.0 Cannot invoke \'components\' with an argument list of type \'(NSCalendarUnit, fromDate: NSDate?, toDate: NSDate?, options: nil)\' var hoy = dataFormatter.stringFromDate(NSDate()) var despues = dataFormatter.stringFromDate(fecha) var calHoy = dataFormatter.dateFromString(hoy) var calcDesp = dataFormatter.dateFromString(despues) let cal = NSCalendar.currentCalendar() let unit:NSCalendarUnit = .Day let components = cal.components(unit, fromDate: calcDesp, toDate:

UILocalNotification Repeat Interval for Custom Alarm (sun, mon, tue, wed, thu, fri, sat)

北城以北 提交于 2019-11-26 04:44:00
问题 I\'m using UILocalNotification for Alarm Purpose. I have a custom option for repeat based on weekdays (sun, mon, tue, wed, thu, fri, sat). So many applications did this process. I tried My level best. But I can\'t get it to work. Please you guys help me.... 回答1: You cannot set custom repeat intervals with UILocalNotification. This has been asked before (see below) but only limited options are provided. The repeatInterval parameter is an enum type and it limited to specific values. You cannot

Differences in NSDateComponents syntax?

南楼画角 提交于 2019-11-26 04:28:31
问题 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

NSDate get year/month/day

给你一囗甜甜゛ 提交于 2019-11-26 01:10:09
问题 How can I get the year/month/day of a NSDate object, given no other information? I realize that I could probably do this with something similar to this: NSCalendar *cal = [[NSCalendar alloc] init]; NSDateComponents *components = [cal components:0 fromDate:date]; int year = [components year]; int month = [components month]; int day = [components day]; But that seems to be a whole lot of hassle for something as simple as getting a NSDate \'s year/month/day. Is there any other solution? 回答1: