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];
}
Related
here I used one label at top to display the today month with year. And also I have 6 label which will display the todays date like say 31dec to 5 Jan. Here is the code:
This will display the todays date from next 5 date
NSArray *labelArray = #[flabel, slabel, tlabel, folabel, fivlabel,sixlabel];
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = #"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:today options:nil];
UILabel *label = (UILabel *)labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
}
This will display the current month with year
// show the present month
NSDate *date = [NSDate date];
// NSDateFormatter *date = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat=#"MMMM";
NSString * monthString = [[dateFormatter stringFromDate:date] capitalizedString];
dateFormatter.dateFormat=#"yyyy";
NSString * yearString = [[dateFormatter stringFromDate:date] capitalizedString];
dateLabel.text = [NSString stringWithFormat:#"%# ,%#",monthString,yearString];
Important:
what I need is I need to display like below image:
The current date should display at last and should show previous 5 dates before.
I have two button at left and right. when ever user press right button like say now its 31Dec. And when user press right it should change to 1Jan and should display the previous 5 date from Jan 1. And also the month .label should automatically change to Jan,2016. Like below image:
Two button action:
- (IBAction)calRight:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
}
- (IBAction)calLeft:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:-1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
}
This code I did for changing only month I tried. But no changing my date label.
Please explain me in code.
Full Edited:
- (void)viewDidLoad {
[super viewDidLoad];
NSDate *firstLabelDate = [NSDate date]; //make it class variable
NSDate *lastLabelDate = [NSDate date]; //make it class variable
NSArray *labelArray = #[flabel, slabel, tlabel, folabel, fivlabel,sixlabel]; //make it class variable
[self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
}
- (IBAction)calRight:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
[self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
}
- (IBAction)calLeft:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:-1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
[self updateDateLabelsGoingForward:NO andFromDay:self.firstLabelDate];
}
- (void)updateDateLabelsGoingForward:(BOOL) goForward andFromDay:(NSDate*) dayFrom {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = #"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
int dateToBeModified = goForward?1:-1;
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:dateToBeModified toDate:dayFrom options:nil];
UILabel *label = (UILabel *)self.labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if(i==5){
self.lastLabelDate = nextDate;
}
if(i==0){
self.firstLabelDate = nextDate;
}
}
}
Define one Date object globally NSDate *firstdate; and assign its value in viewDidLoad firstdate = [NSDate date];
now use following method to show your dates & month call this method from viewDidLoad [self dateChange];
-(void)dateChange
{
NSArray *labelArray = #[flabel, slabel, tlabel, folabel, fivlabel,sixlabel];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = #"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:i toDate:firstdate options:nil];
UILabel *label = (UILabel *)labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if (i==5) {
dateFormatter.dateFormat=#"MMMM";
NSString * monthString = [[dateFormatter stringFromDate:nextDate] capitalizedString];
dateFormatter.dateFormat=#"yyyy";
NSString * yearString = [[dateFormatter stringFromDate:nextDate] capitalizedString];
dateLabel.text = [NSString stringWithFormat:#"%# ,%#",monthString,yearString];
}
}
}
now set your calRight and calLeft as bellow
- (IBAction)calRight:(id)sender {
firstdate = [NSDate dateWithTimeInterval:86400 sinceDate:firstdate];
[self dateChange];
}
- (IBAction)calLeft:(id)sender {
firstdate = [NSDate dateWithTimeInterval:-86400 sinceDate:firstdate];
[self dateChange];
}
First create an array which will store date's for 6 days...NSArray *dates.
Then initialise it with [NSDate date] at dates[0].
Use NSDateComponents and subtract the date component and store the previous 5 days in the array from dates[1.....4].
On click of the button you can increment or decrement the entire array and display the dates all again....
Pretty simple actually you are almost there, add one more variable that will hold the last date displayed in the last label, let us not mess up by taking the date out of the label via NSDateFormatter etc. long route!
NSDate *firstLabelDate = [NSDate date]; //make it class variable
NSDate *lastLabelDate = [NSDate date]; //make it class variable
NSArray *labelArray = #[flabel, slabel, tlabel, folabel, fivlabel,sixlabel]; //make it class variable
[self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
And your button actions should be modified as below,
- (IBAction)calRight:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
[self updateDateLabelsGoingForward:YES andFromDay:self.lastLabelDate];
}
- (IBAction)calLeft:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMMM,yyyy"];
NSDate *tempDate = [dateFormat dateFromString:dateLabel.text];
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:-1];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *newDate = [calendar dateByAddingComponents:dateComponents toDate:tempDate options:0];
dateLabel.text = [dateFormat stringFromDate:newDate];
[self updateDateLabelsGoingForward:NO andFromDay:self.firstLabelDate]
}
Add a common method to reload your date labels.
- (void)updateDateLabelsGoingForward:(BOOL) goForward andFromDay:(NSDate*) dayFrom {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSCalendar *calendar = [NSCalendar currentCalendar];
dateFormatter.dateFormat = #"ddMMM";
for (NSInteger i = 0; i < 6; ++i) {
int dateToBeModified = goForward?(1*i):(-1*i);
NSDate *nextDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:dateToBeModified toDate:dayFrom options:nil];
UILabel *label = (UILabel *)self.labelArray[i];
label.text = [dateFormatter stringFromDate:nextDate];
if(i==5){
self.lastLabelDate = nextDate;
}
if(i==0){
self.firstLabelDate = nextDate;
}
}
}
Hope this helps, and this could help you in the longer run.
I wish to get the following result:
set arr[0]=jan23,2016 …… upto arr[6] =jan29,2016.
For that I have the following code:
// To get future or next 6 days from now or current date
NSCalendar * calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents * datecomponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDayfromDate:[NSDate date]];
for(int i=0;i<6;++i)
{
NSDate * todaydate = [calendar dateFromComponents:datecomponents];
NSLog(#"%#",todaydate);
++datecomponents.day;
NSDateFormatter * dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateStyle:NSDateFormatterMediumStyle];
NSString * convertedstring = [dateformatter stringFromDate:todaydate];
NSLog(#“converteddate:%#",convertedstring);
For this I got a response like the following:
converteddate:Jan 23, 2016 similarly upto jan29,2016
Then I do like this:
NSMutableString * newstring = [[NSMutableString alloc]init];
[newstring stringByAppendingString:convertedstring];
NSLog(#"%#",newstring);
And also like this:
NSMutableArray * arrdata;
arrdata = [[NSMutableArray alloc]init];
[arrdata addObject:convertedstring];
NSLog(#"%#",arrdata);
}
How to get the above result set.after iteration of strings how to convert those into arrays.
The array must be declared and allocated outside of the for loop as you are resetting it every iteration:
NSMutableArray * arrdata = [[NSMutableArray alloc]init];
NSCalendar * calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents * datecomponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDayfromDate:[NSDate date]];
for(int i=0;i<6;++i)
Also there is no need to do this, as you never really use newstring anyway:
NSMutableString * newstring = [[NSMutableString alloc]init];
[newstring stringByAppendingString:convertedstring];
Here is the full answer:
NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
NSDateComponents *datecomponents = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDayfromDate:[NSDate date]];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc]init];
[dateformatter setDateStyle:NSDateFormatterMediumStyle];
NSMutableArray *arr = [[NSMutableArray alloc] init];
for(int i = 0; i < 6; i++)
{
NSDate * todaydate = [calendar dateFromComponents:datecomponents];
NSLog(#"%#",todaydate);
++datecomponents.day;
NSString *convertedstring = [dateformatter stringFromDate:todaydate];
[arr addObject:convertedstring];
}
Hope this helps!
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 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);
}