Skip to main content
Best answer

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


paulminors
Forum|alt.badge.img+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

Best answer by ikbelkirasanBest answer by ikbelkirasan

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

 

View original
Did this topic help you find an answer to your question?
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

andywingrave
Forum|alt.badge.img+9
  • Zapier Expert
  • 854 replies
  • April 9, 2020

You could just run some javaScript with the following logic:

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

 


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • April 9, 2020

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?


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • April 9, 2020

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

 


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • April 9, 2020

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


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • April 14, 2020

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


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • April 14, 2020

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

 


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • April 15, 2020

Will do! Thanks @ikbelkirasan 


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • April 21, 2020

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


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • April 21, 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,
 },
];

 


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • April 21, 2020

Will give this a go.


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • April 26, 2020

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


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • May 3, 2020

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


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • May 3, 2020

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


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • May 5, 2020

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


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • May 5, 2020

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


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • Answer
  • May 5, 2020

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

 


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • May 5, 2020

Thanks I’ll give this a go.


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • June 8, 2020

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


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • June 8, 2020

@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


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • June 8, 2020

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


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • June 8, 2020

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.


ikbelkirasan
Forum|alt.badge.img+12
  • Zapier Expert
  • 555 replies
  • June 9, 2020

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


paulminors
Forum|alt.badge.img+1
  • Author
  • Zapier Expert
  • 26 replies
  • June 10, 2020

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