Expand my Community achievements bar.

SOLVED

Require Today's Day of Month with suffix written in JavaScript

Avatar

Level 4

Hello All,

I have searched through the forums and have only found a post using FormCalc which adds the "st" "nd" "rd" "th" suffixes for the day of the month. I require a script which does this written in JavaScript as pulling in current numeric day of the month and adding the suffixes to the string.

I have a paragraph of text which displays as follows:

Dated this _______ day of ____________, _______

I have the code working for month and year was quite straightforward, however, I am having much difficulty with the day of the month adding in the suffixes correctly from 1 to 31.

Please help as soon as possible, thank you all for your time.

Lewis

1 Accepted Solution

Avatar

Correct answer by
Level 10

You're close. But the script can still be shorter.

var nDay = new Date().getDate(),

r = "";

if (nDay == 1 || nDay == 21 || nDay == 31) {

r += "st";

} else if (nDay == 2 || nDay == 22) {

r += "nd";

} else if (nDay == 3 || nDay == 23) {

r += "rd";

} else {

r += "th";

}

this.rawValue = nDay + r;

View solution in original post

5 Replies

Avatar

Level 10

That's not that complicated.

As an example, this script with populate a textfield what nth day of the month the current day is.

var aNthDay = ["", "First", "Second", "Third", "Fourth", "Fifth", "Sixth", "Seventh", "Eighth", "Ninth", "Tenth", "Eleventh", "Twelfth", "Thirteenth", "Fourteenth", "Fifthteenth", "Sixteenth", "Seventeenth", "Eighteenth", "Nineteenth", "Twentieth", "Twenty-first", "Twenty-second", "Twenty-third", "Twenty-fourth", "Twenty-fifth", "Twenty-sixth", "Twenty-seventh", "Twenty-eighth", "Twenty-ninth", "Thirtieth", "Thirty-first"],

    iDay = util.printd("d", new Date());

this.rawValue = aNthDay[iDay];

Avatar

Level 4

Very cool radzmar,

However, I was not clear on what was attempted, I am trying to get this to work:

var today = new Date();

var dd = today.getDate();

if (dd == 1) {

this.rawValue = dd + "st";

}

else if (dd == 2) {

this.rawValue = dd + "nd";

}

else if (dd == 3) {

this.rawValue = dd + "rd";

}

else if (dd >= 4) {

this.rawValue = dd + "th";

}

Trying to get the suffix to work in all scenarios. Tried this:

var today = new Date();

var dd = today.getDate();

if (dd == 1,21,31) {

this.rawValue = dd + "st";

}

else if (dd == 2,22) {

this.rawValue = dd + "nd";

}

else if (dd == 3,23) {

this.rawValue = dd + "rd";

}

else if (dd == 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,24,25,26,27,28,29,30) {

this.rawValue = dd + "th";

}

Avatar

Level 4

I am trying this, but, cannot reset local time, laptop is locked:

var today = new Date();

var dd = today.getDate();

if (dd == 1 || dd == 21 || dd == 31) {

this.rawValue = dd + "st";

}

else if (dd == 2 || dd == 22) {

this.rawValue = dd + "nd";

}

else if (dd == 3 || dd == 23) {

this.rawValue = dd + "rd";

}

else if (dd == 4 || dd == 5 || dd == 6 || dd == 7 || dd == 8 || dd == 9 || dd == 10 || dd == 11 || dd == 12 || dd == 13 || dd == 14 || dd == 15 || dd == 16 || dd == 17 || dd == 18 || dd == 19 || dd == 20 || dd == 24 || dd == 25 || dd == 26 || dd == 27 || dd == 28 || dd == 29 || dd == 30) {

this.rawValue = dd + "th";

}

Do you think this will work?

Avatar

Correct answer by
Level 10

You're close. But the script can still be shorter.

var nDay = new Date().getDate(),

r = "";

if (nDay == 1 || nDay == 21 || nDay == 31) {

r += "st";

} else if (nDay == 2 || nDay == 22) {

r += "nd";

} else if (nDay == 3 || nDay == 23) {

r += "rd";

} else {

r += "th";

}

this.rawValue = nDay + r;

Avatar

Level 4

Very nice!  Thank you!

All the best...

Lewis