I would like to get a date from an amount of months plus a date.
as an example.
I would like the output to be:
start date: 4th april
amount of months: 5
output date: 4th september
Is there any code to do this?
Thanks
If you have a read of Apple's Date and Time Programming Guide there are a few solutions that should present themselves.
For example, you could create a base NSDate and then use the NSCalendar dateByAddingComponents:toDate:options: method to add the relevant number of months as a component.
If you're after some clean sample source code, see the "Adding Components to a Date" section within the Calendrical Calculations section of the above programming guide.
Yes there is, have a look at Apple's Date and Time Programming Guide.
Related
I want to have a function to return the next business day of the date that is sent in as parameter.
Weekends and Canadian holidays should be included.
What is the easiest way to do this ? Is it possible to do this without setting a reference to a range of cells for the holidays' dates?
Unless you provide a list of holidays, you won't be able to calculate the next working date. Remember the rules for Easter involve the phase of the moon! So this is not a simple task.
That is before you start to consider which different countries. Even if you restrict yourself to the US holidays, it takes a fair bit of calculations just to get the 3rd Thursday in November etc
Here is a demonstration of how you could calculate the next working day.
I wouldn't hold just the holidays but every date in the calendar. Note for Easter I have fudged the holiday column to get the correct next working day answer.
You could miss a few steps in the calculation, I left them in for clarity.
Use the =WORKDAY() function. You'll have to specify a range of holidays as the third parameter.
Is it possible to set the DatePickerMode to just the Day of the Week (Monday - Sunday) and MM/dd/yyyy format? I can see the days of the week when I am using UIPickerModeDateAndTime, but in this case I dont need to select Time, Just the Dates with the days (if Monday-Sunday).
In other words, combining this two Modes (minus the time option)
There's no way of customizing the UIDatePicker apart from that, but you can use a customized UIPickerView for it:
UIPickerView
Find an example of using it here:
http://www.techotopia.com/index.php/An_iOS_5_iPhone_UIPickerView_Example
I have to create "holiday" table and then create php script so I could show it on my site.
Holidays can be specific, like 15.05.2012 - 15-th of the may.
And non-specific: First(or second, third) sunday of july
Is there any way to create calculated column, so this phrase "First(or second, third) sunday of july", could turn into x.07.2012.
Use a calendar table. There is no magic code built into SQL Server that knows when Easter is. This article shows the basic premise - you fill up a table with all the dates from year x to year y, then you update a column called IsHoliday for the dates that are holidays based on specific logic (easiest to do this once, in a loop, then all your code later can refer to the calculated bit):
ASP Faq reference. The current link no longer works, this is the archive.org cached version of the page
The link in the answer now takes you to a bogus page that wants to load a virus. Just heads up.
http://codeinet.blogspot.com/2006/08/auxiliary-calendar-table-for-sql.html
This seems to be a working version.
I have a date and I want to increment it by 1 month. How can I do this?
I'm new to iPhone programming. Can this be done or do I have to implement it myself?
You really need to use an NSCalendar for this type of activity, as this has methods like -dateByAddingComponents:toDate:options: which will let you add "components" (NSDateComponents that represent 1 month or 1 year, etc. for example) onto an existing calendar date.
For some general background reading, you might also want to take a look at the Date and Time Programming Guide as it covers some of this, albeit quite briefly.
I have an entity "Event" witch contains two properties: date (NSDate) and repeat (NSInteger - 0 = NONE, 1 = DAILY, 2 = WEEKLY, 3 = MONTHLY, ...).
Does anyone knows how can I filter events by repeats passing a date ?
Example:
First event: 01-01-2010 / weekly
Second event: 10-02-2010 / monthly
Current date: 10-06-2010
Request:
Get all events where Event.date == "Current date" OR Event.date.day == "Current date".day if Event.repeat == monthly
Returned event:
Second event
I hope someone understand what I'm trying to explain :s
The problem here is that NSDate doesn't have a day (or any other similar) property. If it did, your predicate would work as written.
Data and time programming is deceptively complex under the hood. For example, in common usage, the phrase "same date" means the exact same calendar day. However, from the codes perspective it also means the same week, month and year because days are no more significant to code than any other arbitrary calendar division. Even in ordinary usage, "day" can refer to a specific range of hours e.g. Saturday, August 21 2010 or it can refer to any arbitrary range of 24 hours as in, "within a day." Which one do you need for this app?
NSDate is really an object wrapper around a microsecond accurate timestamp. It has methods for converting to strings and for creating and comparing timestamps but it doesn't understand calendar attributes such seconds, minutes, hours, days, weeks, month, or years. That is what NSCalendar is for and Core Data does not innately support that class as a data type as doing so for all possible calendars would be to complex.
If calendar attributes are required in a model, you need to create an custom entity that models a calendar date. Set the entities attributes to calendar attributes you need to model and then a relationship to the object that needs the calendar date as a property e.g.
CalendarDate{
date:NSDate
minute:int
hour:int
day:int
month:int
year:int
events<--(required,nullify)-->>Event.date
}
You can create a custom class for the entity and provide a method that automatically populates the object based on the passed NSDate and any calendar you choose.
Now your predicate is easy.
NSPredicate *myPred;
myPred=[NSPredicate predicateWithFormat:#"(date.date==%# or date.day==%i) AND repeat=%i", currentDate, 45, kMonthly];
This seems like it is cumbersome but it is required owing to the true complexity of calendar dates. There simply isn't an easy way to handle date and time calculations and comparisons for all uses.
I am not certain this can be done at the SQLite level. It certainly can be done once your events are pulled into memory but that, I suspect, defeats your goal.
There might be some clever way to de-normalize the data and thereby create a situation that can be filtered.
For example, if you had the day of month and month pulled out into integer fields you might be able to devise a way to determine based on those if things align. It is not coming to me directly but I would definitely look in that direction for a solution.
Another alternative, one that calendars tend to use, is to create dependent events for the specific dates coming up. When the event is created you create all of the dependent events so that you are just filtering on a date. Nasty to be sure.
Last option is to pull all events into memory and calculate from there.