nsdatecomponents

In Objective-C, to get the current hour and minute as integers, we need to use NSDateComponents and NSCalendar?

落爺英雄遲暮 提交于 2019-12-06 03:54:59
问题 I'd like to get the current hour and minute as integers. So if right now is 3:16am, I'd like to get the two integers: 3 and 16. But it looks like [NSDate date] will give the number of seconds since 1970, or it can give a string of the current time representation, but there is no easy way to get them as integers? I see a post in Getting current time, but it involved NSDateComponents and NSCalendar ? That's way too complicated... all that was need is something like NSDate *date = [NSDate date];

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

丶灬走出姿态 提交于 2019-12-06 03:05:29
问题 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 |

NSDateComponents weekday doesn't show correct weekday?

倖福魔咒の 提交于 2019-12-05 23:23:11
I got a NSDate for example 1/6 -12 (Friday) and trying to find out what weekday it is. My week starts at Monday so Friday should be weekday 5. NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setFirstWeekday:2]; NSDateComponents *components = [calendar components:NSWeekdayCalendarUnit fromDate:firstOfJulyDate]; NSLog(@"weekday %i",components.weekday); Output: weekday 6 Doesn't matter what i set setFirstWeekday to, output will always be 6. What am i doing wrong? Try doing something like this: NSCalendar *gregorian = [[[NSCalendar alloc]

sethours in NSDateComponents

只谈情不闲聊 提交于 2019-12-05 20:26:23
I'm trying to sethours in NSDateComponents, I wrote the following code NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *dateComps = [[NSDateComponents alloc] init]; [dateComps setDay:intDay]; [dateComps setMonth:intMonth]; [dateComps setYear:intYear]; [dateComps setHour:intHoures]; [dateComps setMinute:intMinutes]; NSDate *itemDate; itemDate = [calendar dateFromComponents:dateComps]; NSLog(@"reminder date: %@", itemDate); but when I set the hours as 13, it sets it as 11. I want hours in 24 style. Anyone can help me in this issue?

How do I subtract a duration from an NSDate, but not include the weekends?

爷,独闯天下 提交于 2019-12-05 08:15:54
Using today as an example, how do I determine which date it was, 230 workdays ago? I know how to do it iteratively with a while loop checking date and subtracting 1 if it's a workday, but I'm wondering if there is a better method. Also, let's take a Sunday 1 PM as an example, and subtract 3 work days and 2 hours from that time. First, it doesn't make sense to subtract work-time from weekends. So it would have to move the time to 23:59:59 of Friday, and then subtract those 3 days and 2 hours. If it's a Monday at 1:30 AM, and I'm subtracting 5 days and 3 work-hours from that time, then the

How can I format two NSDate as a time difference string (e.g. 2days ago) and make it follow the regional formatting of the device?

无人久伴 提交于 2019-12-05 04:55:31
问题 Does the standard framework support time difference formatting and create a format that follows the regional settings? I know I can break it to NSDateComponents but then I will have to append the text and create different language support files myself. I'm wondering that there may be a way to formatting the date and make it follows the regional setting simple and similar to this... dateFormat = [[[NSDateFormatter alloc] init] autorelease]; [dateFormat setDateStyle:NSDateFormatterMediumStyle]

How to manage NSDate for different timezones

柔情痞子 提交于 2019-12-04 16:50:17
I have created a calendar in my app, using the date object this way: NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSMinuteCalendarUnit)fromDate:[NSDate date]]; NSInteger day = [weekdayComponents day]; NSInteger month = [weekdayComponents month]; NSInteger year = [weekdayComponents year]; m_dateFormatter.dateFormat = @"dd/MM/yyyy"; [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; NSDateComponents

How to get dates of current month in Objective c?

余生颓废 提交于 2019-12-04 14:46:35
I am developing a app which uses the date, month, year. I want the current month dates,because current month can have 28,29,30,31 days. I have tried to get current year months. but i don't know the code for above. here is my code. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSDate *today = [NSDate date]; NSCalendar *currentCalendar = [NSCalendar currentCalendar]; NSDateComponents *yearComponents = [currentCalendar components:NSYearCalendarUnit fromDate:today]; int currentYear = [yearComponents year]; for(int months = 0; months < 12; months++) { NSString *monthName =

In Objective-C, to get the current hour and minute as integers, we need to use NSDateComponents and NSCalendar?

六月ゝ 毕业季﹏ 提交于 2019-12-04 07:05:40
I'd like to get the current hour and minute as integers. So if right now is 3:16am, I'd like to get the two integers: 3 and 16. But it looks like [NSDate date] will give the number of seconds since 1970, or it can give a string of the current time representation, but there is no easy way to get them as integers? I see a post in Getting current time , but it involved NSDateComponents and NSCalendar ? That's way too complicated... all that was need is something like NSDate *date = [NSDate date]; int hour = [date getHour]; // which is not possible Is there a simpler way than using 3 classes

NSCalendar NSDateComponents weekofYear return 1 with date 2014-12-31

隐身守侯 提交于 2019-12-04 06:38:42
问题 I want to get weekofYear with date '2014-12-31' ,but it always return 1 not 52 here is my code: NSCalendar *calendar = [NSCalendar currentCalendar]; NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit; NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:[NSDate date]]; NSlog(@"%i",dateComponent