Add one month to current date - blackberry

I want to add 1 month to date, but it is adding only 4 weeks.
I tried like this,
2012-05-04 + DateTimeUtilities.ONEMONTH = 2012-05-31
The result i am getting is 2012-05-31
I want to add a full month (30 days or 31, or when month is a leapyear 29 or 28).

Try converting your time to a Calendar object then increment the month field:
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(timeInMillis));
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1);
long newTimeInMillis = cal.getTime().getTime();
You may want to check for overflow from December to January and increment the year.
The API documentation confirms that DateTimeUtilities.ONEMONTH is four weeks, so what you got is what you should expect.

Related

How can I calculate today's date ONLY if today is less than or equal to the last day of the current month in Google Sheets?

I would like to express the following expression in Google Sheets:
IF today's date is less than or equal to the last day of the current month (in this case October 2021), THEN output today's date, OTHERWISE output the last day of the month (i.e. October 31, 2021)
I've tried a few different ways but nothing seems to work. Below is my latest attempt, but I get an #ERROR.
=IF(TODAY(<=2021,10,31),TODAY(),DATE(2021,10,31))
Thank you in advance for your help.
You need EOMONTH() function. Try below formula-
=IF(TODAY()<=EOMONTH(TODAY(),0),TODAY(),EOMONTH(TODAY(),0))
Just use =TODAY(). if you are interested in current month.
If you need today's date, but not further then the last day of October, use:
=MIN(TODAY(), DATE(2021, 10, 31))

UIDatePicker strange behaviour with Islamic calendar

Note: Bug reported to Apple
Radar number: 29265429 (Link)
I am using a UIDatePicker. When I give Gregorian calendar it works fine. The days starts from 1 to 31
However, when I give it Islamic islamicUmmAlQura it gives me a strange behaviour. The days starts from 1 to 30 but there is a '2' above 1 and below 30 such that days are as follows 2,1,2,3,4 ... 30
I have created a new empty iOS project and placed the following code into the viewDidLoad method:
let picker = UIDatePicker(frame: CGRect(x: 0, y: 30, width: 0, height: 0))
picker.datePickerMode = .date
picker.date = Date()
picker.calendar = Calendar(identifier: .islamicUmmAlQura)
picker.autoresizingMask = UIViewAutoresizing.flexibleRightMargin
picker.frame.size.width = 300
view.addSubview(picker)
Screenshot:
It is not that hard to make your own UIPicker that looks like a UIDatePicker, but you have to be careful because the bug is in NSCalendar as well as NSDatePicker.
Just to be clear the extra '2' is not selectable, just like 30 is not selectable in a short month. NSDatePicker does not hide invalid days or months, but makes them unselectable. So in the Gregorian calendar if you select February you will still see 29, 30, and 31, but trying to select them the picker will got to 28. The DataPicker figures out how many months and days to show by calling maximumRangeOfUnit: on the calendar. In an Islamic calendar the correct maximum is {1, 30}, but NSCalendar returns {1,31} for maximumRangeOfUnit:NSCalendarUnitDay for all Islamic calendars.
So there are two bugs. One is that NSCalendar is saying that there can be 31 days in an Islamic Month (which I don't believe is true), and the second issue is that NSDatePicker is showing the 31st day a 2 instead of 31.
Making your own NSDatePicker is really not that hard. The amount of element in each component is always the same (just make sure to manually set 30 for the days and not use the value from NSCalendar). The tricky part is deciding if a date is invalid and showing the element as gray and not letting the picket rest on it. You can use rangeOfUnit:InUnit:forDate to get the number of days in a month or number of months in a year. If an invalid element get selected them move the value down to the highest valid date (ie if a user selects 30 in a month with 29 days select 29).
Once a row is selected you can convert from day-month-year to an NSDate with dateFromComponents on NSCalendar.

Date offset from beginning of year

I want to know How do I find the date offset from beginning of year in iOS. I want a function which will take the date as an input and tell me the number of days passed.
For ex Jan 2 = 2nd Day of year and Feb 14 = 45th Day of Year

Showing days passed in month, or total days in month if the month is over (google spreadsheets)

Looking for a formula (for google spreadsheets) that shows either the days past in the current month if we're not past the last day of the current month, or the total days in that month if that month is behind us.
So, if today's March 25th, the formula would output 25 ... if today's April 1st, though, the formula would output 31.
Based on your sample, this should work (assuming the date is in A1):
=if(day(A1)=1,A1-date(year(A1),month(A1)-1,1),A1-date(year(A1),month(A1),1)+1)

How do I get a Date from a week number?

I want to find all items created in a given week, and pass in a week number param. (created_at is a normal timestamp.)
Given a week number, what is the easiest way to find a date in that particular week?
(Any date in the week will do, as I will use beginning_of_week and end_of_week in the scope.)
You can get Date objects representing the beginning and end of your week using the commercial method:
week = 41;
wkBegin = Date.commercial(2010, week, 1)
wkEnd = Date.commercial(2010, week, 7)
Now do your find:
Item.find(:all, :conditions->:create_date=>wkBegin..wkEnd.end_of_day)
Assuming you mean "a given week number in the current year", you can do the following:
2.weeks.since(Time.gm(Time.now.year))
=> Fri Jan 15 00:00:00 UTC 2010
Substitute (week_number - 1) for the 1 in the above, and you'll get a date in the desired week.

Resources