Local push notification last day of month - nsdate

I want to send local push notification last day of every month 12.00.
I want someone to check if this is the right code since I don't wanna wait a month to see if it works? Thnx!
NSDate *curDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate];
comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
NSDate *fireTime;
[comps setMonth: [comps month] + 1];
[comps setDay: 0];
[comps setHour: 14];
[comps setMinute: 0];
NSDate *lastDayMonth = [calendar dateFromComponents:comps];
NSLog(#"%#", lastDayMonth);
fireTime = lastDayMonth;
localNotif.repeatInterval = NSMonthCalendarUnit;
localNotif.fireDate = fireTime;
localNotif.alertBody = [NSString stringWithFormat: #"working?"];

Well, obviously you should set the Hour as 12 if you want the notification to be fired at 12:00...
Why not try Hours or Days first. If they work, the Month should do as well.

Related

NSdate repeating date for upcomming sunday

I am trying to create a date for next Sunday, and make it repeat for every Sunday after.
I know how to create the date using components, and components setWeek is depreciated, so I don't know how to do it now.
if You got the First Sunday Then You Should Do this To repeat it after seven Days!
NSString *sunday = #"Here Your Sunday Date";
NSDate *dateSunday = [self dateFromString:sunday];
[self GetNextSunday:dateSunday];
Then Call This Method To Get Next Sunday
-(void)GetNextSunday:(NSDate*)date
{
NSDate *NextSunday = [[NSDate alloc]init];
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components = [gregorian components:( NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
components.day = components.day + 8;
NextSunday = [gregorian dateFromComponents:components];
NSLog(#"%#",NextSunday);
}
This Will Return Next 7th Day ! I hope this Will Help You.
for Any Query Comment Below!

Formatting Date from server time to local time using NSTimeZone and Dateformatter

I am receiving a date/time as a NSString from my server where I am converting that time into a NSDate to the users local time using NSTimeZone. After which I try to reformat this NSDate into a better more readable NSString using the new NSDateFormatter format, however when I try to apply this new format it reverts the resulting dateString back to the original Server time.
I would like to know what I am doing wrong, I would like to show the converted time in the new format.
this is the code I am using
// set date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
// change time to systemTimeZone
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:timeZone];
NSDate *localTime = [dateFormatter dateFromString:[singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]];
// reformat converted Time to readable format
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"dd/MM/yy - hh:mm a"];
NSString *dateWithNewFormat = [dateFormat1 stringFromDate:localTime];
NSLog(#"TimeZone - %#", timeZone);
NSLog(#"UTC ServerTime - %#", [singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]);
NSLog(#"UTC to deviceTimeZone - %#", localTime);
NSLog(#"NewFormat - %#", dateWithNewFormat);
This is an example of my output
TimeZone - Pacific/Auckland (NZST) offset 43200
UTC ServerTime - 2013-08-22 01:45:59
UTC to deviceTimeZone - 2013-08-21 13:45:59 +0000
NewFormat - 22/08/13 - 01:45 AM
any help would be greatly appreciated
The NSDateFormatter that reads the date must be set to the timezone that the date you are parsing is in, in your case, it is UTC. The date formatter will then be able to produce an NSDate object (which represents a specific moment in time regardless of timezones). You can then give that NSDate object to another NSDateFormatter that is configured to format dates in a specific time zone.
// set date format
NSDateFormatter *dateParser = [[NSDateFormatter alloc] init];
dateParser.dateFormat = #"yyyy-MM-dd HH:mm:ss";
dateParser.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
NSDate *specificMomentInTime = [dateParser dateFromString:[singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]];
// reformat converted Time to readable format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd/MM/yy - hh:mm a";
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:specificMomentInTime];
NSLog(#"UTC ServerTime - %#", specificMomentInTime);
NSLog(#"NewFormat - %#", dateWithNewFormat);

NSDateFormatter reports June 2, 2013 as being in week zero

I am using NSDateFormatter to convert a series of dates to a week number within a month.
The date formatting code I am using is yyyyMMW and everything I have read tells me that W will be between 1-5.
But, the 2nd of June 2013 fell on a Sunday (the default start day of the week in the gregorian calendar) and it's week number is reported as 0 even though the start date of the week is calculated correctly:
2013-06-03 14:15:45.611 date=20130531, week=2013055, start of week=20130526
2013-06-03 14:15:45.612 date=20130602, week=2013060, start of week=20130602
2013-06-03 14:15:45.612 date=20130603, week=2013061, start of week=20130602
Some quick and dirty test code to reproduce the log shown above:
NSDateFormatter *dateFormatDaily = [[NSDateFormatter alloc] init];
[dateFormatDaily setDateFormat:#"yyyyMMdd"];
NSDateFormatter *dateFormatterWeekly = [[NSDateFormatter alloc] init];
[dateFormatterWeekly setDateFormat:#"yyyyMMW"];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setFirstWeekday:1]; // default but set here for clarity
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setMonth:5];
[dateComponents setDay:31];
[dateComponents setYear:2013];
NSDate *date_1 = [calendar dateFromComponents:dateComponents];
[dateComponents setMonth:6];
[dateComponents setDay:2];
NSDate *date_2 = [calendar dateFromComponents:dateComponents];
[dateComponents setDay:3];
NSDate *date_3 = [calendar dateFromComponents:dateComponents];
NSArray *datesToTest = #[date_1, date_2, date_3];
for (NSDate *date in datesToTest) {
NSString *weekNo = [dateFormatterWeekly stringFromDate:date];
NSDate *beginningOfWeek = nil;
BOOL rc = [calendar rangeOfUnit:NSWeekCalendarUnit startDate:&beginningOfWeek interval:NULL forDate:date];
if (rc) {
NSLog(#"date=%#, week=%#, start of week=%#", [dateFormatDaily stringFromDate:date], weekNo, [dateFormatDaily stringFromDate:beginningOfWeek]);
} else {
NSLog(#"Could not calculate beginning of week");
}
}
Any ideas? A week number of 0 under any circumstances seems wrong to me.
Thanks
There are various parameters that cause this effect. First of all, you did not set a calendar for the date formatter. If you add
[dateFormatterWeekly setCalendar:calendar];
to your code, then the output will be as you expected:
date=20130531, week=2013055, start of week=20130526
date=20130602, week=2013062, start of week=20130602
date=20130603, week=2013062, start of week=20130602
But in your case, the date formatter uses the current calendar, and therefore has separate parameters firstWeekDay and minimumDaysInFirstWeek. These parameters are locale dependent. If I test this on the iOS Simulator with the "Region Format" set to "German -> Germany", then
[[dateFormatterWeekly calendar] firstWeekday] = 2
[[dateFormatterWeekly calendar] minimumDaysInFirstWeek] = 4
and I assume that you will have similar values, because now I get the same output as you.
Now for the date formatter, the week starts on a Monday, which means that June 2 is in the week starting at May 27. This counts as "week #0" in June, because only one day of this week is in June, but minimumDaysInFirstWeek = 4. The first week in a month that has at least
minimumDaysInFirstWeek days, counts as "week #1".
(I found the relevance of the minimumDaysInFirstWeek parameter here:
http://www.cocoabuilder.com/archive/cocoa/326845-week-of-month-confusion.html)

Wrong year set when converting NSDate to NSString

I'm trying to produce a string using a NSDate category in this way:
NSString* dateString = nil;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocal currentLocale]];
[dateFormat setDateFormat:#"dd LLL YYYY"];
dateString = [dateFormat stringFromDate:self];
return dateString;
The conversion works fine except in ONE case (I report the debug session):
if I try to convert an NSDate object like this:
(gdb) po self
2012-01-01 00:00:00 +0000
I obtain:
(gdb) po dateString
01 Jan 2011
Why the year is set back to 2011????
PS. I have already checked NSDate returns wrong year and I'm NOT using the Japanese calendar.
thanks a lot
Try this:
NSDate *pickerDate = [NSDate date];
NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents* components = [[[NSDateComponents alloc] init] autorelease];
components.day = 0; //This value to take from today to next 1 or 2 or 3 days
NSDate* newDate = [calendar dateByAddingComponents: components toDate: pickerDate options: 0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMMM"];
NSString *textDate = [NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:newDate]];
[dateFormatter release];

how can I add one minutes in current NSDate of iphone and save in NSDate?

I can get current date of iphone via this code as
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *dateString = [dateFormatter1 stringFromDate:currentDate];
Now I just want to increase one mint then save NSString format,
how can I?
You can use dateByAddingTimeInterval.
NSDate *currentDate = [NSDate date];
NSDate *datePlusOneMinute = [currentDate dateByAddingTimeInterval:60];
//60 seconds
Or even shorter:
Objective-C
//60 seconds
[NSDate dateWithTimeIntervalSinceNow:60];
Swift
//60 seconds
NSDate(timeIntervalSinceNow: 60)

Resources