Subtracting months


#1

Is there a way to convert the MMMM time attribute into a number to do "October - August" and get the difference in months?


#2

You can do math with dates by using one of the different format strings in the {time} command.

The simplest would be to use the M formatting option that gives you the month as a number 1 - 12.

Comparing months: {={time: M} - {time: M; shift=-2M}}

If you have dates from different years this simple approach won't work, but you can combine it with {time: YYYY} to get the current year and use a slightly more complex equation to do the math.

You can also use {time: X} (this is called a timestamp) to get the number of seconds since 1970, which can be used to calculated differences in seconds which you can then convert to hours, day, or (approximate) months.

See here for more formatting options:


#3

Ok, and then how do I convert the timestamp back into a regular format?


#4

Going back to your example above, if you wanted to get the difference in months, you would do something like:

Month difference: {=round(({time: X} - {time: X; shift=-2M})/60/60/24/30)}

The math here is: seconds/60 -> minutes/60 -> hours/24 -> days/30 -> months. We round the result as a month isn't exactly 30 days.


#5

You're the best. :smiley: