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:]
Related
I want to fire a notification at 10PM(irrespective of country) every week. Do i need to use timeZone. Currently I am using below code to fire notification for every week but there how to fire notification exactly at 10PM.
NSDate *date = [NSDate date];
NSDate* myNewDate = [date dateByAddingTimeInterval:7*24*60*60];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = myNewDate;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.repeatInterval = NSWeekCalendarUnit;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[localNotification release];
I would think that setting your fireDate to 22:00 on the day you want to get the notification (rather than "a week from now") would do what you want. You'll probably want to use NSCalendar to do that.
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *currentDate = [NSDate date];
NSDate *fireDate = nil;
[dateComponents setDay:3]; // ...or whatever day.
[dateComponents setHour:22];
[dateComponents setMinute:0];
fireDate = [gregorian dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
[localNotification setFireDate:fireDate];
[dateComponents release];
I made these changes and it worked...
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
NSDate *currentDate = [NSDate date];
NSDate *fireDate = nil;
//Getting current Hour
NSDateComponents *components = [[NSCalendar currentCalendar] components:(kCFCalendarUnitHour | kCFCalendarUnitMinute) fromDate:currentDate];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
[dateComponents setDay:7];//After 1 week
[dateComponents setHour:22-hour-1]; //Calculating Remaining time for 10PM && "-1" because i am adding 60 min
[dateComponents setMinute:60-minute];
//Adding remaining time to current Time
fireDate = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents
toDate:currentDate
options:0];
[localNotification setFireDate:fireDate];
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]];
}
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
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.
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.