nsdatecomponents

NSDateComponentsFormatter's stringFromDate(_, toDate:) returns nil

天涯浪子 提交于 2019-12-04 06:12:15
Question Why is string nil? let formatter = NSDateComponentsFormatter() let referenceDate = NSDate(timeIntervalSinceReferenceDate: 0) let intervalDate = NSDate(timeInterval: 3628810, sinceDate: referenceDate) let string = formatter.stringFromDate(referenceDate, toDate: intervalDate) I'm expecting a string like "6w 10s" to be returned. (6 weeks is 3,628,800 seconds.) Attempted Troubleshooting To troubleshoot, I tried setting allowedUnits : formatter.allowedUnits = .YearCalendarUnit | .MonthCalendarUnit | .WeekCalendarUnit | .DayCalendarUnit | .HourCalendarUnit | .MinuteCalendarUnit |

How to add a number of weeks to an NSDate?

ⅰ亾dé卋堺 提交于 2019-12-04 03:11:03
问题 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. 回答1: Update: As Zaph said in his

How do I implement previous/next month buttons and show dates for current month?

耗尽温柔 提交于 2019-12-04 02:35:34
问题 Scenario: I have an expense tracking iOS Application and I am storing expenses from a expense detail view controller into a table view (with fetched results controller) that shows the list of expenses along with the category and amount and date. I do have a date attribute in my entity "Money" which is a parent entity for either an expense or an income. Question: What I want is to basically categorize my expenses on a monthly basis and display it as the section header title for example : (Nov

How to create a date object for tomorrow at 8am

坚强是说给别人听的谎言 提交于 2019-12-03 21:40:45
I am normally pretty good with this, but I am having trouble with the NSDate object. I need a NSDate object set for tomorrow at 8am (relatively). How would I do this and what is the simplest method? John Sauer Here's how WWDC 2011 session 117 - Performing Calendar Calculations taught me: NSDate* now = [NSDate date] ; NSDateComponents* tomorrowComponents = [NSDateComponents new] ; tomorrowComponents.day = 1 ; NSCalendar* calendar = [NSCalendar currentCalendar] ; NSDate* tomorrow = [calendar dateByAddingComponents:tomorrowComponents toDate:now options:0] ; NSDateComponents*

Working with NSDate components in Swift

我们两清 提交于 2019-12-03 05:58:31
I have a date that I need to split in some components. for example let components = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil) var dateToPrint = "\(date.day) days \(date.hour) hours" dateToPrint will be the number of days and hours from aDate to now. But if i want the number of weeks instead of days let components = NSCalendarUnit.CalendarUnitWeek | NSCalendarUnit.CalendarUnitHour let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil) var

Where is the extra 75 seconds coming from?

北战南征 提交于 2019-12-03 00:55:53
While writing some unit tests on a Julian Day calculator, I found that dates prior to 2nd December 1847 were being initialised incorrectly by NSDate. They appear to have 75 seconds added on. I haven't been able to find anything pointing to that date (which is well after the Gregorian calendar cutoff). Is it a bug or is there a historic calendar adjustment that I've not come across? int main(int argc, const char * argv[]) { @autoreleasepool { NSCalendar *cal = [NSCalendar currentCalendar]; NSDateComponents *dateComps = [NSDateComponents new]; dateComps.year = 1847; dateComps.month = 12;

How can I figure out the day difference in the following example

别来无恙 提交于 2019-12-02 16:31:27
问题 I am calculating the day difference between two dates, but I figured out the the following code is actually giving me the difference for 24 hours rather than the difference in date. I have the following code: func daysBetweenDate(startDate: NSDate, endDate: NSDate) -> Int { let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Day], fromDate:startDate, toDate: endDate, options: []) return components.day } So, for the following example I get this result: lastLaunch

Weird behavior when creating an NSDate of January the 1st and the 2nd

会有一股神秘感。 提交于 2019-12-02 09:58:47
问题 I'm creating a date like this : NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY"]; NSInteger year = [[dateFormatter stringFromDate:[NSDate date]] intValue]; NSLog(@"NSInteger YEAR : %d\n", year); //minutes, hours, day and month are initialized elsewhere... NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:year]; [comps

How can I figure out the day difference in the following example

非 Y 不嫁゛ 提交于 2019-12-02 08:40:41
I am calculating the day difference between two dates, but I figured out the the following code is actually giving me the difference for 24 hours rather than the difference in date. I have the following code: func daysBetweenDate(startDate: NSDate, endDate: NSDate) -> Int { let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Day], fromDate:startDate, toDate: endDate, options: []) return components.day } So, for the following example I get this result: lastLaunch:2016-06-10 01:39:07 +0000 toady: 2016-06-11 00:41:41 +0000 dayDiff:0 I would have expected the day

Weird behavior when creating an NSDate of January the 1st and the 2nd

蓝咒 提交于 2019-12-02 07:50:55
I'm creating a date like this : NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY"]; NSInteger year = [[dateFormatter stringFromDate:[NSDate date]] intValue]; NSLog(@"NSInteger YEAR : %d\n", year); //minutes, hours, day and month are initialized elsewhere... NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:year]; [comps setMonth:month]; [comps setDay:day]; [comps setHour:hours]; [comps setMinute:minutes]; NSDate *sampleDate =