Best answer

How to insert the last day of a month into a date field


Userlevel 4
Badge +1

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

icon

Best answer by ikbelkirasan 5 May 2020, 22:58

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

23 replies

Userlevel 4
Badge +1

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

Userlevel 4
Badge +1

Thanks I’ll give this a go.

Userlevel 4
Badge +1

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

Userlevel 4
Badge +1

Will do! Thanks @ikbelkirasan 

Userlevel 4
Badge +1

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

Userlevel 7
Badge +12

@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,
},
];

 

Userlevel 4
Badge +1

Will give this a go.

Userlevel 4
Badge +1

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

Userlevel 7
Badge +9

You could just run some javaScript with the following logic:

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

 

Userlevel 7
Badge +12

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

Userlevel 7
Badge +12

@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 }];

 

Userlevel 4
Badge +1

@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?

Userlevel 7
Badge +12

@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

Userlevel 4
Badge +1

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

Userlevel 4
Badge +1

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.

Userlevel 7
Badge +12

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

Userlevel 4
Badge +1

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?

Userlevel 7
Badge +12

@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 = [
{
lastOfMonth,
},
];

 

Userlevel 4
Badge +1

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

Userlevel 7
Badge +12

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

Userlevel 4
Badge +1

@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.

Userlevel 4
Badge +1

@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?

Userlevel 7
Badge +12

@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,
},
];