How to get week startdate and week enddate from iphone calendar - iphone

I am new to iphone.I am doing one project in that project i am struck in the concept of getting week startdate and week enddate of iphone calendar from my application.If anybody know this concept please help me...

Here, I am position code. See it that will help you.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];// you can use your format.
NSString *dateString = [dateFormat stringFromDate:today];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorian components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
int dayofweek = [[[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit fromDate:today] weekday];// this will give you current day of week
[components setDay:([components day]-(dayofweek))];// for beginning of the week.
// or
[components setDay:([components day]+(7-dayofweek))];// for end day of the week
NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
NSDateFormatter *dateFormat_first = [[NSDateFormatter alloc] init];
[dateFormat_first setDateFormat:#"yyyy-MM-dd"];
NSString *dateString2 = [dateFormat stringFromDate:beginningOfWeek];
Hope, this will help you...

This will help you
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);
[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];
components = [cal components:NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[[NSDate alloc] init]];
[components setDay:([components day] - ([components weekday] - 1))];
NSDate *thisWeek = [cal dateFromComponents:components];
[components setDay:([components day] - 7)];
NSDate *lastWeek = [cal dateFromComponents:components];
[components setDay:([components day] - ([components day] -1))];
NSDate *thisMonth = [cal dateFromComponents:components];
[components setMonth:([components month] - 1)];
NSDate *lastMonth = [cal dateFromComponents:components];
NSLog(#"today=%#",today);
NSLog(#"yesterday=%#",yesterday);
NSLog(#"thisWeek=%#",thisWeek);
NSLog(#"lastWeek=%#",lastWeek);
NSLog(#"thisMonth=%#",thisMonth);
NSLog(#"lastMonth=%#",lastMonth);
from Here

Related

Getting next date and previous day date

I basically want to convert a particular date (x) to the previous day date(x - 1 day) and also to the next day date (x + 1 day). I am using the following code for this :
NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)];
However I have my date (x) in NSString format, and I need to convert the NSString(myDateString) to NSDate(myDate) before applying the above code.
I have a NSString containing date in format MM-dd-yyyy.
For converting I am using the following code , but I am getting absurd values.
NSLog(#"myDateString=%#",myDateString);//output:myDateString=10-25-2012//all correct
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM-dd-yyyy"];
NSDate *myDate=[formatter dateFromString:myDateString];
NSLog(#"myDate=%#",myDate);//output:myDate=2012-10-24 18:30:00 +0000 // I only want the date to be shown // and why has the format changed
NSDate *datePlusOneDay = [currentDate dateByAddingTimeInterval:(60 * 60 * 24)];
NSLog(#"datePlusOneDay=%#",datePlusOneDay);//output:datePlusOneDay=2012-10-25 18:30:00 +0000// I only want the date to come , not time // and why has the format changed
Later again I need to convert the NSDate to NSString
NSString *curentString=[formatter stringFromDate:datePlusOneDay];
NSLog(#"curentString=%#",curentString); //output:curentString=10-26-2012
Similarly I also want to get the previous date.
Please help guys !! and ho ho MERRY CHRISTMAS !!
Do as: componets.day = 1 to obtain the next, -1 for the previous day.
NSDate *date = [NSDate date]; // your date from the server will go here.
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *newDate = [calendar dateByAddingComponents:components toDate:date options:0];
NSLog(#"newDate -> %#",newDate);
The below code should get you the previous date:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
[offsetComponents setDay:-1]; // replace "-1" with "1" to get the datePlusOneDay
NSDate *dateMinusOneDay = [gregorian dateByAddingComponents:offsetComponents toDate:myDate options:0];
Merry Xmas :)
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:( NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit ) fromDate:[[NSDate alloc] init]];
[components setHour:-[components hour]];
[components setMinute:-[components minute]];
[components setSecond:-[components second]];
NSDate *today = [cal dateByAddingComponents:components toDate:[[NSDate alloc] init] options:0]; //This variable should now be pointing at a date object that is the start of today (midnight);
[components setHour:-24];
[components setMinute:0];
[components setSecond:0];
NSDate *yesterday = [cal dateByAddingComponents:components toDate: today options:0];
Try this simple solution for previous date:-
NSDate *datePlusOneDay = [[NSDate date] dateByAddingTimeInterval:-(60 * 60 * 24)];
NSLog(#"datePlusOneDay=%#",datePlusOneDay);
Swift 5 Solution
let calendar = Calendar.current
calendar.date(byAdding: .day, value: -2, to: Date())
function for previousDay
-(void)previousDay
{
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
dateString = [dateFormat stringFromDate:today];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
comps.day =-1;
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
EndingDate = [dateFormat stringFromDate:sevenDays];
}
function for next day
-(void)nextDay
{
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
dateString = [dateFormat stringFromDate:today];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [NSDateComponents new];
comps.day =1;
NSDate *sevenDays = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0];
[dateFormat setDateFormat:#"dd/MM/yyyy"];
EndingDate = [dateFormat stringFromDate:sevenDays];
}

disply the current week start and end date

I want to display the Current week in UINavigationBar and then click on Next or Previous button to display the Previous week or Next week
Example:
July 22 - July 28
July 29 - Auguest 4
To get first date of the week you can do something like this :
- (NSDate *)firstDayOfWeekFromDate:(NSDate *)date {
CFCalendarRef currentCalendar = CFCalendarCopyCurrent();
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:date];
[components setDay:([components day] - ([components weekday] - CFCalendarGetFirstWeekday(currentCalendar)))];
//[components setDay:(CFCalendarGetFirstWeekday(currentCalendar))];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
CFRelease(currentCalendar);
return [CURRENT_CALENDAR dateFromComponents:components];
}
After getting first week of the day you can calculate days as suggested in above answer by #vikas
:)
NSDate * selectedDate = [NSDate date];
-(IBAction)Week_CalendarActionEvents:(id)sender{
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *offsetComponents = [[[NSDateComponents alloc] init] autorelease];
NSDate *nextDate;
if(sender==Week_prevBarBtn) // Previous button events
[offsetComponents setDay:-7];
else if(sender==Week_nextBarBtn) // next button events
[offsetComponents setDay:7];
nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:selectedDate options:0];
selectedDate = nextDate;
[selectedDate retain];
NSDateComponents *components = [gregorian components:NSWeekCalendarUnit fromDate:selectedDate];
NSInteger week = [components week];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MMMM YYYY"];
NSString *stringFromDate = [formatter stringFromDate:selectedDate];
[formatter release];
[Week_weekBarBtn setTitle:[NSString stringWithFormat:#"%#,Week %d",stringFromDate,week]];
}

error getting time from phone in xcode 4?

I trying to fetch the time from the phone's clock but am not able to fetch using this code
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];
Use This
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components =
[gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];
You missed a line in the code.

NSDate, NSCalendar and NSDateComponents timing

NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"GMT+2"]];
NSDateComponents *dateComponents = [calendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [dateComponents day];
NSInteger month = [dateComponents month];
NSInteger year = [dateComponents year];
NSLog(#" %i %i %i ", day, month, year);
That code show me "12 2147483647 2147483647"
How can I get the month and year (integer)
How can add/forward one day? (if we are the first of the month too!)
Thant you for your attention :-)
NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSDate *date = [NSDate date];
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
NSInteger year = [dateComponents year];
NSInteger month = [dateComponents month];
NSInteger day = [dateComponents day];
NSInteger hour = [dateComponents hour];
NSInteger minute = [dateComponents minute];
NSInteger second = [dateComponents second];
[calendar release];
hope this helps
Add more units to NSDayCalendarUnit | NSWeekdayCalendarUnit - you are not requesting month or year, therefore they are invalid!
Add/subtract a NSDateComponents object configured for 1 day and [NSCalendar dateByAddingComponents:toDate:options:]. Alternatively, and probably just as correct, use [NSDate dateWithTimeInterval:sinceDate:]

How to create a custom date on iPhone

I'm trying to set some components of todays date with NSDateComponent like so:
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
[comps setHour:1];
[comps setMinute:44];
NSCalendar *cal = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *date = [cal dateByAddingComponents:comps toDate:[NSDate date] options:0];
[comps release];
NSLog(#"%#", date);
But this example will ADD the time to the current date. Now what I want to do is ADD one day but set the hour and minute to the specified values (no adding). How can I do this?
This worked in the end:
NSDateComponents *comp = [[NSCalendar currentCalendar] components:NSYearCalendarUnit
NSMonthCalendarUnit | NSDayCalendarUnit fromDate:[NSDate date]];
[comp setDay:[comp day] +1];
[comp setHour: 12];
[comp setMinute: 00];
NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comp];
Easy: Just use the date property...
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
[comps setHour:1];
[comps setMinute:44];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
Edit: forgot the calendar. From the manual (1st page of NSDateComponents in bold):
Important: An NSDateComponents object is meaningless in itself; you need to know what calendar it is interpreted against, and you need to know whether the values are absolute values of the units, or quantities of the units.
There's also an example of how to use it.