nsdate

Age extracted from birth date always off by inconsistent amount

强颜欢笑 提交于 2019-12-20 07:43:50
问题 I'm using the following code to convert a user-supplied birthdate to its equivalent years from the current date. The output is always off by an inconsistent amount in years and very large numbers in days and months. NSDateFormatter *tempFormatter = [[NSDateFormatter alloc] init]; [tempFormatter setDateFormat:@"yyyy-mm-dd hh:mm:ss"]; NSDate *birthDate = [tempFormatter dateFromString:[NSString stringWithFormat:@"%@-%@-%@ 01:00:00",self.birthYear.text,self.birthMonth.text,self.birthDay.text]];

NSDate from NSString - dateFromString returns nil

安稳与你 提交于 2019-12-20 07:28:15
问题 I want to get a NSDate object from a string of date, but it returns nil. What am I doing wrong? Thanks! NSString *dateString = @"2012-04-05T13:27:32.369Z"; NSDateFormatter* df = [[NSDateFormatter alloc]init]; [df setDateFormat:@"YYYY-MM-dd'T'HH:mm:ss.SSSZ"]; NSDate* date = [df dateFromString:dateString]; NSLog(@"1%@",date); [df release]; 回答1: The "Z" must be quoted: NSDateFormatter* df = [[NSDateFormatter alloc]init]; NSString *dateString = @"2012-04-05T13:27:32.369Z"; [df setDateFormat:@

Creating NSDate from NSDateComponents off by one day

為{幸葍}努か 提交于 2019-12-20 05:23:34
问题 I'm trying to set a particular date for a unit test: NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setCalendar:calendar]; [components setYear:2011]; [components setMonth:5]; [components setDay:12]; [components setHour:1]; [components setMinute:0]; [components setSecond:0]; NSDate *date = [calendar dateFromComponents:components]; NSLog(@"Date I tried to set: %

Swift - NSDate and last week of year

江枫思渺然 提交于 2019-12-20 04:24:51
问题 I am making a TimeTable app, and i have a method that adds 1 week to the current date, this works as it is supposed to, however if the week transitions from December to January, it adds 1 day extra. Here is my code: func getWeekDates(var date: NSDate) -> [NSDate] { var dates: [NSDate] = [NSDate]() for var i = 0; i < 5; i++ { date = date.dateAtWeekStart() + 1.day - 1.week date += i.day dates.append(date) } return dates } And dateAtWeekStart() : func dateAtWeekStart() -> NSDate { let flags :

NSDateFormatter reports June 2, 2013 as being in week zero

拥有回忆 提交于 2019-12-20 04:20:55
问题 I am using NSDateFormatter to convert a series of dates to a week number within a month. The date formatting code I am using is yyyyMMW and everything I have read tells me that W will be between 1-5. But, the 2nd of June 2013 fell on a Sunday (the default start day of the week in the gregorian calendar) and it's week number is reported as 0 even though the start date of the week is calculated correctly: 2013-06-03 14:15:45.611 date=20130531, week=2013055, start of week=20130526 2013-06-03 14

Error when inserting NSDate to CoreData

守給你的承諾、 提交于 2019-12-20 04:08:57
问题 NSDateFormatter *dt = [[NSDateFormatter alloc] init]; [dt setDateFormat:@"dd-MM-yyyy"]; NSDate *bdate = [dt dateFromString:@"02-02-2013"]; [nouContacte setValue:bdate forKey:@"birthday"]; I don't understand why that code doesn't work. It inserts 9 different digits like 318366000. I can insert strings, "booleans", numbers, but i can't with date. birthday field in coredata is date type. any idea? 回答1: For a "Date" attribute, Core Data stores the "number of seconds since 1 January 2001, GMT" in

Calculate seconds between current time and next event

守給你的承諾、 提交于 2019-12-20 03:53:40
问题 This should be simple, but it's proving challenging for me. I'd like to know the best approach to calculating the difference in seconds between [NSDate date] and a future event x seconds from that time. There are several different types of event, and each event may occur several times a day, and at different times, depending what day of the week it happens to be. What I am trying to do is have the user select an event type from a picker and then set an alarm in Notification Center for the

How to format the date to IST?

帅比萌擦擦* 提交于 2019-12-20 02:42:26
问题 I have to format the date as below: 19-JUL-2014 10:27:16 IST How can I do that? Should I send "IST" as string object? I tried - NSDate* sourceDate = [NSDate date]; NSLog(@"Date is : %@", sourceDate); NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone]; NSLog(@"TimeZone is : %@", currentTimeZone); NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init] ; dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm:ss Z"]; [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]]; NSLog(@"%@",

NSSortdescriptor ineffective on fetch result from NSManagedContext

放肆的年华 提交于 2019-12-19 21:16:52
问题 I'm trying to sort my NSFetchRequest result using a NSSortdescriptor using a key pointing to a NSDate value. My fetch results come out totally random for no clear reason. The NSManagedObjectContext I'm using is updated with a save from a nested child context created on a subclass of NSOperation. I know all this is done successfully because I can get all the data needed from the parent (main) context. Fetching from it just wont sort on date! Strange thing is; predicates for selecting the

How to auto generate NSManagedObject subclasses with date attribute as Date instead of NSDate?

馋奶兔 提交于 2019-12-19 17:33:06
问题 I'm currently updating my project to Swift 3 and I'm moving all my NSDate methods and extensions to Date in order to keep the standard in the app. The problem is that I use Xcode to auto-generate my NSManagedObject subclasses and it is generating the date attributes as NSDate instead of Date. Is there a way to generate it with the date attributes as Date? EDIT Per Apple Developer Documentation: Core Data natively supports a variety of attribute types, such as string, date, and integer