I want to get day parts from a date interval.
For example I have a date interval 21/12/2013 to 28/12/2013.
Now I want to get all day part from this interval like as 21,22,23,24,25,26,27,28.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateFormat:#"MMM dd, yyyy"];
NSString *date_str=[NSString stringWithFormat:#"%#",from_date];
NSLog(#"%#",date_str);
NSString *date_str1=[NSString stringWithFormat:#"%#",to_date];
NSLog(#"%#",date_str1);
NSDate *starting_date = [dateFormatter dateFromString:date_str];
NSDate *end_date = [dateFormatter dateFromString:date_str1];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger unitFlags = NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:starting_date toDate:end_date options:0];
NSInteger days = [components day];
That code giving total no of days.
How can i get this output?
Thanks
Continuing from your code and assuming,
NSString *date_str=[NSString stringWithFormat:#"%#",#"December 21, 2013"];
NSString *date_str1=[NSString stringWithFormat:#"%#",#"December 28, 2013"];
If you need it within the same month, it can be in this way,
NSDateComponents *component1 = [gregorian components:unitFlags fromDate:starting_date];
NSDateComponents *component2 = [gregorian components:unitFlags fromDate:end_date];
for (int i = component1.day; i <= component2.day; i++) {
NSLog(#"%i", i);
}
But if your dates expand between different months, then you have to add checking for month components in the loop.
I will continue on your code, with some dirty code of mine, but it gets the job done if what you provided is your date format:
NSArray *begining_day = [date_str componentsSeparatedByString:#"/"];
NSMutableArray *part_days = [[NSMutableArray alloc] init];
for (int i = 0; i < days; i++) {
[part_days addObject:([begining_day[0] integerValue] + i)];
}
Now part_days will have your days, BUT! You have to handle the end of each month.
I'm 100% sure you'll find better solutions, but I thought I could share the first thing I thought of, you might benefit from it.
You can try below code:
NSArray *array = [[NSArray alloc] initWithObjects:#"1/12/14",#"2/12/14",#"3/12/14",#"4/12/14",#"5/12/14", nil];
for (int i = 0; i < [array count]; i++) {
NSArray *myArray = [[array objectAtIndex:i] componentsSeparatedByString:#"/"];
[dayArray addObject:[myArray objectAtIndex:0]];
}
NSLog(#"%#",dayArray);
You can incrementally add 24 hours to your startDate until you pass your endDate. Using NSDateComponents you can pick out the day. Instead of logging them with NSLog just store them in a string or array.
NSDate *startDate = [NSDate dateWithTimeIntervalSinceReferenceDate:300000000];
NSDate *endDate = [NSDate dateWithTimeIntervalSinceReferenceDate:302000000];
for (NSDate *nextDate = startDate; [nextDate compare:endDate] < 0; nextDate = [nextDate dateByAddingTimeInterval:24*60*60] ) {
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:nextDate];
NSInteger day = [components day];
NSLog(#"day %li", (long)day);
}
Related
I am trying to get all dates in the current week of a given date:
In the example below, the input is
2015-11-29 23:40:37 +0000
, so I would expect the output to be an array of dates from November 23 - November 29, but the actual output is November 30 - December 6.
-(NSArray *)datesForWeekOf : (NSDate *) date {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *startOfWeek;
NSDate *endOfWeek;
NSTimeInterval interval;
[calendar rangeOfUnit:NSCalendarUnitWeekOfMonth startDate:&startOfWeek interval:&interval forDate:date];
endOfWeek = [startOfWeek dateByAddingTimeInterval:interval-1];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian];
NSDateComponents *days = [[NSDateComponents alloc] init];
NSInteger dayCount = 0;
NSMutableArray *allDates = [NSMutableArray array];
while ( TRUE ) {
[days setDay: ++dayCount];
NSDate *date = [gregorianCalendar dateByAddingComponents: days toDate: startOfWeek options: 0];
[allDates addObject:date];
if ( [date compare: endOfWeek] == NSOrderedDescending )
break;
}
return allDates;
}
You can get all dates of this week and next week using following code:-
NSArray * allDatesOfThisWeek = [self daysThisWeek];
NSArray * allDatesOfNextWeek = [self daysNextWeek];
Following methods are used for calculating dates of this week:-
-(NSArray*)daysThisWeek
{
return [self daysInWeek:0 fromDate:[NSDate date]];
}
-(NSArray*)daysNextWeek
{
return [self daysInWeek:1 fromDate:[NSDate date]];
}
-(NSArray*)daysInWeek:(int)weekOffset fromDate:(NSDate*)date
{
NSCalendar *calendar = [NSCalendar currentCalendar];
//ask for current week
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:date];
//create date on week start
NSDate* weekstart=[calendar dateFromComponents:comps];
NSDateComponents* moveWeeks=[[NSDateComponents alloc] init];
moveWeeks.weekOfYear=weekOffset;
weekstart=[calendar dateByAddingComponents:moveWeeks toDate:weekstart options:0];
//add 7 days
NSMutableArray* week=[NSMutableArray arrayWithCapacity:7];
for (int i=1; i<=7; i++) {
NSDateComponents *compsToAdd = [[NSDateComponents alloc] init];
compsToAdd.day=i;
NSDate *nextDate = [calendar dateByAddingComponents:compsToAdd toDate:weekstart options:0];
[week addObject:nextDate];
}
return [NSArray arrayWithArray:week];
}
If you want to get dates of next to next week from today, then pass weekOffset=2 like this:-
NSArray * allDatesOfNextToNextWeek = [self daysInWeek:2 fromDate:now];
If you want to get dates of previous week from today, then pass weekOffset=-1 like this:-
NSArray * allDatesOfPreviousWeek = [self daysInWeek:-1 fromDate:now];
Hope, this is what you're looking for. Any concern get back to me.
I have get Sequence of month using following code
NSDate *aDate = [NSDate dateWithTimeInterval:row*30*24*60*60 sinceDate:self.earliestPresentedDate];
NSDateComponents *components = [self.calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[NSDate date]];
NSDate *today = [self.calendar dateFromComponents:components];
components = [self.calendar components:(NSEraCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:aDate];
NSDate *otherDate = [self.calendar dateFromComponents:components];
if ([today isEqualToDate:otherDate]) {
} else {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale currentLocale];
formatter.dateFormat = #"MMM ";
[lblDate setText:[formatter stringFromDate:aDate]];
}
i want to get Sequence of year how to get...please give solution
Make one function which gives to date of next year as below,
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.year = +1;
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *newDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale currentLocale];
formatter.dateFormat = #"yyyy";
NSString *strDate = [formatter stringFromDate:newDate];
NSLog(#"%#",strDate);
Set this date in your label.
above I am passing current date so I Will get year : 2016.
Simple way, it logs this and the subsequent 7 years.
NSInteger howManyYears = 8;
NSInteger thisYear = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]];
for (NSInteger i = 0; i < howManyYears; ++i) {
NSLog(#"%ld", thisYear++);
}
or to get an array of strings
NSMutableArray *theYears = [NSMutableArray array];
NSInteger howManyYears = 8;
NSInteger thisYear = [[NSCalendar currentCalendar] component:NSCalendarUnitYear fromDate:[NSDate date]];
for (NSInteger i = 0; i < howManyYears; ++i) {
[theYears addObject:[NSString stringWithFormat:#"%ld", thisYear++]];
}
NSLog(#"%#", theYears);
I suppose that I ask duplicate question, but I can not find solution for me. I need to get 1st day of the previous month. As I understand I have to get calendar from today, subtract 1 month and set day to be 1st. After that I have to convert to date. Unfortunately I get last day of previous month.
I use following code:
NSDate *maxDate = [[NSDate alloc] init];
NSCalendar *calendarCurr = [NSCalendar currentCalendar];
NSDateComponents *components = [calendarCurr components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:maxDate];
[components setMonth:-1];
components.day = 1;
NSDate *minDate = [calendarCurr dateFromComponents:components];
NSLog(#"%#", minDate);
EDIT:
Yes, it is duplicate, but answers in that question does not help me. I still get 30/09/2014 (when today is 17/11/2014) instead of 01/10/2014. I suppose that my problem is time of todays date.
Well, Here a new approach:
NSDate *today = [NSDate date];
NSDateFormatter *formatterYear = [[NSDateFormatter alloc] init];
[formatterYear setDateFormat:#"yyyy"];
NSDateFormatter *formatterMonth = [[NSDateFormatter alloc] init];
[formatterMonth setDateFormat:#"MM"];
[formatterMonth setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSString *yearString = [formatterYear stringFromDate:today];
NSString *monthString = [formatterMonth stringFromDate:today];
NSInteger month,year;
if ([monthString integerValue] == 1) {
year = [yearString integerValue] -1;
month = 12;
} else
{
year = [yearString integerValue];
month = [monthString integerValue]-1;
}
NSDateFormatter *exitFormatter = [[NSDateFormatter alloc] init];
[exitFormatter setDateFormat:#"yyyy-MM-dd"];
[exitFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
NSString *stringDate;
if (month < 10) {
stringDate = [NSString stringWithFormat:#"%ld-0%ld-01",(long)year,(long)month];
} else {
stringDate = [NSString stringWithFormat:#"%ld-%ld-01",(long)year,(long)month];
}
NSDate *firstDatePreviousMonth = [exitFormatter dateFromString:stringDate];
NSLog(#"View the date: %#",[firstDatePreviousMonth description]);
Wrong calculation.
Use this code to set Month component.
[components setMonth:components.month -1];
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:#"MM"];
NSString *stringCurrentDate = [dateFormatter1 stringFromDate:currentDate];
int monthInt= [stringCurrentDate intValue];
[dateFormatter1 setDateFormat:#"yyyy"];
NSString *dateString = [dateFormatter1 stringFromDate:currentDate];
int yearInt = [dateString intValue];
monthInt-= 1;
if (monthInt== 0) {
monthInt= 12;
yearInt -=1;
}
dateString = [NSString stringWithFormat:#"%#-01-%#",[#(monthInt)stringValue],[#(yearInt)stringValue]];
NSLog(#"this is previous month first date : %#",dateString);
run it as it is, obviously first day of month will be 01, so this is static here. your can change accordingly. happy coding
Try this:
NSDate *maxDate = [NSDate date];
NSCalendar *calendarCurr = [NSCalendar currentCalendar];
NSDateComponents *components = [calendarCurr components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitTimeZone | NSCalendarUnitSecond | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:maxDate];
[components setMonth:components.month - 1];
[components setDay:1];
NSDate *minDate = [calendarCurr dateFromComponents:components];
NSLog(#">>>>>>>>>>%#", minDate.description);
I have two date string and I wanted to get the in between dates.
For example,
NSString *startDate = #"25-01-2014";
NSString *endDate = #"02-02-2014";
In between dates will be (26-01-2014, 27-01-2014, 28-01-2014.......)
preferably include startDate and endDate as well. Most of the question I managed to find asked for number of days. But I needed it to be actual date. Is there anyway that I can get the in between dates?
NSString *start = #"2010-09-01";
NSString *end = #"2010-12-05";
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"yyyy-MM-dd"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
fromDate:startDate
toDate:endDate
options:0];
NSLog(#"Difference in date components: %d", components.day);
I managed to find this which only returns number of days difference.
NSString *start = #"2010-09-01";
NSString *end = #"2010-12-05";
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"yyyy-MM-dd"];
NSDate *startDate = [f dateFromString:start];
NSDate *endDate = [f dateFromString:end];
NSMutableArray *dates = [#[startDate] mutableCopy];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit
fromDate:startDate
toDate:endDate
options:0];
for (int i = 1; i < components.day; ++i) {
NSDateComponents *newComponents = [NSDateComponents new];
newComponents.day = i;
NSDate *date = [gregorianCalendar dateByAddingComponents:newComponents
toDate:startDate
options:0];
[dates addObject:date];
}
[dates addObject:endDate];
The array dates now contains the list of dates between startDate and endDate, including those, for midnight in the timezone of the device.
Note, that on some timezones this might cause trouble, as the switch from and to Daylight Saving Time might occur at that moment, see WWDC 2011 Video "Session 117 - Performing Calendar Calculations" for further information. One trick is to shift the hour to a save time, i.e. noon, do the calculation and than subtract 12 hours.
Ok, you're most of the way there. You have a date formatter that converts the date strings to NSDates. You have the number of days between the dates. Now you need to loop from the start date for that many days, adding a variable number of days to the start date.
The method you need is dateByAddingComponents:toDate:options:.
Something like this (goes immediately after your code) :
int days = components.day;
NSDateComponents *daysToAdd = [[NSDateComponents alloc] init];
for (int count = 0; count <= days; count++)
{
daysToAdd.day = count;
NSDate *loopDate = [gregorianCalendar dateByAddingComponents: daysToAdd
toDate: startDate
options: 0 ];
NSLog(#"Day %d = %#", count+1, [f stringFromDate: loopDate]);
}
I haven't tested it, but that's the basic idea...
The code above should include both the start and end dates.
If you don't want to include the start date, make the loop start at count = 1 instead of count = 0.
If you don't want to include the end date, make the loop check count < days.
A little late but found a fix to the 'TIMEZONE' time issue that vikingosegundo talked about in his answer. I simply converted the NSDate to an NSString by calling a stringFromDate method on the formatter *f in your for loop...
for (int i = iStart; i < components.day; ++i) {
NSDateComponents *newComponents = [NSDateComponents new];
newComponents.day = i;
newComponents.hour = 0;
newComponents.minute = 0;
newComponents.second = 0;
NSDate *date = [gregorianCalendar dateByAddingComponents:newComponents
toDate:startDate
options:0];
// THIS IS THE CODE I ADDED TO MINE //
// convert NSDate to string calling on *f (NSDateformatter)
NSString *string = [f stringFromDate:date];
// split the string to separate year, month, etc...
NSArray *array = [string componentsSeparatedByString:#"-"];
// create second NSString withFormat and only add the variable from the date that you find pertinent
NSString *sDateNew = [NSString stringWithFormat:#"%#-%#-%#",array[0],array[1],array[2]];
// then simply add the sDateNew string to dates array
[dates addObject:sDateNew];
}
I think this should be put into the comment section of your answer vikingsegundo but it would not let me.
I cannot use a UIDatePicker. I just want to throw that out at the very beginning before all the commenters tell me to just use a UIDatePicker. I want to have a Picker Wheel that shows all the day of the year, leading up to today, and display it as Day: 1/Jan 1, Day: 2/Jan 2 and so on. Here is what I have so far, which detects what day 'number' of the year it is, and populates the PickerWheel with all the dates leading up to today:
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSInteger dc = [currentCalendar ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit
forDate:today];
options = [[NSMutableArray alloc]init];
for (int index = 1; index <= dc; index++)
{
NSString *pickerItemTitle = [NSString stringWithFormat: #"Day: %d", index];
[options addObject: pickerItemTitle];
}
What can I do to make it also match up the NSInteger with the date, and have that be part of the pickerItemTitle as well?
Got it figured out. Added some code to form a date based off integer of day into the loop. Now, the viewWillAppear method that sets the array for the uipickerview looks like this:
- (void)viewWillAppear:(BOOL)animated
{
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSInteger dc = [currentCalendar ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit
forDate:today];
options = [[NSMutableArray alloc]init];
for (int index = 1; index <= dc; index++)
{
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:index];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MMM dd"];
NSString *articleDateString = [formatter stringFromDate:date];
NSString *pickerItemTitle = [NSString stringWithFormat: #"Day: %d/%#", index, articleDateString];
[options addObject: pickerItemTitle];
}
[super viewWillAppear:YES];
}