Subtract one month from date

I would like to get the first day of the previous month.

I get the current execution time through the time generator node. And now i would like to get the first day of the previous month - so I can use these further down the workflow.

Eg.

I execute the workflow today, so the time generated is June 16, 2014

The date value I want to get back is the 1st of last month, so: May 1st, 2014

Is there a specific datetime calculator node somewhere or do I need to fall back to a Java snippet?

 

Hi,

I'd say you can get this done by starting with a  "date field extraction", followed by a "constant value" node for Day := 1 and a "math formula" node for Month := Month - 1, then recomposing the string with a column combiner (order doesn't matter) and parse results of this with "string to date". Slightly awkward, but it should work.

Cheers
E

I would use a Java Snippet as it is the easiest implementation.

// system imports

import java.util.GregorianCalendar;

// system variables

// Your custom variables:

// expression start

Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.MONTH, -1);
calendar.set(Calendar.DAY_OF_MONTH, 1);

out_newDateTime = calendar.getTime(); // this generates a dateTime variable
out_newStrFlowVar = out_newDateTime.toString(); //this generates a Date string

where the out_newDateTime variable is defined as a DateandTime object for a column and

where the out_newStrFlowVar variable is defined as a string for a flow variable (using the default string format).

 

I was not sure what format you needed it so I included both.

If you need the string in a different format you can use a SimpleDateFormat object.

Hope this helps,

-- Scott

 

 

A DateTime calculator node would be a nice addition to the Time Series collection of nodes. Another useful addition would be a node that sets a field of a date to a specific value. Both of these nodes would be useful in gstoel's use case, and I suspect to others (including myself).

I've developed a couple of metanodes to perform these two tasks (attached). Simply configure each metanode in the usual way by right-clicking the metanode and selecting "Configure". As metanodes, they are easily reusable in other workflows.

Please note: although I performed some testing, these metanodes have not been exhaustively tested. Try them out and please check that they are calculating the expected values! 

Don