nsdate

How can I figure out the day difference in the following example

非 Y 不嫁゛ 提交于 2019-12-02 08:40:41
I am calculating the day difference between two dates, but I figured out the the following code is actually giving me the difference for 24 hours rather than the difference in date. I have the following code: func daysBetweenDate(startDate: NSDate, endDate: NSDate) -> Int { let calendar = NSCalendar.currentCalendar() let components = calendar.components([.Day], fromDate:startDate, toDate: endDate, options: []) return components.day } So, for the following example I get this result: lastLaunch:2016-06-10 01:39:07 +0000 toady: 2016-06-11 00:41:41 +0000 dayDiff:0 I would have expected the day

Comparing two dates with nsdate

独自空忆成欢 提交于 2019-12-02 08:07:31
In my app I have to compare two times: current (which is 12:44) and given I tried: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"hh:mm"]; NSDate *neededDate = [[NSDate alloc] init]; neededDate = [dateFormatter dateFromString:@"14:45"]; if ([neededDate compare:[NSDate date]] == NSOrderedAscending) { NSLog(@"yes"); } else NSLog(@"no"); [neededDate release]; [dateFormatter release]; But any time I launch it, NSLog says "no". Please, suggest where I' m wrong. Thank you in advance. Because your neededDate will be equal to "1970/01/01 14:45" . You

Weird behavior when creating an NSDate of January the 1st and the 2nd

蓝咒 提交于 2019-12-02 07:50:55
I'm creating a date like this : NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"YYYY"]; NSInteger year = [[dateFormatter stringFromDate:[NSDate date]] intValue]; NSLog(@"NSInteger YEAR : %d\n", year); //minutes, hours, day and month are initialized elsewhere... NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setYear:year]; [comps setMonth:month]; [comps setDay:day]; [comps setHour:hours]; [comps setMinute:minutes]; NSDate *sampleDate =

NSDate from NSString - dateFromString returns nil

时光总嘲笑我的痴心妄想 提交于 2019-12-02 07:47: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]; The "Z" must be quoted: NSDateFormatter* df = [[NSDateFormatter alloc]init]; NSString *dateString = @"2012-04-05T13:27:32.369Z"; [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"]; NSDate* date = [df dateFromString:dateString]; NSLog(@"1: %@",date); NSString

Set current time to yyyy-MM-dd 00:00:00 using Swift

ぃ、小莉子 提交于 2019-12-02 07:00:57
I want to ask about NSDate , how to set/format current time like "2015-08-12 09:30:41 +0000" to "2015-08-12 00:00:00 +0000 I'm already using : var dateFormatter = NSDateFormatter() dateFormatter.dateStyle = .MediumStyle dateFormatter.timeStyle = .NoStyle //result date will be : Aug 12, 2015 but the value date is not stored in the Database exactly as "2015-08-12 00:00:00 +0000" but storing as "2015-08-11 17:00:00 +0000 UTC" Leo Dabus let dateString = "2015-08-12 09:30:41 +0000" let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" dateFormatter.calendar = NSCalendar

Creating NSDate from NSDateComponents off by one day

只愿长相守 提交于 2019-12-02 06:58:31
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: %@", date); However, this prints out "Date I tried to set: 2011-05-11 17:00:00 +0000." The date seems

NSLog showing the previous Date

青春壹個敷衍的年華 提交于 2019-12-02 06:30:59
问题 I want to retrieve all the entries from core data added between two dates.I am using NSPredicate . As I am not getting the correct result I tried logging the date.It is showing the previous dates.After googling for a while I added [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; to my code.Now,Its showing correct dates but the results are still wrong. This is the code I used. NSDate *fromDate = [[self dateFormatter]dateFromString:self.fromDateField.text]; NSLog(@"%@"

unexpected result of dateFromComponents:

夙愿已清 提交于 2019-12-02 06:14:21
问题 the following code NSCalendar *cal = [NSCalendar currentCalendar]; [cal setFirstWeekday:2]; [cal setMinimumDaysInFirstWeek:4]; NSDateComponents *comp = [[NSDateComponents alloc] init]; [comp setYear: 2012]; [comp setMonth:3]; [comp setDay: 12]; NSDate *date = [cal dateFromComponents:comp]; NSLog(@"date:%@",date); logs: date:2012-03-11 23:00:00 +0000 Any Idea, why this happen and how to avoid Tnx 回答1: I assume you are using the CET time zone locally. This is what the [NSCalendar

Find exact difference between two dates

女生的网名这么多〃 提交于 2019-12-02 05:24:44
问题 I want some changes in the date comparison. In my application I am comparing two dates and getting difference as number of Days, but if there is only one day difference the system shows me 0 as a difference of days. NSDateFormatter *date_formater=[[NSDateFormatter alloc]init]; [date_formater setDateFormat:@"MMM dd,YYYY"]; NSString *now=[NSString stringWithFormat:@"%@",[date_formater stringFromDate:[NSDate date]]]; LblTodayDate.text = [NSString stringWithFormat:@"%@",[NSString stringWithFormat

NSDateFormatter won't format strange date string

纵饮孤独 提交于 2019-12-02 05:03:11
问题 So I have a date string I receive that looks like this: "2013-03-20T21:13:26-7:00" that I receive from a web back end. I have no control over the back end, just a fyi. My preference would be to have the date formatted like this: 9:13pm at 3/20. When I do the following NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSDate *date = [dateFormatter dateFromString:@"2013-03-20T21:13:26-7:00"]; date is null. My first thought is that the date string looks odd, and maybe I should