nsdatecomponents

Adjusting a date from an API

不羁的心 提交于 2019-12-13 04:22:44
问题 I've got an API I'm hitting that returns a date that's at midnight on the East Coast of the U.S. That date, in turn, is used by a computed var to return a localized string. The issue I'm encountering is anyone who's one or more time zones behind the Eastern time will get the day prior from the date object returned from the API. Here's what I've come up with to address this. It feels hacky, so I was hoping to see if there's a better way to get a date object in the non-Eastern time zone that

In NSDateComponents, how do you know if it's AM or PM?

坚强是说给别人听的谎言 提交于 2019-12-12 10:36:53
问题 When I use NSDateComponents, how do I know I'm setting a time as Am or Pm? Or is it 24-hour format? Here's the code: NSDate *morningEnd = [NSDate date]; NSDateComponents *components1 = [gregorian components:NSUIntegerMax fromDate:morningEnd]; [components1 setHour:11]; [components1 setMinute:59]; How do I know if I'm setting it as 11:59 AM or 11:59 PM? 回答1: The hour is defined out of 24 hours. 0 - 11 will be AM, 12 - 23 will be PM. Edit: Corrected my range to be 0 - 23 as per the comments

getting hour component from an NSDate

自古美人都是妖i 提交于 2019-12-12 05:33:23
问题 I'm trying to get the hour component from an NSDate . Here is what I do: NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+1"]]; NSDateComponents *startComponents = [calendar components:DATE_COMPONENTS fromDate:_start]; startComponents.timeZone = [NSTimeZone timeZoneWithName:@"GMT+1"]; int hours = [startComponents hour]; Now before yesterday the hours value always was one hour ahead. But since

NSDateComponentFormatter Print 1 for NSTimeInvervals <= 60

别等时光非礼了梦想. 提交于 2019-12-12 01:54:48
问题 I'm trying to have create a timer application and utilize iOS8's NSDateComponentsFormatter. How can I output a properly-formatted string for NSTimeInterval values between 0 and 60? As seen in the Swift playground, it seems to be returning a value of 1. I'd like to return a string with a format seen on NSTimeIntervals > 60, "0:12". Note: I know that a I can manually take the time in seconds and calculate hour, minutes, etc. but that would not be using the NSDateComponentsFormatter. //

Get Month and year of 1 year advance in iPhone

我怕爱的太早我们不能终老 提交于 2019-12-11 18:35:46
问题 in my app I need to get month name and year for next 1 year and store in array. I mean to say suppose today's September 2012, I need month name and year till August 2013. Can you please tell me how will I get month and year? Thanx in advance 回答1: Use the NSMonthCalendarUnit for the components parameter of the current calendar to get the number of the current month, e.g. 9 for September and then NSYearCalendarUnit for the current year, e.g. 2012. Then use these in a for loop which uses modulus

Group NSDate in weeks and order from newest to oldest

断了今生、忘了曾经 提交于 2019-12-11 10:04:53
问题 i'm developing an app for iPhone and I'm stuck to this point: i get this data from an object: { date = "2014-08-04 11:21:26 +0000"; idChallenger = "-1"; index = "-1"; time = "-1"; }, { date = "2015-07-31 14:50:40 +0000"; idChallenger = 43; index = "-1"; time = "-1"; }, { date = "2015-07-31 16:18:57 +0000"; idChallenger = "-1"; index = "-1"; time = "-1"; }, { date = "2015-07-31 16:19:29 +0000"; idChallenger = "-1"; index = "-1"; time = "-1"; }, { date = "2015-08-04 15:25:26 +0000";

Set the first of the month of a NSDate

懵懂的女人 提交于 2019-12-11 05:29:17
问题 I'm using a NSDate that is incremented/decremented thanks to NSDateCompoments: dateComponents = [[NSDateComponents alloc] init]; self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0]; ... [dateComponents setDay:1]; self.date = [gregorian dateByAddingComponents:dateComponents toDate:self.date options:0]; [dateComponents setDay:0]; Now, I want to set the day to set the NSDate to the first of the month (and the first month of the year too), but I dont know

Swift DateComponentsFormatter drop leading zeroes but keep at least one digit in Minutes place

℡╲_俬逩灬. 提交于 2019-12-11 04:47:52
问题 I am using the following DateComponentsFormatter in Swift: let formatter = DateComponentsFormatter() formatter.unitsStyle = .positional formatter.allowedUnits = [.hour, .minute, .second] formatter.zeroFormattingBehavior = [.default] This produces results like 2:41 (for 2 minutes and 41 seconds) and 08 (for 8 seconds). I would, however, like to keep at least one digit in the Minutes place, returning 0:08 instead of 08. Is this possible with the DateComponentsFormatter? I would prefer a

Error combining NSCalendarUnit with OR (pipe) in Swift 2.0

末鹿安然 提交于 2019-12-11 03:51:21
问题 I have some code that's breaking in Swift 2.0: let formatter = NSDateComponentsFormatter() formatter.allowedUnits = NSCalendarUnit.Year formatter.allowedUnits |= .Month formatter.allowedUnits |= .WeekOfMonth formatter.allowedUnits |= .Day formatter.allowedUnits |= .Hour formatter.allowedUnits |= .Minute I get the error Binary operator '|=' cannot be applied to 'NSCalenderUnit' operands . What's the new way of doing this kinda thing? 回答1: NSCalendarUnit is an OptionSetType in Swift 2, instead

NSCalendar dateFromComponents: vs NSDateComponents date

三世轮回 提交于 2019-12-11 01:37:06
问题 Say I have the following code which initializes an NSDateComponents: NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [[NSDateComponents alloc] init]; [components setYear:1987]; [components setMonth:3]; [components setDay:17]; [components setHour:14]; [components setMinute:20]; [components setSecond:0]; And now I want to convert the components to an NSDate. It seems like there are two ways: Method 1: NSDate *date = [calendar dateFromComponents:components];