Projecting quarterly dates using datetime variables

8 Ansichten (letzte 30 Tage)
Alexandre C Borges
Alexandre C Borges am 12 Aug. 2017
Bearbeitet: dpb am 15 Aug. 2017
Hi there,
I'm trying to project quarter-end dates but the calquarter (or calmonth) function doesn't seem to work correctly if the start date is a month with 30 days.
Here's an example where it works correctly (start date is month with 31 days). The result in this case is adjusted for months with 30 and 31 days:
>> t = datetime(2013,05,31):calquarters(1):datetime(2014,05,31)
t =
31-May-2013 31-Aug-2013 30-Nov-2013 28-Feb-2014 31-May-2014
Here is an example where it doesn't work correctly (start date is month with 30 days). The result in this case has the 30th of each month as quarter-end, which is not correct:
>> t = datetime(2013,06,30):calquarters(1):datetime(2014,05,31)
t =
30-Jun-2013 30-Sep-2013 30-Dec-2013 30-Mar-2014
I appreciate any help with this. Many thanks!!!
Alex
  3 Kommentare
Stephen23
Stephen23 am 15 Aug. 2017
Bearbeitet: Stephen23 am 15 Aug. 2017
"Looks like a bug..."
I disagree: it looks like a badly posed definition. Consider the first of the month:
datetime(2013,05,1):calquarters(1):datetime(2014,05,1)
What would the expected output be? What about the second of the month?:
datetime(2013,05,2):calquarters(1):datetime(2014,05,2)
And so on up until 30. This is ambiguous because what humans see as "intuitive" is really an unrelated operation. I agree with Peter Perkins' comment below: dateshift is the correct way to achieve this task.
dpb
dpb am 15 Aug. 2017
I agree I think it a definition issue; see follow-up...I pose an alternate syntax altho I think the "intuitive" solution is the more likely one users would expect in a high fraction of instances so should rate higher on the scale of how to implement/make the definition. The point of Matlab is the RAD arena; things that take extra effort to fix up detract from that objective....and, of course, that's all in the realm of $0.02, imo, ymmv, etc., etc., etc., ... caveats. :)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 12 Aug. 2017
Bearbeitet: dpb am 12 Aug. 2017
OH! Wait! That comment re: Peter reminds me of a trick he did show earlier that may fixup the problem-- dateshift. Let's see with your bum example:
>> tq = datetime(2013,06,30):calquarters(1):datetime(2014,05,31)
tq =
30-Jun-2013 30-Sep-2013 30-Dec-2013 30-Mar-2014
>> dateshift(tq,'end','month')
ans =
30-Jun-2013 30-Sep-2013 31-Dec-2013 31-Mar-2014
>>
VOILA! I agree it should work "out of the box", but there at least there is a reasonable way to clean up the mess...
You could even wrap the original call in the dateshift function and do it in a single step.
  6 Kommentare
dpb
dpb am 15 Aug. 2017
Bearbeitet: dpb am 15 Aug. 2017
Peter, I think your above explanation should go almost verbatim in the 'Algorithms' section of the documentation; it is the kind of information that does reduce confusion and add clarification to users (particularly newer ones or old ones to whom it is a new beast).
I'll reiterate the comments that the examples for datetime are far too simple in general to illustrate the usage of the forms; the cases shown never deal with these issues. This thread would be an ideal example to use to both illustrate the calculation as implemented and the intent of the functions in use to solve the problems ("warts") left over.
dpb
dpb am 15 Aug. 2017
Bearbeitet: dpb am 15 Aug. 2017
Alexandre -- yes, dateshift is handy, indeed; not sure one would ever find it from the documentation without hours and hours of looking, though, as first of all you have to go to the subsection on 'Arithmetic and Plotting' to even find it mentioned and (at least for me) on first blush it really was neither of those subjects that I even thought I was looking for to fix up similar issues when I first encountered datetime and had such problems.
In remembering it here, it does show that my "stream of consciousness" responses often do bear fruit in it was being reminded of Peter's helpful comments in those other threads that made me think of dateshift here; it isn't yet ingrained enough that it was my first aha! response to your question.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by