nsdate

How to determine if locale's date format is Month/Day or Day/Month?

亡梦爱人 提交于 2019-11-29 12:55:55
问题 In my iPhone app, I'd like to be able to determine if the user's locale's date format is Month/Day (i.e. 1/5 for January fifth) or Day/Month (i.e. 5/1 for January fifth). I have a custom NSDateFormatter which does not use one of the basic formats such as NSDateFormatterShortStyle (11/23/37). In an ideal world, I want to use NSDateFormatterShortStyle, but just not display the year (only month & day#). What's the best way to accomplish this? 回答1: You want to use NSDateFormatter's

Parsing ISO 8601 with NSDateFormatter

痞子三分冷 提交于 2019-11-29 12:48:56
I've read through the various posts on SO about this, but have still not solved the problem. Here's the string that I'm trying to parse into an NSDate object: 2012-04-08T12:00:00.00+02:00 I understand that the problem is the time zone format. Here's the date format string that I'm trying: yyyy-MM-dd'T'HH:mm:SSZZZZ Thanks for any help. Three things: Use the -[NSDateFormatter getObjectValue:forString:range:error:] method to learn how much of the string was parsed and what error prevented parsing further. The ZZZZ format string corresponds to something like "GMT+02:00", with that "GMT" in there.

NSDateComponents weekOfYear returns wrong date

心不动则不痛 提交于 2019-11-29 12:45:29
so i am making a basic week picker, where you can pick a week number and year, and get the corresponding date from the weeks startpoint. A basic test scenario let calendar = NSCalendar.currentCalendar() let components = NSDateComponents() components.year = 2015 components.weekOfYear = 15 print(calendar.dateFromComponents(components)!) You can see, that i set the wanted year to 2015 and the wanted week to 15, but the result i get is: "2014-12-31 23:00:00 +0000" ... and thats really gets me hanging. No matter what week and year i choose, it will always be off and in december. Thank you in

NSDate subtract one month

隐身守侯 提交于 2019-11-29 11:50:38
问题 I want two NSInteger year, month to go one month back. e.g. now is 2011-01 how can I get 2010-12 . adding is one thing, but I want to subtract. NSDate *date = [NSDate date]; NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date]; NSInteger year = [dateComponents year]; NSInteger month = [dateComponents month]; [calendar release]; NSString * pdf_name = [NSString stringWithFormat:@"file_%d_%02d.pdf", year, month]; greets endo EDIT: My solution NSCalendar *calendar= [

How to make a periodic call to a method in objective c?

限于喜欢 提交于 2019-11-29 11:19:47
if the question is not explained clearly please excuse me. I'm developing an iphone Client-Server app, i created all the classes, instances and ect. I can even send get and parse the response too.. Anyway, now i need to make my method be called in a defined period of time(for instance, call it repeatly in 10 seconds). I googled a lot and also take a look at NSDate but i couldn't solve.. Now, can anyone please help me how to handle this situation? Thank you very much You can create and schedule an instance of NSTimer to call your method on a given time interval. See in particular the following

NSDate for first day of the month

蹲街弑〆低调 提交于 2019-11-29 10:48:47
问题 This is quite a simple concept, but as of yet I have been unable to find an elegant (and calendar locale independent) solution. I need to find the first day of the month for an arbitrary NSDate . For example, given an arbitrary NSDate ( arbitraryDate ) another NSDate object will be returned (let's call this firstDayOfMonthDate ) which represents the first day of the month in arbitraryDate . The time component does not matter, as I just need the NSDate object for the first day of the month

NSCalendar, why does setting the firstWeekday doesn't effect calculation outcome?

风格不统一 提交于 2019-11-29 10:36:34
i need to calculate the weekday for a given date, however, depending on the calendar a week can star on Monday and somwhere on Sunday so i wanted to set it, to start on Monday, using [[NSCalendar currentCalendar] setFirstWeekday:2]; however, the calculation outcome is the same { [[NSCalendar currentCalendar] setFirstWeekday:1]; NSDateComponents *weekdayComponents = [[NSCalendar currentCalendar] components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date]; NSInteger weekday = [weekdayComponents weekday] - 1; NSLog(@"%d", weekday); } { [[NSCalendar currentCalendar] setFirstWeekday:2];

NSPredicate BETWEEN with NSDate causes -[__NSDate constantValue]: unrecognized selector sent to instance 0x1e7ff0

大兔子大兔子 提交于 2019-11-29 10:08:39
I'm trying to fetch from Core Data records that have a startTime between two dates. Here's my code: NSDate *today = [NSDate date]; NSDate *distantFuture = [NSDate distantFuture]; // Create the predicate NSPredicate *predicate = [NSPredicate predicateWithFormat:@"startTime BETWEEN %@", [NSArray arrayWithObjects:today, distantFuture, nil]]; NSLog(@"today: %@, distantFuture: %@", today, distantFuture); NSLog(@"predicate: %@", predicate); // Add the predicate to the fetchRequest [[[self fetchedResultsController] fetchRequest] setPredicate:predicate]; NSError *error; if (![[self

JSON date to NSDate Issue

青春壹個敷衍的年華 提交于 2019-11-29 09:26:05
问题 I have a date string in JSON format, it looks like this: 2013-05-22T10:54:40.437 And I'm trying to convert it to NSDate. - (NSString *)dateFromString:(NSString *)datestring{ // Convert string to date object NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"]; NSDate *date = [dateFormat dateFromString:datestring]; //date is nil here. NSDateFormatter *newDateFormatter = [[NSDateFormatter alloc]init]; [newDateFormatter

Compare NSDate for Today or Yesterday

心不动则不痛 提交于 2019-11-29 08:52:25
问题 Well I guess this has been asked a thousand times, but for some reason the answeres dont really work or had other problems,.... Anyway here is what I have "working" : NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *currentDate = [NSDate date]; NSDateComponents *comps = [[NSDateComponents alloc] init]; // set tomorrow (0: today, -1: yesterday) [comps setDay:0]; NSDate *dateToday = [calendar dateByAddingComponents:comps toDate:currentDate options:0]; [comps setDay:-1]; NSDate