nscalendar

Creating a new NSCalendar calendar

穿精又带淫゛_ 提交于 2019-12-11 01:35:23
问题 I want to be able to convert between the Gregorian and Julian calendars (note: not "julian date") using NSCalendar on the iPhone. How can I subclass NSCalendar to use the Julian calendar? I want to do this without regards for 1582 or 1752 or any given year. 回答1: While you can certainly have a go at it, but I suspect that NSCalendar is tricky to subclass. It's toll-free bridged, which complicates things; is entwined with NSLocale, which complicates things; it's initialized with identifiers

How can I use NSCalendar range function within Calendar?

不问归期 提交于 2019-12-11 00:59:11
问题 I have the following code which used compile in Swift 2 however won't in Swift 4.2. The range function that returns a boolean is no longer a part of the Calendar data type however it is a part of the NSCalendar data type. Is there a way I can use or format this function to make it compile in Swift 4.2? extension Calendar { /** Returns a tuple containing the start and end dates for the week that the specified date falls in. */ func weekDatesForDate(date: NSDate) -> (start: NSDate, end: NSDate)

Objective-C/iOS: Get date for specific day in week (sunday)

时间秒杀一切 提交于 2019-12-10 19:48:34
问题 I need to get the date of a specific day (Sunday) in the current week. I use this date to determine if some weekly resetting should occur, and I do some calculations on last reset date is before sundays date etc. This has in principal been ask and answered many times, and my solution has worked for the past 6 months. But today, the 29th of December 2013, it stopped working. The function used at present time: + (NSDate *) getSundaysDate{ NSDate *currentDate = [NSDate date]; NSCalendar

Apple Swift: how to get current seconds in 1/100 or 1/1000?

青春壹個敷衍的年華 提交于 2019-12-10 15:34:59
问题 func updateTime() { var date = NSDate() var calendar = NSCalendar.currentCalendar() var components = calendar.components(.CalendarUnitSecond, fromDate: date) var hour = components.hour var minutes = components.minute var seconds = components.second counterLabel.text = "\(seconds)" var myIndicator = counterLabel.text?.toInt() if myIndicator! % 2 == 0 { // do this } else { // do that } } I'd like to know how I can change this code so I get 1/10 or 1/100 or 1/1000 of a second to display in

NSCalendar in Swift - init can return nil, but isn't optional

对着背影说爱祢 提交于 2019-12-10 13:56:37
问题 NSCalendar(calendarIdentifier: calendarName) can return nil if calendarName is not valid - this is the original Objective-C behaviour, and is also true in Swift. However, it appears that the compiler believes the initializer returns an NSCalendar rather than an NSCalendar? , as follows: let c1 = NSCalendar(calendarIdentifier: "gregorian")// _NSCopyOnWriteCalendarWrapper let c2 = NSCalendar(calendarIdentifier: "buddhist")// _NSCopyOnWriteCalendarWrapper //let c3:NSCalendar = NSCalendar

Get calendarweek of an NSDate according ISO 8601

♀尐吖头ヾ 提交于 2019-12-10 12:16:38
问题 is there a way to get calendarweek of an NSDate according ISO 8601. I think: Datecomponents *dateComponents = [cal components:(NSWeekOfYearCalendarUnit ) fromDate: testDate ]; int calandarWeek = [dateComponents weekOfYear]; uses a different algorithm. ISO definition: CW1 is the first week in the year containing 4 day, CW start on Mondays 回答1: I found out. It´s possible to configure the NSCalender: [cal setFirstWeekday:2]; //week starts on Monday [cal setMinimumDaysInFirstWeek:4]; //first week

Convert gregorian date to Hijri date

≯℡__Kan透↙ 提交于 2019-12-10 11:41:19
问题 The source code comes from: Hijri (islamic) calendar in swift التاريخ الهجري How can I convert string date to NSDate? I just put them together but it has some mistake with the output. let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "YYYY-MM-DD" let GregorianDate = dateFormatter.dateFromString("\(Yeartext1.text!)-\(Monthtext1.text!)-\(Daytext1.text!)") let islamic = NSCalendar(identifier: NSIslamicCalendar) let components = islamic?.components(NSCalendarUnit(rawValue: UInt.max

Converting NSDate from a calendar to another

回眸只為那壹抹淺笑 提交于 2019-12-10 10:35:22
问题 I'm having trouble here. Took me hours but no use. Please keep in mind I checked the heck of StackOverFlow for a fix but there isn't any. When I convert the _date Object from Gregorian to Hijri it does not convert. I know that because when I print it it still gives me the Gregorian day. This troubles me because I want to setDate of a UIDatePicker and it's setting it incorrectly because the _date is gregorian and my UIDatePicker object calendar set to setCalendar:_hijriCalendar //DATE SETTING

How to subtract date components?

与世无争的帅哥 提交于 2019-12-10 01:23:33
问题 As today is Friday , which is 6 according to NSCalendar . I can get this by using the following Calendar.current.component(.weekday, from: Date()) How do I get weekday component of Saturday last week, which should be 7 ? If I do Calendar.current.component(.weekday, from: Date()) - 6 . I am getting 0 which is not valid component. 回答1: Try this, you have to get the date first then subtract again from it: var dayComp = DateComponents(day: -6) let date = Calendar.current.date(byAdding: dayComp,

Swift displaying the time or date based on timestamp

喜欢而已 提交于 2019-12-08 07:58:17
问题 I have an API that returns data including a timestamp for that record. In swift I have the timestamp element loaded and converted into a double and can then convert that into time. I want to be able to return the time if the date of the record is today and return the date if the record is not today. See below: let unixTimeString:Double = Double(rowData["Timestamp"] as! String)! let date = NSDate(timeIntervalSince1970: unixTimeString) // This is on EST time and has not yet been localised. var