I am trying to set repeating notifications for every saturday and sunday but for some reason the date is coming out wrong.This is my code
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekOfMonth |NSCalendarUnitWeekOfMonth;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
NSDateComponents *dt = [[NSDateComponents alloc]init];
comps.hour = hour;
comps.minute = [[goal.reminderTimeInfo valueForKey:#"minutes"] intValue];
[dt setMonth:[comps month]];
[dt setYear:[comps year]];
[dt setWeekOfMonth:[comps weekOfMonth]];
comps.weekday = 1; //Sunday
[dt setWeekday:[comps weekday]];
NSDate *sundayDate = [calendar dateFromComponents:dt];
Its suppose to give out
2015-07-05 19:00:00 +0000
but its giving
2015-07-04 19:00:00 +0000
And if give the local notification a particular date.For example its sunday.Will it fire every sunday?
You did not change the hour and the minutes for dt but for comps.
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear | NSCalendarUnitWeekOfMonth |NSCalendarUnitWeekOfMonth;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
NSDateComponents *dt = [[NSDateComponents alloc]init];
dt.hour = 19;
dt.minute = 00;
[dt setMonth:[comps month]];
[dt setYear:[comps year]];
[dt setWeekOfMonth:[comps weekOfMonth]];
[dt setWeekday:1];
NSDate *sundayDate = [calendar dateFromComponents:dt];
Related
I have searched for the solution to find the date of the sunday of the particular week.I have found questions that have been answered but it doesnt show the correct date of sunday.
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
comps.weekday = 1; //Sunday
comps.weekOfMonth = 1;
NSDate *sundayDate = [calendar dateFromComponents:comps];
When i print out the date it gives this.
2015-07-08 10:14:00 +0000
Its the current date.What am i doing wrong?
hope this helps you :
NSDate *today = [NSDate date];
NSCalendar *calenderObj = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
calenderObj.firstWeekday = 1; // Sunday = 1,
// NSDateComponents *components = [calenderObj components:NSWeekOfYearCalendarUnit fromDate:today];
NSDate *objDate = nil;
[calenderObj rangeOfUnit:NSWeekCalendarUnit startDate:&objDate interval:NULL forDate:today];
NSLog(#" === %#", objDate);
Output :
=== 2015-07-15 10:26:00 +0000
I am attempting to grab a date using a specified timezone name, for example "America/New_York". For some reason, when I choose my correct timezone "America/Chicago", it shows the correct time (e.g. 9:41PM). However, when I choose New York, it should show an hour ahead (expecting 10:41PM), but I get an hour behind my current time (what I get: 8:41PM).
- (NSDate*)dateFromTimezone:(NSTimeZone*)timezone
{
timezone = ( timezone == nil ? [NSTimeZone timeZoneWithName:CUSTOMTIMEZONE] : timezone );
NSDate * date = [NSDate date];
NSCalendarUnit units = NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:timezone];
[gregorian components:units fromDate:date];
NSDateComponents* dateComponents = [[NSCalendar currentCalendar] components:units fromDate:date];
[dateComponents setTimeZone:timezone];
[dateComponents setCalendar:gregorian];
return [gregorian dateFromComponents:dateComponents];
}
I am working on a Calender app and it's working fine when i navigate to next month as i am able to get the last day of the month and from where i am able to get the first day of coming month .but when i navigate to previous month i m not able to get the last month's first day.Is there any way to get first day of last month.
The following works fine for me
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *currentDate = [NSDate date];
NSDateComponents *components = [calendar components:(NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:currentDate];
components.month = components.month - 1;
components.day = 1;
NSDate *newDate = [calendar dateFromComponents:components];
This also works if your current month is january. NSDateComponents will automatically decrement the year and set the month to december.
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear
fromDate:[NSDate date]];
components.day = 1;
components.month = components.month - 1;
NSDate *lastmonthDay1 = [[NSCalendar currentCalendar] dateFromComponents: components];
NSLog(#"First day of last month====: %#", [lastmonthDay1 descriptionWithLocale:[NSLocale currentLocale]]);
You can do something like below....
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDateComponents *comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
[comp setDay:1];
[comp setMonth:CurrentMonth];//previous month
[comp setYear:currentYear];
NSDate *firstDayOfMonthDate = [gregorian dateFromComponents:comp];
NSCalendar *cal1=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps1 = [cal1 components:NSWeekdayCalendarUnit fromDate:firstDayOfMonthDate];
// 1 for mon 8 for sun
int startWithDay=[comps1 weekday]==1?8:[comps1 weekday];
//1 for monday
// 2 for tuesday and so on....
Let me know it is working or not!!!
Happy Coding!!!!
Is there any easier way to assign only hour and minute from one NSDate A to NSDate B, but keep B's Year Month and Day?
I don't know If it's an easier way, but it's the correct way:
NSDate *dateA = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSUInteger timeComps = (NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSTimeZoneCalendarUnit);
NSDateComponents *comps = [gregorian components:timeComps fromDate:dateA];
[components setHour: 3];
[components setMinute: 42];
NSDate *newDateB = [gregorian dateFromComponents: components];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSCalendarUnit *dateComponent = (NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit | NSWeekdayOrdinalCalendarUnit);
NSDateComponents *components1 = [calendar components:dateComponent fromDate:dateA];
NSInteger day = components1.day;
NSInteger month = components1.month;
NSInteger year = components1.year;
NSDateComponents *components2 = [calendar components:dateComponent fromDate:dateB];
NSInteger hour = components2.hour;
NSInteger minute = components2.minute;
NSInteger second = components2.second;
NSDateComponents *requiredComponent = [[NSDateComponents alloc] init];
requiredComponent.day = day;
requiredComponent.month = month;
requiredComponent.year = year;
requiredComponent.hour = hour;
requiredComponent.minute = minute;
requiredComponent.second = second;
NSDate *date = [calendar dateFromComponents:requiredComponent];
NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[NSLocale currentLocale]];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"US/Central"]];
NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSWeekCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:today];
[nowComponents setHour:hour];
[nowComponents setMinute:0];
[nowComponents setSecond:0];
NSLog(#"%#", [calendar dateFromComponents:nowComponents]);
logs 2012-01-01 06:00:00 +0000. The time is correct based on the timezone setting, but today is July 20th... What am I doing wrong?
You need to extract the year, month, and day components, and construct a new NSDate, like this:
NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setLocale:[NSLocale currentLocale]];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:#"US/Central"]];
NSDateComponents *nowComponents = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate:today];
today = [calendar dateFromComponents:nowComponents];
You do not need to get the time components and then set them back to zero: you can keep them at their defaults by not asking to extract them in the first place.