Skip to main content

Does anyone who how I could put a date into a date field that is the last day of the currnet month? So if a Zap ran today (April) it would return the date: 30/04/2020

You could just run some javaScript with the following logic:

var today = new Date()
, lastOfMonth = new Date( today.getFullYear(), today.getMonth()+1, 0 )

 


I would do this with a the code action step?

I tried this and it returned the following error: 

Error: You did not define `output`! Try `output = {id: 1, hello: "world"};`

Do I need to input any variables?


@paulminors - Adding to @Saastronomical ‘s answer, the following code should output the last day of the month in the format DD/MM/YYYY.

const today = new Date();
const lastOfMonthDate = new Date(today.getFullYear(), today.getMonth() + 1, 0);
const formatDigits = (digit) => String(digit).padStart(2, "0");

const day = formatDigits(lastOfMonthDate.getDate());
const month = formatDigits(lastOfMonthDate.getMonth() + 1);
const year = lastOfMonthDate.getFullYear();
const lastOfMonth = `${day}/${month}/${year}`;

output = t
{
lastOfMonth,
},
];

 


Thanks @ikbelkirasan I’ve input that code and it seems to be working :)


@ikbelkirasan my Zap just ran today with that code and it returned the date 29/04/2020 instead of 30/03/2020. Any idea how to update this to return the last day of the month?


@paulminors - Try the following code and let me know if it fixes the issue.

const formatDigits = (digit) => String(digit).padStart(2, "0");

const d = new Date();
d.setDate(0);
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
d.setMonth(d.getMonth() + 1);
d.setDate(d.getDate() - 1);

const day = formatDigits(d.getDate());
const month = formatDigits(d.getMonth() + 1);
const year = d.getFullYear();
const lastOfMonth = `${day}/${month}/${year}`;

output =
{
lastOfMonth,
},
];

 


Will do! Thanks @ikbelkirasan 


@ikbelkirasan weird, the javascript is still returning the date 29/04/2020.


@paulminors hmm, what about this bit of code?

const formatDigits = (digit) => String(digit).padStart(2, "0");

const d = new Date();
d.setUTCDate(0);
d.setUTCHours(0);
d.setUTCMinutes(0);
d.setUTCSeconds(0);
d.setUTCMonth(d.getUTCMonth() + 1);
d.setUTCDate(d.getUTCDate() - 1);

const day = formatDigits(d.getUTCDate());
const month = formatDigits(d.getUTCMonth() + 1);
const year = d.getUTCFullYear();
const lastOfMonth = `${day}/${month}/${year}`;

output =
{
lastOfMonth,
},
];

 


Will give this a go.


@ikbelkirasan it’s weird when I input this code and do a test in the Zap editor, it works and returns the date 30/04/2020. However, when the Zap actually runs it returns 29/04/2020.


@paulminors - Sorry for the late response. That’s so weird.. Did you check your timezone settings for that zap?


Yep, it’s set to Pacific/Auckland (New Zealand) time.


It just ran again and returned the date 28/05/2020 for the month of May.


@paulminors - I’ll check again now and let you know.


@paulminors - I think I figured it out. Try this instead:

const formatDigits = digit => String(digit).padStart(2, "0");

const d = new Date();
d.setDate(1);
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
d.setMonth(d.getMonth() + 1);
d.setDate(d.getDate() - 1);

const day = formatDigits(d.getDate());
const month = formatDigits(d.getMonth() + 1);
const year = d.getFullYear();
const lastOfMonth = `${day}/${month}/${year}`;

output = { lastOfMonth }];

 


Thanks I’ll give this a go.


@ikbelkirasan Just an update on this, I’m still having the same issue as before where this is returning the day before the last of the month e.g. for June, it’s calculating 29th instead of the 30th. 

Do you think my time zone settings could be a factor?


@paulminors - It could be! I have tested the code with a few timezone settings though and it worked fine. Can you confirm if you’re using the latest version of the code? Thanks


Yep, I’m using the latest version (also to confirm, there is nothing set in the ‘Input Data’ field).


Also, when I run the test in Zapier, it returns the correct date (30/06/2020). But when the Zap runs with live data, it’s returning 29/06/2020). So it’s odd that testing vs. live is returning different results.


@paulminors - That’s really weird! I can’t think of a reason why that would happen. Especially since it works in testing.


What I can do is simply use the +1d to add a day to the calculated date.