nsdate

Will two [NSDate date] following each-other always return different times?

落爺英雄遲暮 提交于 2019-12-04 04:43:03
问题 Will dateA and dateB always be different? // Two [NSDate date] following each other NSDate *dateA = [NSDate date]; // Line X NSDate *dateB = [NSDate date]; // Line X+1 That is, will the line below always return NO? [dateA isEqualToDate:dateB] ("Always" meaning that like a very fast processor wouldn't execute the two commands so fast that dateA and dateB would be assigned the same time with "sub-second" accuracy). I want to have a "unique" timestamp for some internal identification (not DB

NSDate change time zone

社会主义新天地 提交于 2019-12-04 03:58:36
问题 I am writing an app that involves time zone. I want to grab the user's device time, lets call that dateA , and I have a target date that will always be in EDT time zone ( dateB ). I want to get the difference between the 2 dates and show the user, but in dateB 's time zone. eg: user device time is 07:30AM PDT, and dateB is 11:00AM EDT. So the time difference would be 30 minutes. My algorithm is: 1) Get user device time 2) convert to EDT 3) grab the time difference between dateA and dateB My

Determine the number of months between two dates using Cocoa

一世执手 提交于 2019-12-04 03:36:47
How do I calculate the number of months between two dates using Cocoa? Thanks, Stan Look at NSCalendars components:fromDate:toDate:options method. It allows you to subtract dates and then extract the months property value NSInteger month = [[[NSCalendar currentCalendar] components: NSCalendarUnitMonth fromDate: yourFirstDate toDate: yourSecondDate options: 0] month]; To get an answer that includes the fraction of a month the following can be used: - (NSNumber *)numberOfMonthsBetweenFirstDate:(NSDate *)firstDate secondDate:(NSDate *)secondDate { if ([firstDate compare:secondDate] ==

How to retrieve number of hours past midnight from an NSDate object?

爷,独闯天下 提交于 2019-12-04 03:34:20
问题 I need to retrieve the number of hours past midnight from a UIDatePicker control in an iPhone project. datePickerMode is set to UIDatePickerModeTime , so the user only can set a time, no date. When the user is done and dismisses the view the UIDatePicker is on, the following date might be retrieved (as an example): NSDate *returnTime = timePicker.date; NSLog(@"returnTime: %@", returnTime); // returns for example @"1970-01-01 10:13:00 PM +0000" As said, I'm looking for the number of hours past

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

NSDate from Swift 2.3 to Date from Swift 3 Conversion

。_饼干妹妹 提交于 2019-12-04 02:58:45
So I've just updated to Xcode 8 and converted my Swift 2.3 code to Swift 3, and Swift 3 just converted all NSDate to Foundation.Date And I had an extension for NSDate , now called Foundation.Date , it looked like this: extension NSDate { var firstDayOfTheMonth:NSDate { var date: NSDate? NSCalendar.currentCalendar().rangeOfUnit(.Month, startDate:&date , interval: nil, forDate: self) return date! } } And Swift 3 converted it to this: extension Foundation.Date { var firstDayOfTheMonth:Foundation.Date { var date: Foundation.Date? (Calendar.current as NSCalendar).range(of: .month, start:&date ,

Date formatter returns nil for June

非 Y 不嫁゛ 提交于 2019-12-04 02:53:32
I have a strange problem with those lines of code : NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MMyyyy"]; NSDate * date = [df dateFromString:@"062008"]; NSLog(@"Date %@", date); The result is : Date (null) But when I change the month like this : NSDateFormatter * df = [[NSDateFormatter alloc] init]; [df setDateFormat:@"MMyyyy"]; NSDate * date = [df dateFromString:@"072008"]; NSLog(@"Date %@", date); The result is : Date 2008-06-30 23:00:00 +0000 For only the month of June (06), the date conversion from string fails ! I think, it's something related to timezone,

Copying an NSDate (wanting independent objects)

╄→гoц情女王★ 提交于 2019-12-04 02:46:46
NSDate conforms to NSCopying protocol. According to the documentation for NSCopying protocol: a copy must be a functionally independent object with values identical to the original at the time the copy was made. But, when I do this: NSDate *date1 = [NSDate date]; NSDate *date2 = [date1 copy]; NSLog(@"result: date1 0x%x date2 0x%x", (int)date1, (int)date2); // "result: date1 0x2facb0 date2 0x2facb0" The two objects are identical (same object id). What am I missing? How do I get an independent object as a copy? copy does not guarantee different object pointer. “Functionally independent” means

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

NSDateFormatter for BST instead of GMT+1

╄→尐↘猪︶ㄣ 提交于 2019-12-04 02:29:55
问题 I want to display the following string on my time axis: "GMT/BST" Here's the code: NSDateFormatter *dateformatter=[[NSDateFormatter alloc] init]; [dateformatter setDateFormat:@"zzz"]; timeZoneString = [NSMutableString stringWithFormat:@"%@ / %@",[dateformatter stringFromDate:startDate],[dateformatter stringFromDate:endDate]]; But this gives "GMT/GMT+01:00" What is the NSDateFormatter code to turn "GMT+01:00" into "BST" ? I can't get the right formatters to do this, having tried z|zzz|Z|ZZZ|v