nsdate

Array of all NSDate for a week

感情迁移 提交于 2019-12-23 02:19:28
问题 I would like to have a function that takes a week number as input and returns an array of all the NSDates that this specific week is made by. Something like this: -(NSArray*)allDatesInWeek:(int)weekNumber { NSDate *today = [NSDate date]; NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; [calendar setFirstWeekday:2]; NSDateComponents *todayComp = [calendar components:NSYearCalendarUnit fromDate:today]; int currentyear = todayComp.year; /* Calculate and

NSDate hebrew date label

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 02:18:35
问题 I make label with the date for today but I need hebrew date for today. this my code NSDate *today = [NSDate date]; NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateStyle:NSDateFormatterShortStyle]; NSString *dateString = [dateFormat stringFromDate:today]; [_label setText:dateString]; I not build calendar this label with date for today. Thanks! 回答1: Assuming the user's current locale is not set for Hebrew, then you need to ensure the date formatter's locale is

Getting first and last days of current week

不羁岁月 提交于 2019-12-22 12:28:00
问题 How can I get first and last days of the current week? What I need is some way of filtering a set of objects with NSDate property to leave only ones, that appear on current week, and then filter them by days. Also I need some way to get localized day names. I've tried to play with NSCalendar, NSDate and NSDateComponents, but it seems that I can't just check calendar's firstWeekday, as there might be weeks with days from previous/next month. 回答1: Use NSDateComponents to get the weekday, which

NSDate formatter

随声附和 提交于 2019-12-22 11:14:11
问题 I try to convert a string to NSDATE with no luck unfortunately. Friday, October 22, 11:26:45 ET 2010 I know the options for formatting (http://sree.cc/objective-c/nsdate-format-string-in-objective-c) but i cant get it to work. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"???????"]; anyone? 回答1: Date Format Specifiers. So you'd probably need something like: [dateFormatter setDateFormat:@"eeee, MMMM dd, HH:mm:ss z yyyy"]; eeee - Local day of

NSDateComponents weekday doesn't show correct weekday?

陌路散爱 提交于 2019-12-22 10:29: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.

Sort an array by NSDates using the sort function [duplicate]

你。 提交于 2019-12-22 09:31:48
问题 This question already has answers here : How do I sort a swift array containing instances of NSManagedObject subclass by an attribute value (date) (5 answers) Closed 4 years ago . I have a model class called Event . import Foundation import MapKit public class Event { let id: Int var title: String? let status: String let location: String var description: String? var latitude: CLLocationDegrees? var longitude: CLLocationDegrees? var startDate: NSDate? var endDate: NSDate? init(id: Int,

How I can get the NSDate of yesterday

送分小仙女□ 提交于 2019-12-22 06:48:46
问题 how I can get the NSDate of yesterday in iOS? Thanks 回答1: Here is one way to do that: NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setDay:-1]; NSDate *yesterday = [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:[NSDate date] options:0]; Take a look at the Date and Time programming guide. 来源: https://stackoverflow.com/questions/7197994/how-i-can-get-the-nsdate-of-yesterday

Convert NSDate to String with strftime

让人想犯罪 __ 提交于 2019-12-22 05:42:14
问题 How would I convert an NSDate to a NSString, formatted with strftime specifiers? 回答1: you could use strftime. NSDate *date = [NSDate date]; time_t time = [date timeIntervalSince1970]; struct tm timeStruct; localtime_r(&time, &timeStruct); char buffer[80]; strftime(buffer, 80, "%d.%m.%Y %H:%M:%S", &timeStruct); NSString *dateStr = [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding]; I hope it's correct. 回答2: You'll need an NSDateFormatter , using setDateFormat: . Here's the

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

╄→гoц情女王★ 提交于 2019-12-22 05:15:09
问题 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.

Find NSDate for the next closest specific day of week for any date

99封情书 提交于 2019-12-22 04:35:07
问题 Let's suppose that today is Wednesday. I can break an NSDate down into NSDateComponents , but I need to find the NSDate with the next upcoming Monday. If today is Monday, then the next upcoming Monday is today. What's the right way to achieve this? 回答1: You can use nextDateAfterDate: method on NSCalendar object to achieve this, let now = Date() // today var matchingComponents = DateComponents() matchingComponents.weekday = 2 // Monday let comingMonday = Calendar.current.nextDate(after: now,