nsdate

How to compare two date?

倖福魔咒の 提交于 2020-01-02 17:30:19
问题 I have three dates: (1) previousDate (2) currentDate (3) nextDate, I want to check whether currentDate is later then previous date and earlier than nextDate. How do I do that? 回答1: NSDateFormatter *df= [[NSDateFormatter alloc] init]; [df setDateFormat:@"yyyy-MM-dd"]; NSDate *dt1 = [[NSDate alloc] init]; NSDate *dt2 = [[NSDate alloc] init]; dt1=[df dateFromString:@"2011-02-25"]; dt2=[df dateFromString:@"2011-03-25"]; NSComparisonResult result = [dt1 compare:dt2]; switch (result) { case

How to convert a DD-MM-YYYY date format string into a YYYY-MM-DD date format string or into a NSDate object in Objective-C?

一曲冷凌霜 提交于 2020-01-02 08:22:11
问题 I have read date from XML which give me back string where the date is like DD-MM-YYYY . But when I want to add it to my core data database, SQLite sorts me that wrong so I have to convert it to date format or to string like: YYYY-MM-DD . But SQLite does not support either Date() or To_Date() functions. Can you help me? 回答1: You can use NSDateFormatter to format a date in Objective C. Here's a quick example which might not do exactly what you want, but should hopefully give some pointers: //

IOS: problem with NSOrderedDescending and NSOrderedAscending

ぃ、小莉子 提交于 2020-01-02 07:36:30
问题 I want to compare dates and I use this code in one example date values for my date are: date1 = 6/06/2011 date2 = 8/06/2011 if dateSelected = 7/06/2011 it's all ok, but if dateSelected = 6/06/2011 or dateSelected = 8/06/2011 the code don't entry inside my "if", why??? if (([dateSelected compare:date1] == NSOrderedDescending) && ([dateSelected compare:date2]== NSOrderedAscending)) {} 回答1: NSDates represent a point in time since 1/1/2000 00:00 UTC. So all of those dates have time components.

Determining if an NSDate is today in NSPredicate

大城市里の小女人 提交于 2020-01-02 07:12:13
问题 I've tried multiple methods and tried some suggestions on this site, but how can I determine if a CoreData attribute "dueDate" is equal to "Today", I know NSDate is a specific time as well but I want the predicate to return if it is the same day/month/year regardless of what the time is in the day. Any suggestions? 回答1: This is what I came up with: - (NSPredicate *)matchAttribute:(NSString *)attributeName withDate:(NSDate *)date { NSDateComponents *components = [[NSCalendar currentCalendar]

Building NSDate from integers (year … second) with Cocoa/objective-C

痞子三分冷 提交于 2020-01-02 01:40:07
问题 I have year, mont, ... , second in integer type, and I need to build NSDate from them. I tried this code, but I got (null). NSDateFormatter *tempFormatter = [[[NSDateFormatter alloc]init]autorelease]; [tempFormatter setDateFormat:@"yyyy-mm-dd hh:mm:ss"]; NSString* message = [NSString stringWithFormat:@"%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, minute, second]; NSDate *day3 = [tempFormatter dateFromString:message]; What's wrong with this code? 回答1: instead of [tempFormatter

Do an action when a sound has finished playing in AVAudioPlayer?

☆樱花仙子☆ 提交于 2020-01-02 00:15:10
问题 I am using the AVAudioPlayer framework, and I have several sounds that play one at a time. When a sound is finished playing, I want the application to do something. I tried to use audioPlayerDidFinishPlaying to do the action at the end of the first sound, but I couldn't use that for the second sound because I got a redefinition error. Can I use NSTimeInterval to get the duration of a sound? // ViewController.h #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> #import <AVFoundation

Swift - Check if date is in next week / month. ( isDateInNextWeek() isDateInNextMonth() )

喜你入骨 提交于 2020-01-01 19:33:50
问题 We have those convenient functions in calendar: let calendar = NSCalendar.currentCalendar() calendar.isDateInToday(date) calendar.isDateInTomorrow(date) But I am missing those two: calendar.isDateInNextWeek(date) calendar.isDateInNextMonth(date) As @Rob mentioned, the meaning is: "in the calendar week starting this coming Sunday, going through the following Saturday" I having a hard time figuring out how to implement those functions in a robust way that covers all the corner cases. Can

Number of Months and Days between two NSDates

家住魔仙堡 提交于 2020-01-01 17:08:36
问题 I would like to calculate the number of months and days between two NSDates. I have the number of days calculating correctly, but how can convert that to months and remainder days? This is what I'm using to calculate total number of days, which is working correctly. - (NSInteger) numberOfDaysUntil { NSDate *fromDate; NSDate *toDate; NSCalendar *calendar = [NSCalendar currentCalendar]; [calendar rangeOfUnit:NSDayCalendarUnit startDate:&fromDate interval:NULL forDate:[self dateOnly:[NSDate date

How can I set only time in NSDate variable?

…衆ロ難τιáo~ 提交于 2020-01-01 04:33:05
问题 I have NSDate variable and would like to change only time (date shouldn't be changed). Is it possible ? Eg: user pick date interval in DatePicker date (If it's start date I would like to set time as 00:00:00, if it's end date, I set time as 23:59:59) Thanks all for your help. Regards, Alex. 回答1: You'll want to use NSDateComponents. NSDate *oldDate = datePicker.date; // Or however you get it. unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay; NSCalendar

NSDate dateByAddingTimeInterval subtracting days incorrectly changes time

社会主义新天地 提交于 2020-01-01 03:21:08
问题 Am I missing something about how NSDate dateByAddingTimeInterval works with days? Here is my code for subtracting 33 days from a specific date (AIDate). AIDate has a time value of 00:00 PST. activityOne.ActivityDateTime = [AIDate dateByAddingTimeInterval:-33*24*60*60]; If the AIDate equals 4-9-2013 00:00 PST, the above code will subtract 33 days and 1 hour which works out to 3-6-2013 23:00 PST. 回答1: 24*60*60 means 24 hours, and not 1 day. There is a huge difference between those two when your