• Instructions to Ask a Question

    For any assistance, please click the "Ask a Question" button and select the Pabbly product for which you require support.

    We offer seven comprehensive applications designed to help you efficiently manage and grow your business:

    Our support team endeavors to respond within 24 business hours (Monday to Friday, 10:00 AM to 6:00 PM IST). We appreciate your understanding and patience.

    🚀 Exclusive Lifetime Offers 🚀

    We invite you to take advantage of our special one-time payment plans, providing lifetime access to select applications:

    • 🔥 Pabbly Connect — Lifetime Access for $249View Offer
    • 🔥 Pabbly Subscription Billing — Lifetime Access for $249View Offer
    • 🔥 Pabbly Chatflow — Lifetime Access for $249View Offer

    Make a one-time investment and enjoy the advantages of robust business management tools for years to come.

convert numbers to ordinal numbers

Stuart

Member
Hi,
Is there any way to convert numbers to ordinal form?
That is 1 > 1st, 2 > 2nd 3 > 3rd, 123 > 123rd.
I could do it with a lookup table for 1 and 2 but that's not a free task!
 

Fagun Shah

Well-known member
Try this JS code action instead, which converts multiple comma separated cardinal numbers to ordinal numbers -



function convertToOrdinal(numbers) {
if (!Array.isArray(numbers)) {
return ['Input should be an array of numbers'];
}

const ordinalForms = [];

for (const number of numbers) {
if (typeof number !== 'number') {
ordinalForms.push('Not a number');
} else if (number % 1 !== 0) {
ordinalForms.push('Not an integer');
} else if (number >= 11 && number <= 13) {
ordinalForms.push(number + 'th');
} else {
switch (number % 10) {
case 1:
ordinalForms.push(number + 'st');
break;
case 2:
ordinalForms.push(number + 'nd');
break;
case 3:
ordinalForms.push(number + 'rd');
break;
default:
ordinalForms.push(number + 'th');
}
}
}

return ordinalForms;
}

// Example usage:
const result = convertToOrdinal([12, 1, 13]);//Replace 12,1,13 with your numbers
return (result);


1698807448540.png
 

wibbler

Active member
Hi I would actually like the reverse! Converting Ordinal dates (1st, 2nd...) in a form input to 1,2... in Pabbly so that it can be processed correctly by the Date Formatter. Is there a javascript for this too?
 

wibbler

Active member
Thanks! However, I have this:

Jan 4th 2025

And I want to make it

Jan 4 2025

with one Pabbly step. Is that possible with your solution? I think i'd have to isolate the date, strip out with regex, and then put the date together again.
 

Stuart

Member
you could do that just change the last line from
JavaScript:
console.log(result)
to
JavaScript:
return(result)
but it is a paid task.
you could do it with free tasks but I think it will be 2 steps (split, then regex).

1736276125076.png
 

wibbler

Active member
Thanks again! I think from your wording, there is no free way to do this in one step within Pabbly?
 

ArshilAhmad

Moderator
Staff member
Yes, if you want to use regex to strip out 'th' or 'nd', you would have to split the date and then put it back together once it's stripped of the unwanted characters

===============================================================

Or you can also use two 'Replace Text' action steps to remove the unwanted characters.

 

wibbler

Active member
Thanks - I think with Replace Text there would need to be multiple steps to remove "th", "nd", "st", am I right?
 

ArshilAhmad

Moderator
Staff member
Yes, you need three 'Replace Text' action steps in your workflow to achieve the desired result. Also, you have to map the result of one Text Formatter to another. Check out the workflow shared below.

 
Top