rahy
1
To calculate a period I use frappe.utils.add_month
and frappe.utils.add_years
But it doesn’t return a period.
Example: start is Jan 20, 2020
If I add 1 month to start:
end = frappe.utils.add_months(start, 1)
this will return Feb 20, 2020
which for a period I expect the return to be Feb 19, 2020
so the next period also start on date 20 (Feb 20 - Mar 19)
Is there any function to do this period?
Or is there any way to deduct 1 day, something like frappe.utils.subtract_days(end,1)
rahy
2
Found solution:
end = frappe.utils.add_days(end, -1)
will return 1 day before.
1 Like
rmeyer
3
You can also try this, for more flexibility:
moment('2020-01-20').add(1, 'month').subtract(1, 'day').format()
// "2020-02-19"
It is of course best to use the framework but some cases are not possible.
I made this to get the previous week, month or year:
function previous(period, n=1) {
const last_time = moment().subtract(n, period);
return {
from_date: last_time.startOf(period).format(),
to_date: last_time.endOf(period).format(),
};
}
previous('month');
// {from_date: '2020-11-01', to_date: '2020-11-20'}
2 Likes
rahy
4
Thanks @rmeyer,
Does it work in python?
rahy
6
Thanks anyway.
Because of your moment
I found a library for python moment
But I stick to my code because it doesn’t have to install another library.