nsdatecomponents

Creating NSDate from NSDateComponents off by one day

只愿长相守 提交于 2019-12-02 06:58:31
I'm trying to set a particular date for a unit test: NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setCalendar:calendar]; [components setYear:2011]; [components setMonth:5]; [components setDay:12]; [components setHour:1]; [components setMinute:0]; [components setSecond:0]; NSDate *date = [calendar dateFromComponents:components]; NSLog(@"Date I tried to set: %@", date); However, this prints out "Date I tried to set: 2011-05-11 17:00:00 +0000." The date seems

unexpected result of dateFromComponents:

夙愿已清 提交于 2019-12-02 06:14:21
问题 the following code NSCalendar *cal = [NSCalendar currentCalendar]; [cal setFirstWeekday:2]; [cal setMinimumDaysInFirstWeek:4]; NSDateComponents *comp = [[NSDateComponents alloc] init]; [comp setYear: 2012]; [comp setMonth:3]; [comp setDay: 12]; NSDate *date = [cal dateFromComponents:comp]; NSLog(@"date:%@",date); logs: date:2012-03-11 23:00:00 +0000 Any Idea, why this happen and how to avoid Tnx 回答1: I assume you are using the CET time zone locally. This is what the [NSCalendar

Crash while using Datecomponents having larger differences

旧城冷巷雨未停 提交于 2019-12-02 04:22:14
问题 Crashing for large interval between dates while trying to use date components in swift (for example dates used is 16 August 1979 to 11 may 2053) The code for date components is ComponentsforDetail = (Calendar.current as NSCalendar).components([.second], from: FromDateValue!, to: ToValueDate!, options: []) Using breakpoints i found out the thread_exc happens in an assembly line code libswiftFoundation.dylib`static Foundation.DateComponents._unconditionallyBridgeFromObjectiveC (Swift.Optional<_

NSDateComponents weekOfYear returns wrong value

别来无恙 提交于 2019-12-02 03:38:01
问题 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierISO8601]; [calendar setTimeZone:[NSTimeZone defaultTimeZone]]; [calendar setFirstWeekday:2]; [calendar setMinimumDaysInFirstWeek:1]; NSDateComponents *mockTodayComponents = [[NSDateComponents alloc] init]; mockTodayComponents.day = 28; mockTodayComponents.month = 12; mockTodayComponents.year = 2015; NSDate *date = [calendar dateFromComponents:mockTodayComponents]; NSDateComponents *c = [calendar

Crash while using Datecomponents having larger differences

落花浮王杯 提交于 2019-12-02 01:34:39
Crashing for large interval between dates while trying to use date components in swift (for example dates used is 16 August 1979 to 11 may 2053) The code for date components is ComponentsforDetail = (Calendar.current as NSCalendar).components([.second], from: FromDateValue!, to: ToValueDate!, options: []) Using breakpoints i found out the thread_exc happens in an assembly line code libswiftFoundation.dylib`static Foundation.DateComponents._unconditionallyBridgeFromObjectiveC (Swift.Optional<__ObjC.NSDateComponents>) -> Foundation.DateComponents: 0x16cf280 <+0>: push {r4, r5, r7, lr} 0x16cf284

Get the exact difference between 2 dates for a single NSDateComponent

元气小坏坏 提交于 2019-12-02 00:57:24
问题 How can I get the exact difference (in decimal) between 2 values of NSDate . Eg. Jan 15 2016 to Jul 15 2017 = 1.5 Years . I can use something like: NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: date1, toDate: date1, options: nil).year but this gives me absolute values . i.e. for above example it would give me 1 Year . Is it possible to get exact values correct to at least a few decimal places? 回答1: The terms you've used here are misleading. When you say

Get the exact difference between 2 dates for a single NSDateComponent

岁酱吖の 提交于 2019-12-01 20:47:13
How can I get the exact difference (in decimal) between 2 values of NSDate . Eg. Jan 15 2016 to Jul 15 2017 = 1.5 Years . I can use something like: NSCalendar.currentCalendar().components(NSCalendarUnit.CalendarUnitYear, fromDate: date1, toDate: date1, options: nil).year but this gives me absolute values . i.e. for above example it would give me 1 Year . Is it possible to get exact values correct to at least a few decimal places? The terms you've used here are misleading. When you say "absolute" you mean "integral." And when you say "exact" you mean "within some desired precision." Let's say

How to add a number of weeks to an NSDate?

自闭症网瘾萝莉.ら 提交于 2019-12-01 17:40:38
I have in the past used the below function to add on a specific time interval using NSDateComponents to an existing date. (NSDate *)dateByAddingComponents:(NSDateComponents *)comps toDate:(NSDate *)date options:(NSCalendarOptions)opts From iOS8 the week value is deprecated for NSDateComponents , which means I can't achieve what I want to do: generate a new NSDate by adding a certain number of weeks to a given NSDate . Any help would be greatly appreciated. Update: As Zaph said in his answer, Apple actually recommends using weekOfYear or weekOfMonth instead of the answer I provided. View Zaph's

How to print name of the day of the week?

非 Y 不嫁゛ 提交于 2019-12-01 14:49:21
Im trying to print out the name of the day of the week i.e Monday, Tuesday, Wednesday. I currently have this bit of code that does just that. I was wondering if there is a way to get rid of my switch statement and make this better. Thanks! func getDayOfWeek(_ today: String) -> String? { let formatter = DateFormatter() formatter.dateFormat = "yyyy-MM-dd" guard let todayDate = formatter.date(from: today) else { return nil } let myCalendar = Calendar(identifier: .gregorian) let weekDay = myCalendar.component(.weekday, from: todayDate) switch weekDay { case 1: return "Sunday" case 2: return

How to compute age from NSDate

一个人想着一个人 提交于 2019-12-01 13:31:02
问题 I am working on an app that needs to find someones age depending on their birthday. I have a simple NSDate but how would I find this with NSDateFormatter ? 回答1: - (NSInteger)ageFromBirthday:(NSDate *)birthdate { NSDate *today = [NSDate date]; NSDateComponents *ageComponents = [[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:birthdate toDate:today options:0]; return ageComponents.year; } NSDateFormatter is for formatting dates (duh!). As a rule of thumb, whenever you need