nscalendar

Swift - Check if date is in next week / month. ( isDateInNextWeek() isDateInNextMonth() )

喜你入骨 提交于 2020-01-01 19:33:50
问题 We have those convenient functions in calendar: let calendar = NSCalendar.currentCalendar() calendar.isDateInToday(date) calendar.isDateInTomorrow(date) But I am missing those two: calendar.isDateInNextWeek(date) calendar.isDateInNextMonth(date) As @Rob mentioned, the meaning is: "in the calendar week starting this coming Sunday, going through the following Saturday" I having a hard time figuring out how to implement those functions in a robust way that covers all the corner cases. Can

When does dateByAddingComponents:toDate:options return nil?

偶尔善良 提交于 2020-01-01 02:51:08
问题 I've been using dateByAddingComponents:toDate:options: and dateByAddingUnit:value:toDate:options: and using optional binding to get the date out of it. Like this: guard let startOfNextMonth = calendar.dateByAddingComponents(oneMonthComponent, toDate: startOfThisMonth, options: []) else { return nil } Where oneMonthComponent is just an NSDateComponent with the month value set to 1. When I read about it, in the documentation they both say something like: Returns nil if date falls outside the

Calendar.current.date(bySettingHour takes so long to compile

China☆狼群 提交于 2019-12-31 08:00:07
问题 I have used Calendar.current.date(bySettingHour code for setting particular date. Problem is it takes so long to compile ~4 seconds print("Time seconds ",Date().timeIntervalSince1970) for i in 0..<9999 { let nowDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date())! } print("Time seconds ",Date().timeIntervalSince1970)// For loop took 4 seconds Is there any way to reduce compile time ? 回答1: You can't test performance in a Playground, and most of all not compile

Swift - check if a timestamp is yesterday, today, tomorrow, or X days ago

坚强是说给别人听的谎言 提交于 2019-12-29 10:21:31
问题 I'm trying to work out how to decide if a given timestamp occurs today, or +1 / -1 days. Essentially, I'd like to do something like this (Pseudocode) IF days_from_today(timestamp) == -1 RETURN 'Yesterday' ELSE IF days_from_today(timestamp) == 0 RETURN 'Today' ELSE IF days_from_today(timestamp) == 1 RETURN 'Tomorrow' ELSE IF days_from_today(timestamp) < 1 RETURN days_from_today(timestamp) + ' days ago' ELSE RETURN 'In ' + days_from_today(timestamp) + ' ago' Crucially though, it needs to be in

Why NSCalendar's dateFromComponents returns wrong date?

柔情痞子 提交于 2019-12-29 01:38:34
问题 I am expecting 2010-10-11 00:00:00 But instead I receive 2010-10-10 21:00:00 +0000 I know, that I can add a few hours and receive desired date, but I do not understand why I get previous day with 21 hours... Please, help! NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:2010]; [comps setMonth:10]; [comps setDay:11]; [comps setHour:0]; [comps setMinute:0]; [comps setSecond:0]; NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDate

Issues with stringWithFormat

风流意气都作罢 提交于 2019-12-25 18:44:18
问题 Please how do I make my stringWithFormat/initWithFormat to display am/pm instead of 24hrs, even though users selects am/pm from the pickerviewer? When a user select time; eg 4:15 pm, instead of it to display 4:15pm as confirmation, it displays 16:15 instead. I want it as 4:15pm. PLEASE HELP!! -(IBAction)datePickerValueChanged:(id)sender { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

Convert NSDates from one calendar to another

混江龙づ霸主 提交于 2019-12-25 04:02:59
问题 There are lots of questions with the same title but none has answered my question. I want to be able to convert a NSString that represents Islamic calendar to a NSDate that displays that date in Gregorian calendar. This is the code that i have but it still returns the NSDate in Islamic Calendar. - (NSDate*) convertToNSDateFromString : (NSString *)dateString : (NSString *)dateFormat { //Format the Calendar NSDateFormatter *f = [[NSDateFormatter alloc] init]; NSCalendar *gregorian = [

Convert NSDates from one calendar to another

三世轮回 提交于 2019-12-25 04:01:35
问题 There are lots of questions with the same title but none has answered my question. I want to be able to convert a NSString that represents Islamic calendar to a NSDate that displays that date in Gregorian calendar. This is the code that i have but it still returns the NSDate in Islamic Calendar. - (NSDate*) convertToNSDateFromString : (NSString *)dateString : (NSString *)dateFormat { //Format the Calendar NSDateFormatter *f = [[NSDateFormatter alloc] init]; NSCalendar *gregorian = [

How can I find out whether an NSDate is a business day?

耗尽温柔 提交于 2019-12-24 01:51:04
问题 How can I find out whether an NSDate is a business day? That is to say, whether or not it is a weekend according to the user's current locale and calendar settings - so not hardcoded to just be Monday to Friday? NSCalendar has a firstWeekday property, but that just seems to be a presentational thing; it's Sunday in the US and Monday in the UK. EDIT: I'm not worried about holidays, I just want to be able to shade the weekends of a calendar in the same way as the built-in calendar app. 回答1:

Creating NSDate from components without “timezone correction”

假装没事ソ 提交于 2019-12-23 07:00:30
问题 I have a function func createDate(day: Int, month: Int, year: Int) -> NSDate { var comp = NSDateComponents() comp.day = day comp.month = month comp.year = year var cal = NSCalendar.currentCalendar() if var date = cal.dateFromComponents(comp) { return date } return NSDate() } If I call it with createDate(07, 01, 1984) the output is 1984-01-06 23:00:00 +0000 . I assume that there is some influence from the time zone. How can I adjust my code so that the output will be 1984-01-07 00:00:00 +0000