I want to get all NSDate from to to 30 days later. However it doesn't work correctly.
Here is the code i am using:
for (int i = 0; i < days; i++) {
NSDate *today = [self getToday];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = i;
NSDate *nextMonth = [gregorian dateByAddingComponents:components toDate:today options:0];
NSDateComponents *nextMonthComponents = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:nextMonth];
NSDateComponents *todayDayComponents = [gregorian components:NSDayCalendarUnit fromDate:today];
nextMonthComponents.day = todayDayComponents.day+i;
NSDate *nextMonthDay = [gregorian dateFromComponents:nextMonthComponents];
DLog(#"nextMonthDay = %#",nextMonthDay);
[months addObject:nextMonthDay];
}
I am getting list :
nextMonthDay = 2013-01-03 22:00:00 +0000
nextMonthDay = 2013-01-04 22:00:00 +0000
nextMonthDay = 2013-01-05 22:00:00 +0000
nextMonthDay = 2013-01-06 22:00:00 +0000
nextMonthDay = 2013-01-07 22:00:00 +0000
nextMonthDay = 2013-01-08 22:00:00 +0000
nextMonthDay = 2013-01-09 22:00:00 +0000
nextMonthDay = 2013-01-10 22:00:00 +0000
nextMonthDay = 2013-01-11 22:00:00 +0000
nextMonthDay = 2013-01-12 22:00:00 +0000
nextMonthDay = 2013-01-13 22:00:00 +0000
nextMonthDay = 2013-01-14 22:00:00 +0000
nextMonthDay = 2013-01-15 22:00:00 +0000
nextMonthDay = 2013-01-16 22:00:00 +0000
nextMonthDay = 2013-01-17 22:00:00 +0000
nextMonthDay = 2013-01-18 22:00:00 +0000
nextMonthDay = 2013-01-19 22:00:00 +0000
nextMonthDay = 2013-01-20 22:00:00 +0000
nextMonthDay = 2013-01-21 22:00:00 +0000
nextMonthDay = 2013-01-22 22:00:00 +0000
nextMonthDay = 2013-01-23 22:00:00 +0000
nextMonthDay = 2013-01-24 22:00:00 +0000
nextMonthDay = 2013-01-25 22:00:00 +0000
nextMonthDay = 2013-01-26 22:00:00 +0000
nextMonthDay = 2013-01-27 22:00:00 +0000
nextMonthDay = 2013-01-28 22:00:00 +0000
nextMonthDay = 2013-01-29 22:00:00 +0000
nextMonthDay = 2013-01-30 22:00:00 +0000
nextMonthDay = 2013-03-03 22:00:00 +0000
nextMonthDay = 2013-03-04 22:00:00 +0000
At the end of the list is:
nextMonthDay = 2013-01-29 22:00:00 +0000
nextMonthDay = 2013-01-30 22:00:00 +0000
nextMonthDay = 2013-03-03 22:00:00 +0000
nextMonthDay = 2013-03-04 22:00:00 +0000
So this is incorrect. There should be instead
2013-01-29 22:00:00 +0000
2013-01-30 22:00:00 +0000
2013-03-01 22:00:00 +0000
2013-03-02 22:00:00 +0000
Maybe someone could help me on this ?
Thank you.
You want to look at the Date & Time Programming Guide, especially: [Adding Components to a Date
](https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendricalCalculations.html#//apple_ref/doc/uid/TP40007836-SW1)
You need to use the setDay: method in your loop to generate NSDate instances for each day offset from the reference NSDate you start with.
NSLog(#"30 consecutive days!");// just hello.
// Set up data outside the loop...
NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
// Surely you want to do something clever with these beyond logging them immediately, right? Let's put them in an array. They'll be in order and we can use them again later.
NSMutableArray *thirtyConsecutiveDays = [[NSMutableArray alloc] initWithCapacity:30];
int days = 30; // This is one less than thirty-one.
// Ok, let's loop!
for (int i = 0; i < days; i++) {
// We're going to want a new one of these each time through the loop.
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
// We need to tell it how far to offset from the reference NSDate.
[offsetComponents setDay:i]; // incrementing by i days.
// We will have our current NSDateComponents friend tell our NSCalendar friend what NSDate we want next...
NSDate *anotherDate = [gregorian dateByAddingComponents: offsetComponents
toDate:today options:0];
// Now add our latest NSDate iteration to our array.
[thirtyConsecutiveDays addObject:anotherDate];
}
// This is just logging results.
for (NSDate *aDate in thirtyConsecutiveDays) {
NSLog(#"%#",aDate);
}
I have different solution for you
int min = 60*60*24;
for (int i = 0; i < days; i++) {
NSDate *today = [self getToday];
NSDate *newDate = [today dateByAddingTimeInterval:(min*i)];
}
Hope it helps you
How about:
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *date = [self getToday];
for (int i = 1; i < 30 ; i++) {
date = [calendar dateByAddingComponents:components toDate:date options:0];
[months addObject:date];
}
Related
I am adding an event with NSDate value
“2017-04-25 15:00:00 +0000”
As per my timezone, it’s 2017-04-25 08:40 PM.
I am getting NSDate value from a function.
NSString *strDateTime = #"Tuesday, 25 Apr 2017 08:30 PM";
NSDateFormatter *formatterLocal = [[NSDateFormatter alloc] init];
[formatterLocal setTimeZone:[NSTimeZone systemTimeZone]];
[formatterLocal setDateFormat:#"EEEE, dd MMM yyyy hh:mm a"];
NSDate *dateAdd = [formatterLocal dateFromString:strDateTime];
While I check reminder app it’s showing event with date 25/04/17, 3:00 PM. While it should be 25/04/17, 8:00 PM.
Can anyone please help me out from this!
I have already checked Get wrong time when adding an event to default calendar in iPhone
I just catch my mistake.
I have set the wrong timezone in "dueDateComponents".
Below is my code.
EKReminder *reminder = [EKReminder reminderWithEventStore:self.eventStore];
reminder.dueDateComponents = [self dateComponentsForDefaultDueDate];
- (NSDateComponents *)dateComponentsForDefaultDueDate {
/*
Changing date components
*/
NSCalendar *cal = [NSCalendar currentCalendar];
//[cal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
NSDateComponents *components = [cal components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |NSCalendarUnitHour | NSCalendarUnitMinute fromDate:[NSDate dateWithTimeIntervalSince1970:[_dictData[#"start"] longLongValue]]];
components.hour = [components hour];
components.minute = [components minute];
return components;
}
I just commented
//[cal setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
Silly mistake :p
For some reason, this code is not converting the second string correctly; this is the code:
NSLog(#"\n\nsearchStartTime: %# searchEndTime: %#",searchStartTime, searchEndTime);
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm a"];
startDate = [dateFormatter dateFromString:searchStartTime];
endDate = [dateFormatter dateFromString:searchEndTime];
NSLog(#"\n\nstartDate: %# endDate: %#", startDate, endDate);
This the definition and value of the searchStartTime and searchEndTime:
searchStartTime: 2015-03-05 08:30 AM searchEndTime: 2015-03-05 09:30 AM
This is the result from NSLog:
startDate: 2015-03-05 08:30:00 +0000 endDate: 2015-03-05 08:30:00 +0000
What's wrong with my code here? I want to take these two date/time objects and use them in a NSPredicate to compare dates in a Core Data record.
It all comes down to knowing your format code strings. You are misusing "HH" here so the hour is coming out wrong.
Another problem is that you are not capturing the error. By calling dateFromString:, you are missing out on your chance to hear about errors. Use getObjectValue:forString:range:error: to learn of problems.
Anyway, I substituted "hh" where you have "HH" and the answers came out right:
NSString* searchStartTime = #"2015-03-05 08:30 AM";
NSString* searchEndTime = #"2015-03-05 09:30 AM";
NSLog(#"\n\nsearchStartTime: %# searchEndTime: %#",searchStartTime, searchEndTime);
NSDateFormatter* dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm a"];
NSDate* startDate = [dateFormatter dateFromString:searchStartTime];
NSDate* endDate = [dateFormatter dateFromString:searchEndTime];
NSLog(#"\n\nstartDate: %# endDate: %#", startDate, endDate);
startDate: 2015-03-05 16:30:00 +0000 endDate: 2015-03-05 17:30:00 +0000
I am in California, and the time is shown relative to London, so if you subtract the time difference you'll see that this is correct.
One error is "HH" which is 24-hour tine but you are also using "a" for an & pm.
Instead use "hh" for 12-hour time.
Example:
NSString *searchStartTime = #"2015-03-05 08:30 AM";
NSString *searchEndTime = #"2015-03-05 09:30 AM";
NSLog(#"\n\nsearchStartTime: %# searchEndTime: %#", searchStartTime, searchEndTime);
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat: #"yyyy-MM-dd hh:mm a"];
NSDate *startDate = [dateFormatter dateFromString:searchStartTime];
NSDate *endDate = [dateFormatter dateFromString:searchEndTime];
NSLog(#"\n\nstartDate: %# endDate: %#", startDate, endDate);
Output:
searchStartTime: 2015-03-05 08:30 AM searchEndTime: 2015-03-05 09:30 AM
startDate: 2015-03-05 13:30:00 +0000 endDate: 2015-03-05 14:30:00 +0000
See: ICU Formatting Dates and Times
Also: Date Field SymbolTable.
I have three dates 'startDate', stopDate and currentDate. I need compare 'startDate' <= 'currentDate' and 'stopDate' >= 'currentDate'. My code.
unsigned int flagsDate = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents* fromComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate:startDate];
NSDate* fromDateOnly = [[NSCalendar currentCalendar] dateFromComponents:fromComponents];
NSDateComponents* toComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate:stopDate];
NSDate* toDateOnly = [[NSCalendar currentCalendar] dateFromComponents:toComponents];
NSDate * currentDate = [NSDate date];
NSDateComponents* currComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate: currentDate];
NSDate* currDateOnly = [[NSCalendar currentCalendar] dateFromComponents:currComponents];
NSTimeInterval fromTime = [fromDateOnly timeIntervalSinceReferenceDate];
NSTimeInterval toTime = [toDateOnly timeIntervalSinceReferenceDate];
NSTimeInterval currTime = [currDateOnly timeIntervalSinceReferenceDate];
NSLog(#"%# %#",currentDate, currDateOnly);
NSLog(#"%# %#",startDate, fromDateOnly);
NSLog(#"%# %#",stopDate, toDateOnly);
That's what brings nslog.
2014-04-10 23:59:53.280 Calendar[2148:90b] 2014-04-10 19:59:53 +0000 2014-04-09 20:00:00 +0000
2014-04-10 23:59:53.282 Calendar[2148:90b] 2014-04-10 20:14:12 +0000 2014-04-10 20:00:00 +0000
2014-04-10 23:59:53.282 Calendar[2148:90b] 2015-04-09 20:14:12 +0000 2015-04-09 20:00:00 +0000
How do I compare dates? I need to compare dates only without time. Why change the date if time is less than 20:00?
NSTimeInterval distanceBetweenDates = [StartDate timeIntervalSinceDate:EndDate];
if (distanceBetweenDates > 0) {
NSLog("Greather than ")
} else if (distanceBetweenDates < 0) {
NSLog("less than");
} else {
NSLog("Equal");
}
Alexey, I suggest you incorporate this wonderful NSDate extension in your project: https://github.com/erica/NSDate-Extensions
You can then use
- (NSInteger)distanceInDaysToDate:(NSDate *)anotherDate
And check if it's < 0, 0 or > 0
If you wanted to compare only the dates, do as follows:
NSInteger flagsDate = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
NSDateComponents* fromComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate:startDate];
NSDateComponents* toComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate:stopDate];
NSDateComponents* currComponents = [[NSCalendar currentCalendar] components:flagsDate fromDate:cu];
NSComparisonResult firstComparison = [[fromComponents date] compare: [currComponents date]];
NSComparisonResult secondComparison = [[toComponents date] compare: [currComponents date]];
then to check your result
if (firstComparison == NSOrderedDescending || firstComparison == NSOrderedSame) {}
if (secondComparison == NSOrderedSame || secondComparison == NSOrderedAscending) {}
I have a simple program which was working with IOS4 and 5.
Now with IOS 6 I'm getting the following error message:
2012-10-05 11:08:16.386 app[1698:19d03] 2012-10-05 09:08:11 +0000
2012-10-05 11:08:18.072 app[1698:19d03] 2012-10-05 09:08:11 +0000
2012-10-05 11:08:19.273 app[1698:19d03] 2012-10-05 09:08:11 +0000
2012-10-05 11:08:19.274 app[1698:19d03] 2012-10-05 09:08:11 +0000
2012-10-05 11:08:19.275 app[1698:19d03] 0
2012-10-05 11:08:25.055 app[1698:19d03] 2011-10-05 09:08:11 +0000
2012-10-05 11:08:25.823 app[1698:19d03] (
"<UILongPressGestureRecognizer: 0xa167f60; state = Possible; view = <UITableViewCellContentView 0xa168000>; target= <(action=_longPressGestureRecognized:, target=<UIPickerTableViewWrapperCell 0xa168090>)>>")
2012-10-05 11:08:25.824 app[1698:19d03] 2012-10-05 09:08:11 +0000
2012-10-05 11:08:25.824 app[1698:19d03] -[__NSArrayI timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x94ef5d0
2012-10-05 11:08:25.825 app[1698:19d03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x94ef5d0'
*** First throw call stack:
(0x1d16012 0x11fae7e 0x1da14bd 0x1d05bbc 0x1d0594e 0x1d25cd3 0xa092 0x120e705 0x145920 0x1458b8 0x206671 0x206bcf 0x205d38 0x17533f 0x175552 0x1533aa 0x144cf8 0x1f4fdf9 0x1f4fad0 0x1c8bbf5 0x1c8b962 0x1cbcbb6 0x1cbbf44 0x1cbbe1b 0x1f4e7e3 0x1f4e668 0x14265c 0x296d 0x28a5)
The first run is with two dates which are equal.
On the second run, I've changed the fhDate on the picker as you can see...
The code is simplified a bit for easier reading:
aiDate = [adoIgazPicker date];
NSLog(#"%#", aiDate);
fhDate = [forgHelyPicker date];
NSLog(#"%#", fhDate);
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *startDate = fhDate;
NSDate *endDate = aiDate;
NSLog(#"%#", startDate);
NSLog(#"%#", endDate);
NSDateComponents *comps = [gregorian components:NSMonthCalendarUnit fromDate:startDate toDate:endDate options:0]; //THIS LINE FAILS IF THE DATES ARE NOT THE SAME
int months = [comps month];
NSLog(#"%i", months);
Thank you for your help!
I've replaced
NSDateComponents *comps = [gregorian components:NSMonthCalendarUnit fromDate:startDate toDate:endDate options:0];
with
NSInteger months = [[[NSCalendar currentCalendar] components: NSMonthCalendarUnit
fromDate: startDate
toDate: endDate
options: 0] month];
and now it's working :).
I am trying to do a date from string however it always makes the date in the month of January... Why?
Code:
NSMutableArray *dateArray = [NSMutableArray array];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYYMMDD"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
for (id <NSFetchedResultsSectionInfo> sectionInfo in [fetchedResultsController sections]) {
NSLog(#"Adding date: %#", [sectionInfo name]);
NSDate *newDate = [dateFormatter dateFromString:[sectionInfo name]];
NSLog(#"Adding date 2: %#", newDate);
[dateArray addObject:newDate];
}
LOGS:
2012-03-01 15:14:48.124 MyApp[21793:fb03] Adding date: 20120827
2012-03-01 15:14:48.124 MyApp[21793:fb03] Adding date 2: 2012-01-27 00:00:00 +0000
2012-03-01 15:14:48.125 MyApp[21793:fb03] Adding date: 20120830
2012-03-01 15:14:48.125 MyApp[21793:fb03] Adding date 2: 2012-01-30 00:00:00 +0000
2012-03-01 15:14:48.125 MyApp[21793:fb03] Adding date: 20120831
2012-03-01 15:14:48.126 MyApp[21793:fb03] Adding date 2: 2012-01-31 00:00:00 +0000
2012-03-01 15:14:48.126 MyApp[21793:fb03] Adding date: 20120906
2012-03-01 15:14:48.127 MyApp[21793:fb03] Adding date 2: 2012-01-06 00:00:00 +0000
2012-03-01 15:14:48.127 MyApp[21793:fb03] Adding date: 20120907
2012-03-01 15:14:48.128 MyApp[21793:fb03] Adding date 2: 2012-01-07 00:00:00 +0000
2012-03-01 15:14:48.128 MyApp[21793:fb03] Adding date: 20120910
2012-03-01 15:14:48.128 MyApp[21793:fb03] Adding date 2: 2012-01-10 00:00:00 +0000
2012-03-01 15:14:48.129 MyApp[21793:fb03] Adding date: 20120913
2012-03-01 15:14:48.129 MyApp[21793:fb03] Adding date 2: 2012-01-13 00:00:00 +0000
The day of month specifier is dd, not DD. Also, you might want to use the yyyy format for years, as YYYY means the ISO 'Week of Year', and may be different from the actual year. So your final format should look like:
[dateFormatter setDateFormat:#"yyyyMMdd"];