nscalendar

WeekOfYear overflowing to 1 in 53rd week in gregorian calendar?

别说谁变了你拦得住时间么 提交于 2021-02-10 23:31:20
问题 The issue I have is quite clear. This year, the 28th to 31st of december 2014 fall to 53rd week of 2014. For some reason, NSCalendar is generating the wrong value when I extract the components from the date. Why? Am I missing something? NSDateComponents *mockTodayComponents = [[NSDateComponents alloc] init]; mockTodayComponents.day = 27; mockTodayComponents.month = 12; mockTodayComponents.year = 2014; NSDate *mockToday = [[NSCalendar currentCalendar] dateFromComponents:mockTodayComponents]; /

日历 NSCalendar

試著忘記壹切 提交于 2020-04-17 04:24:01
【推荐阅读】微服务还能火多久?>>> #import "ViewController.h" @interface ViewController () @property ( strong , nonatomic ) NSCalendar *calender1; @property ( strong , nonatomic ) NSCalendar *calender2; @end @implementation ViewController - ( void )viewDidLoad { [ super viewDidLoad ]; // Do any additional setup after loading the view, typically from a nib. } - ( IBAction )tapBtn1:( id )sender { self . calender1 = [ NSCalendar currentCalendar ]; self . calender2 = [ NSCalendar autoupdatingCurrentCalendar ]; NSLog ( @"%@" , self . calender1 . calendarIdentifier ); NSLog ( @"%@" , self . calender2 .

How to get the 'n' weekday of a Date

不羁岁月 提交于 2020-01-30 11:48:11
问题 I need to translate this function into swift. Basically what does it get the 'n' day of the current week. So for example if i use it with NSDate().getWeekDay(0) it gives me Sun 11 Sept, and so on. But seems rangeOfUnit no longer exists in Swift-3. This was my previous implementation in Swift-2 extension NSDate { func getWeekDay(day: Int) -> NSDate { var beginningOfWeek: NSDate? NSCalendar.currentCalendar().rangeOfUnit(NSCalendarUnit.WeekOfYear, startDate: &beginningOfWeek, interval: nil,

How to get the 'n' weekday of a Date

时光总嘲笑我的痴心妄想 提交于 2020-01-30 11:47:25
问题 I need to translate this function into swift. Basically what does it get the 'n' day of the current week. So for example if i use it with NSDate().getWeekDay(0) it gives me Sun 11 Sept, and so on. But seems rangeOfUnit no longer exists in Swift-3. This was my previous implementation in Swift-2 extension NSDate { func getWeekDay(day: Int) -> NSDate { var beginningOfWeek: NSDate? NSCalendar.currentCalendar().rangeOfUnit(NSCalendarUnit.WeekOfYear, startDate: &beginningOfWeek, interval: nil,

How to get the 'n' weekday of a Date

依然范特西╮ 提交于 2020-01-30 11:47:10
问题 I need to translate this function into swift. Basically what does it get the 'n' day of the current week. So for example if i use it with NSDate().getWeekDay(0) it gives me Sun 11 Sept, and so on. But seems rangeOfUnit no longer exists in Swift-3. This was my previous implementation in Swift-2 extension NSDate { func getWeekDay(day: Int) -> NSDate { var beginningOfWeek: NSDate? NSCalendar.currentCalendar().rangeOfUnit(NSCalendarUnit.WeekOfYear, startDate: &beginningOfWeek, interval: nil,

How to get the day based on a time field (seconds since midnight 1970)?

◇◆丶佛笑我妖孽 提交于 2020-01-11 13:16:17
问题 I'm grabbing data from an api, and one of the values I'm getting is for day of the week, the data returned from api looks like this: "time": 1550376000 I created this function to get the date: func getDate(value: Int) -> String { let date = Calendar.current.date(byAdding: .day, value: value, to: Date()) let dateFormatter = DateFormatter() dateFormatter.dateFormat = "E" return dateFormatter.string(from: date!) } but was told there is a much safer way to get it instead of assuming we get

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,

Calculating StartDate and endDate of a day based on todays date. Swift 2

拜拜、爱过 提交于 2020-01-07 02:35:30
问题 I have the following function which i am using to calculate a start and end of todays date. It was working perfectly last week, i think it may have something to do with the clocks going forwards? func rangeOfPeriod(period: NSCalendarUnit, date: NSDate) -> (NSDate, NSDate) { let calendar = NSCalendar.currentCalendar() var startDate: NSDate? = nil var duration: NSTimeInterval = 0 calendar.rangeOfUnit(period, startDate: &startDate, interval: &duration, forDate: date) let endDate = startDate!

Checking when a date has passed - Swift

拈花ヽ惹草 提交于 2020-01-04 05:46:08
问题 Well, the title pretty much says it all. What I am trying to do is check when a date has passed. So, for example let us say that a user is using my app and then they go to bed and check my app in the morning. When my app opens up I need to check if the day has changed at all. Also I don't really need to know this information when the app is terminated or in the background or anything. I just need to know if the date has changed when the app is running and the user is actually interacting with