nsdatecomponents

NSDateComponents to NSDate - Swift

ぐ巨炮叔叔 提交于 2021-02-18 21:09:19
问题 I have this function : private func getCurrentDateComponent() -> NSDateComponents { let date = NSDate() let calendar = NSCalendar.currentCalendar() let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute | .CalendarUnitMonth | .CalendarUnitYear | .CalendarUnitDay, fromDate: date) return components } When I call it and I use the function date var d = this.getCurrentDateComponent().date I don't know why the variable d is nil... Thank you for your help Ysee 回答1: That's how I

Creating a date with DateComponents

本秂侑毒 提交于 2021-02-04 21:49:18
问题 I want to get the first day and the last day of the week. But my results do not match the documentation from apple: https://developer.apple.com/documentation/foundation/nsdatecomponents/1410442-weekday This is my function: func startAndEndDateOfWeek(weekOfYearWithYear: (week: Int,year: Int)) -> (start: Date, end: Date) { var output = (start: Date.init(), end: Date.init()) let calendar = Calendar(identifier: .gregorian) var firstDayComponents = DateComponents() firstDayComponents.weekOfYear =

NSDateComponents Time Profile

最后都变了- 提交于 2020-01-15 07:40:29
问题 My problem is to do with the Time Profile of NSDateComponents operations given in the code snippet below. I have a method that iterates over a large array of NSDate objects Part of the method requires that I discard the Hour, Minute and seconds of each date. To do this I have 3 steps: I create an NSDateComponents object from my original date. I set the H:M:S elements to 0 I get my new date object that has H:M:S set to 0 as needed The Issue: Steps 1 & 3 above are taking a huge percentage of my

Determine NSDate for Memorial Day in a given year

柔情痞子 提交于 2020-01-13 20:16:47
问题 Is there a better way to determine the NSDate for Memorial Day (the last Monday in May) in a given year? NSInteger aGivenYear = 2013 ; NSCalendar* calendar = [NSCalendar currentCalendar] ; NSDateComponents* firstMondayInJuneComponents = [NSDateComponents new] ; firstMondayInJuneComponents.month = 6 ; // Thanks, Martin R., for pointing out that `weekOfMonth` is wrong for returning the first Monday in June. firstMondayInJuneComponents.weekOfMonth = 1 ; firstMondayInJuneComponents.weekday = 2 ;

All dates between two Date objects (Swift)

情到浓时终转凉″ 提交于 2020-01-08 12:30:27
问题 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,

All dates between two Date objects (Swift)

て烟熏妆下的殇ゞ 提交于 2020-01-08 12:30:20
问题 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,

Finding the locale-dependent first day of the week

天大地大妈咪最大 提交于 2020-01-03 16:45:08
问题 Given an NSDate , how do I find the first day of that date's week, given the user's locale. For example, I've heard that some countries treat Monday as the first day of the week and others use Sunday. I need to return the preceding Monday in the first case but the preceding Sunday in the latter case. My best effort thus far always returns the preceding Sunday, regardless of the device settings applied: NSCalendar *calendar = [NSCalendar currentCalendar]; [calendar setLocale:[NSLocale

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

Why is Calendar.date(from: DateComponents) adding time?

守給你的承諾、 提交于 2019-12-30 11:32:11
问题 I create an instance of DateComponents using the following code: let dateComponents = DateComponents( calendar: .current, timeZone: Calendar.current.timeZone, era: nil, year: nil, month: nil, day: nil, hour: 9, minute: 0, second: 0, nanosecond: 0, weekday: 2, weekdayOrdinal: nil, quarter: nil, weekOfMonth: nil, weekOfYear: nil, yearForWeekOfYear: nil) I then print the dateComponents object and get the following (expected) output: calendar: gregorian (current) timeZone: Europe/London (current)

ios - get in between dates (in actual date, not number of days)

折月煮酒 提交于 2019-12-30 03:37:09
问题 I have two date string and I wanted to get the in between dates. For example, NSString *startDate = @"25-01-2014"; NSString *endDate = @"02-02-2014"; In between dates will be (26-01-2014, 27-01-2014, 28-01-2014.......) preferably include startDate and endDate as well. Most of the question I managed to find asked for number of days. But I needed it to be actual date. Is there anyway that I can get the in between dates? NSString *start = @"2010-09-01"; NSString *end = @"2010-12-05";