JavaScript new Date Ordinal (st, nd, rd, th)
If at all possible, without JavaScript libraries or lots of clunky code I am looking for the simplest way to format a date two weeks from now in the following format:
13th March 2013
The code I am using is:
var newdate = new Date(+new Date + 12096e5);
document.body.innerHTML = newdate;
which returns the date and time two weeks from now, but like this:
Wed Mar 27 2013 21:50:29 GMT+0000 (GMT Standard Time)
Here is the code in jsFiddle.
Any help would be appreciated!
javascript date ordinal-indicator
add a comment |
If at all possible, without JavaScript libraries or lots of clunky code I am looking for the simplest way to format a date two weeks from now in the following format:
13th March 2013
The code I am using is:
var newdate = new Date(+new Date + 12096e5);
document.body.innerHTML = newdate;
which returns the date and time two weeks from now, but like this:
Wed Mar 27 2013 21:50:29 GMT+0000 (GMT Standard Time)
Here is the code in jsFiddle.
Any help would be appreciated!
javascript date ordinal-indicator
apart from a missing "of" I have no problem with that date
– mplungjan
Mar 13 '13 at 22:05
try this stackoverflow.com/questions/1056728/…
– ebram khalil
Mar 13 '13 at 22:05
I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it
– v0d1ch
Nov 25 '14 at 13:40
add a comment |
If at all possible, without JavaScript libraries or lots of clunky code I am looking for the simplest way to format a date two weeks from now in the following format:
13th March 2013
The code I am using is:
var newdate = new Date(+new Date + 12096e5);
document.body.innerHTML = newdate;
which returns the date and time two weeks from now, but like this:
Wed Mar 27 2013 21:50:29 GMT+0000 (GMT Standard Time)
Here is the code in jsFiddle.
Any help would be appreciated!
javascript date ordinal-indicator
If at all possible, without JavaScript libraries or lots of clunky code I am looking for the simplest way to format a date two weeks from now in the following format:
13th March 2013
The code I am using is:
var newdate = new Date(+new Date + 12096e5);
document.body.innerHTML = newdate;
which returns the date and time two weeks from now, but like this:
Wed Mar 27 2013 21:50:29 GMT+0000 (GMT Standard Time)
Here is the code in jsFiddle.
Any help would be appreciated!
javascript date ordinal-indicator
javascript date ordinal-indicator
edited Aug 9 '18 at 22:37
dhilt
7,60231940
7,60231940
asked Mar 13 '13 at 22:02
user1635828
146124
146124
apart from a missing "of" I have no problem with that date
– mplungjan
Mar 13 '13 at 22:05
try this stackoverflow.com/questions/1056728/…
– ebram khalil
Mar 13 '13 at 22:05
I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it
– v0d1ch
Nov 25 '14 at 13:40
add a comment |
apart from a missing "of" I have no problem with that date
– mplungjan
Mar 13 '13 at 22:05
try this stackoverflow.com/questions/1056728/…
– ebram khalil
Mar 13 '13 at 22:05
I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it
– v0d1ch
Nov 25 '14 at 13:40
apart from a missing "of" I have no problem with that date
– mplungjan
Mar 13 '13 at 22:05
apart from a missing "of" I have no problem with that date
– mplungjan
Mar 13 '13 at 22:05
try this stackoverflow.com/questions/1056728/…
– ebram khalil
Mar 13 '13 at 22:05
try this stackoverflow.com/questions/1056728/…
– ebram khalil
Mar 13 '13 at 22:05
I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it
– v0d1ch
Nov 25 '14 at 13:40
I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it
– v0d1ch
Nov 25 '14 at 13:40
add a comment |
12 Answers
12
active
oldest
votes
Here:
JSFiddle
var fortnightAway = new Date(+new Date + 12096e5),
date = fortnightAway.getDate(),
month = ["January","February","March","April","May","June","July",
"August","September","October","November","December"][fortnightAway.getMonth()];
function nth(d) {
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
document.body.innerHTML = date + nth(date) + " " +
month + " " +
fortnightAway.getFullYear();
2
Good work.... Thanks
– Elby
May 27 '14 at 6:03
Works perfect... +1 ford>3 && d<21
– Chandra Nakka
Apr 17 '15 at 10:25
For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";
– krick
Dec 15 '16 at 22:36
That is missing 21,22,23
– mplungjan
Dec 16 '16 at 5:12
if (d == 0) return ''; ... if used outside of a date context
– John
Apr 13 '18 at 14:47
|
show 1 more comment
Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.
function getOrdinalNum(n) {
return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '');
}
tested 1 to 31 and it works correctly
– johnc
Oct 22 '17 at 17:34
add a comment |
Lots of formatting answers, so I'll just work on the nth of any integer-
Number.prototype.nth= function(){
if(this%1) return this;
var s= this%100;
if(s>3 && s<21) return this+'th';
switch(s%10){
case 1: return this+'st';
case 2: return this+'nd';
case 3: return this+'rd';
default: return this+'th';
}
}
The if is for teens, so 13 is 13th and not 13rd.
– kennebec
Mar 13 '13 at 22:32
Why thethis%1
? Mod 1 is always 0 so the statement will always fail, no?
– Forage
Mar 21 '18 at 11:10
add a comment |
I was doing this for dates as well, but because the day of month can only be between 1 and 31, I ended up with a simplified solution.
function dateOrdinal(dom) {
if (dom == 31 || dom == 21 || dom == 1) return dom + "st";
else if (dom == 22 || dom == 2) return dom + "nd";
else if (dom == 23 || dom == 3) return dom + "rd";
else return dom + "th";
};
or compact version using conditional operators
function dateOrdinal(d) {
return d+(31==d||21==d||1==d?"st":22==d||2==d?"nd":23==d||3==d?"rd":"th")
};
http://jsben.ch/#/DrBpl
add a comment |
Lots of answers, here's another:
function addOrd(n) {
var ords = [,'st','nd','rd'];
var ord, m = n%100;
return n + ((m > 10 && m < 14)? 'th' : ords[m%10] || 'th');
}
// Return date string two weeks from now (14 days) in
// format 13th March 2013
function formatDatePlusTwoWeeks(d) {
var months = ['January','February','March','April','May','June',
'July','August','September','October','November','December'];
// Copy date object so don't modify original
var e = new Date(d);
// Add two weeks (14 days)
e.setDate(e.getDate() + 14);
return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();
}
alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013
add a comment |
I'm a bit late to the party, but this should work:
function ordinal(number) {
number = Number(number)
if(!number || (Math.round(number) !== number)) {
return number
}
var signal = (number < 20) ? number : Number(('' + number).slice(-1))
switch(signal) {
case 1:
return number + 'st'
case 2:
return number + 'nd'
case 3:
return number + 'rd'
default:
return number + 'th'
}
}
function specialFormat(date) {
// add two weeks
date = new Date(+date + 12096e5)
var months = [
'January'
, 'February'
, 'March'
, 'April'
, 'May'
, 'June'
, 'July'
, 'August'
, 'September'
, 'October'
, 'November'
, 'December'
]
var formatted = ordinal(date.getDate())
formatted += ' ' + months[date.getMonth()]
return formatted + ' ' + date.getFullYear()
}
document.body.innerHTML = specialFormat(new Date())
add a comment |
As many has mentioned, here is another answer.
This is directly based on @kennebec's answer, which I found the most simplest way to get this date Ordinal generated for given JavaScript
date:
I created two prototype function
as follows:
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Note: just include the above prototype
functions within your JS Script
and use it as described bellow.
And whenever there is a JS
date I need to generate the date with date ordinal I use that prototype method as follows on that JS
date:
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
And it will print out date with date ordinal as shown in the following live demo:
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
.
add a comment |
A short and compact solution:
function format(date, tmp){
return [
(tmp = date.getDate()) +
([, 'st', 'nd', 'rd'][/1?.$/.exec(tmp)] || 'th'),
[ 'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
][date.getMonth()],
date.getFullYear()
].join(' ')
}
// 14 days from today
console.log('14 days from today: ' +
format(new Date(+new Date + 14 * 864e5)));
// test formatting for all dates within a month from today
var day = 864e5, today = +new Date;
for(var i = 0; i < 32; i++) {
console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))
}
(The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)
add a comment |
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
add a comment |
Super simple functional implementation:
const ordinal = (d) => {
const nth = { '1': 'st', '2': 'nd', '3': 'rd' }
return `${d}${nth[d] || 'th'}`
}
const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']
const dateString = (date) => `${ordinal(date.getDate())} ${monthNames[date.getMonth()]} ${date.getFullYear()}`
// Use like this:
dateString(new Date()) // 18th July 2016
Unfortunately this method is too simple and doesn't work for days like the 21st.
– Tom
Oct 22 '16 at 23:36
add a comment |
Strongly inspired by @user2309185's.
const ordinal = (d) => {
return d + (['st', 'nd', 'rd'][d % 10 - 1] || 'th')
}
1
I found an issue with this,13
=13rd
– haakym
Feb 16 '17 at 22:39
add a comment |
Here is an easy solution:
var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd':
(today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));
running this in scratchpad gives "today not defined."
– Nick
Jan 7 '18 at 6:22
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15397372%2fjavascript-new-date-ordinal-st-nd-rd-th%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here:
JSFiddle
var fortnightAway = new Date(+new Date + 12096e5),
date = fortnightAway.getDate(),
month = ["January","February","March","April","May","June","July",
"August","September","October","November","December"][fortnightAway.getMonth()];
function nth(d) {
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
document.body.innerHTML = date + nth(date) + " " +
month + " " +
fortnightAway.getFullYear();
2
Good work.... Thanks
– Elby
May 27 '14 at 6:03
Works perfect... +1 ford>3 && d<21
– Chandra Nakka
Apr 17 '15 at 10:25
For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";
– krick
Dec 15 '16 at 22:36
That is missing 21,22,23
– mplungjan
Dec 16 '16 at 5:12
if (d == 0) return ''; ... if used outside of a date context
– John
Apr 13 '18 at 14:47
|
show 1 more comment
Here:
JSFiddle
var fortnightAway = new Date(+new Date + 12096e5),
date = fortnightAway.getDate(),
month = ["January","February","March","April","May","June","July",
"August","September","October","November","December"][fortnightAway.getMonth()];
function nth(d) {
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
document.body.innerHTML = date + nth(date) + " " +
month + " " +
fortnightAway.getFullYear();
2
Good work.... Thanks
– Elby
May 27 '14 at 6:03
Works perfect... +1 ford>3 && d<21
– Chandra Nakka
Apr 17 '15 at 10:25
For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";
– krick
Dec 15 '16 at 22:36
That is missing 21,22,23
– mplungjan
Dec 16 '16 at 5:12
if (d == 0) return ''; ... if used outside of a date context
– John
Apr 13 '18 at 14:47
|
show 1 more comment
Here:
JSFiddle
var fortnightAway = new Date(+new Date + 12096e5),
date = fortnightAway.getDate(),
month = ["January","February","March","April","May","June","July",
"August","September","October","November","December"][fortnightAway.getMonth()];
function nth(d) {
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
document.body.innerHTML = date + nth(date) + " " +
month + " " +
fortnightAway.getFullYear();
Here:
JSFiddle
var fortnightAway = new Date(+new Date + 12096e5),
date = fortnightAway.getDate(),
month = ["January","February","March","April","May","June","July",
"August","September","October","November","December"][fortnightAway.getMonth()];
function nth(d) {
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
document.body.innerHTML = date + nth(date) + " " +
month + " " +
fortnightAway.getFullYear();
var fortnightAway = new Date(+new Date + 12096e5),
date = fortnightAway.getDate(),
month = ["January","February","March","April","May","June","July",
"August","September","October","November","December"][fortnightAway.getMonth()];
function nth(d) {
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
document.body.innerHTML = date + nth(date) + " " +
month + " " +
fortnightAway.getFullYear();
var fortnightAway = new Date(+new Date + 12096e5),
date = fortnightAway.getDate(),
month = ["January","February","March","April","May","June","July",
"August","September","October","November","December"][fortnightAway.getMonth()];
function nth(d) {
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return "st";
case 2: return "nd";
case 3: return "rd";
default: return "th";
}
}
document.body.innerHTML = date + nth(date) + " " +
month + " " +
fortnightAway.getFullYear();
edited Nov 12 '18 at 10:00
answered Mar 13 '13 at 22:11
mplungjan
86.8k20122181
86.8k20122181
2
Good work.... Thanks
– Elby
May 27 '14 at 6:03
Works perfect... +1 ford>3 && d<21
– Chandra Nakka
Apr 17 '15 at 10:25
For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";
– krick
Dec 15 '16 at 22:36
That is missing 21,22,23
– mplungjan
Dec 16 '16 at 5:12
if (d == 0) return ''; ... if used outside of a date context
– John
Apr 13 '18 at 14:47
|
show 1 more comment
2
Good work.... Thanks
– Elby
May 27 '14 at 6:03
Works perfect... +1 ford>3 && d<21
– Chandra Nakka
Apr 17 '15 at 10:25
For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";
– krick
Dec 15 '16 at 22:36
That is missing 21,22,23
– mplungjan
Dec 16 '16 at 5:12
if (d == 0) return ''; ... if used outside of a date context
– John
Apr 13 '18 at 14:47
2
2
Good work.... Thanks
– Elby
May 27 '14 at 6:03
Good work.... Thanks
– Elby
May 27 '14 at 6:03
Works perfect... +1 for
d>3 && d<21
– Chandra Nakka
Apr 17 '15 at 10:25
Works perfect... +1 for
d>3 && d<21
– Chandra Nakka
Apr 17 '15 at 10:25
For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";
– krick
Dec 15 '16 at 22:36
For clarity, I'd probably code the exceptions as either: if (n == 11 || n == 12 || n == 13) return "th"; or: if (n >= 11 && n <= 13) return "th";
– krick
Dec 15 '16 at 22:36
That is missing 21,22,23
– mplungjan
Dec 16 '16 at 5:12
That is missing 21,22,23
– mplungjan
Dec 16 '16 at 5:12
if (d == 0) return ''; ... if used outside of a date context
– John
Apr 13 '18 at 14:47
if (d == 0) return ''; ... if used outside of a date context
– John
Apr 13 '18 at 14:47
|
show 1 more comment
Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.
function getOrdinalNum(n) {
return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '');
}
tested 1 to 31 and it works correctly
– johnc
Oct 22 '17 at 17:34
add a comment |
Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.
function getOrdinalNum(n) {
return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '');
}
tested 1 to 31 and it works correctly
– johnc
Oct 22 '17 at 17:34
add a comment |
Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.
function getOrdinalNum(n) {
return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '');
}
Here is a one liner inspired by the other answers. It is tested and will take 0 and negative numbers.
function getOrdinalNum(n) {
return n + (n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] : '');
}
answered Jun 7 '17 at 17:00
The Martin
14112
14112
tested 1 to 31 and it works correctly
– johnc
Oct 22 '17 at 17:34
add a comment |
tested 1 to 31 and it works correctly
– johnc
Oct 22 '17 at 17:34
tested 1 to 31 and it works correctly
– johnc
Oct 22 '17 at 17:34
tested 1 to 31 and it works correctly
– johnc
Oct 22 '17 at 17:34
add a comment |
Lots of formatting answers, so I'll just work on the nth of any integer-
Number.prototype.nth= function(){
if(this%1) return this;
var s= this%100;
if(s>3 && s<21) return this+'th';
switch(s%10){
case 1: return this+'st';
case 2: return this+'nd';
case 3: return this+'rd';
default: return this+'th';
}
}
The if is for teens, so 13 is 13th and not 13rd.
– kennebec
Mar 13 '13 at 22:32
Why thethis%1
? Mod 1 is always 0 so the statement will always fail, no?
– Forage
Mar 21 '18 at 11:10
add a comment |
Lots of formatting answers, so I'll just work on the nth of any integer-
Number.prototype.nth= function(){
if(this%1) return this;
var s= this%100;
if(s>3 && s<21) return this+'th';
switch(s%10){
case 1: return this+'st';
case 2: return this+'nd';
case 3: return this+'rd';
default: return this+'th';
}
}
The if is for teens, so 13 is 13th and not 13rd.
– kennebec
Mar 13 '13 at 22:32
Why thethis%1
? Mod 1 is always 0 so the statement will always fail, no?
– Forage
Mar 21 '18 at 11:10
add a comment |
Lots of formatting answers, so I'll just work on the nth of any integer-
Number.prototype.nth= function(){
if(this%1) return this;
var s= this%100;
if(s>3 && s<21) return this+'th';
switch(s%10){
case 1: return this+'st';
case 2: return this+'nd';
case 3: return this+'rd';
default: return this+'th';
}
}
Lots of formatting answers, so I'll just work on the nth of any integer-
Number.prototype.nth= function(){
if(this%1) return this;
var s= this%100;
if(s>3 && s<21) return this+'th';
switch(s%10){
case 1: return this+'st';
case 2: return this+'nd';
case 3: return this+'rd';
default: return this+'th';
}
}
edited Mar 13 '13 at 22:30
answered Mar 13 '13 at 22:14
kennebec
78.1k1986116
78.1k1986116
The if is for teens, so 13 is 13th and not 13rd.
– kennebec
Mar 13 '13 at 22:32
Why thethis%1
? Mod 1 is always 0 so the statement will always fail, no?
– Forage
Mar 21 '18 at 11:10
add a comment |
The if is for teens, so 13 is 13th and not 13rd.
– kennebec
Mar 13 '13 at 22:32
Why thethis%1
? Mod 1 is always 0 so the statement will always fail, no?
– Forage
Mar 21 '18 at 11:10
The if is for teens, so 13 is 13th and not 13rd.
– kennebec
Mar 13 '13 at 22:32
The if is for teens, so 13 is 13th and not 13rd.
– kennebec
Mar 13 '13 at 22:32
Why the
this%1
? Mod 1 is always 0 so the statement will always fail, no?– Forage
Mar 21 '18 at 11:10
Why the
this%1
? Mod 1 is always 0 so the statement will always fail, no?– Forage
Mar 21 '18 at 11:10
add a comment |
I was doing this for dates as well, but because the day of month can only be between 1 and 31, I ended up with a simplified solution.
function dateOrdinal(dom) {
if (dom == 31 || dom == 21 || dom == 1) return dom + "st";
else if (dom == 22 || dom == 2) return dom + "nd";
else if (dom == 23 || dom == 3) return dom + "rd";
else return dom + "th";
};
or compact version using conditional operators
function dateOrdinal(d) {
return d+(31==d||21==d||1==d?"st":22==d||2==d?"nd":23==d||3==d?"rd":"th")
};
http://jsben.ch/#/DrBpl
add a comment |
I was doing this for dates as well, but because the day of month can only be between 1 and 31, I ended up with a simplified solution.
function dateOrdinal(dom) {
if (dom == 31 || dom == 21 || dom == 1) return dom + "st";
else if (dom == 22 || dom == 2) return dom + "nd";
else if (dom == 23 || dom == 3) return dom + "rd";
else return dom + "th";
};
or compact version using conditional operators
function dateOrdinal(d) {
return d+(31==d||21==d||1==d?"st":22==d||2==d?"nd":23==d||3==d?"rd":"th")
};
http://jsben.ch/#/DrBpl
add a comment |
I was doing this for dates as well, but because the day of month can only be between 1 and 31, I ended up with a simplified solution.
function dateOrdinal(dom) {
if (dom == 31 || dom == 21 || dom == 1) return dom + "st";
else if (dom == 22 || dom == 2) return dom + "nd";
else if (dom == 23 || dom == 3) return dom + "rd";
else return dom + "th";
};
or compact version using conditional operators
function dateOrdinal(d) {
return d+(31==d||21==d||1==d?"st":22==d||2==d?"nd":23==d||3==d?"rd":"th")
};
http://jsben.ch/#/DrBpl
I was doing this for dates as well, but because the day of month can only be between 1 and 31, I ended up with a simplified solution.
function dateOrdinal(dom) {
if (dom == 31 || dom == 21 || dom == 1) return dom + "st";
else if (dom == 22 || dom == 2) return dom + "nd";
else if (dom == 23 || dom == 3) return dom + "rd";
else return dom + "th";
};
or compact version using conditional operators
function dateOrdinal(d) {
return d+(31==d||21==d||1==d?"st":22==d||2==d?"nd":23==d||3==d?"rd":"th")
};
http://jsben.ch/#/DrBpl
edited Feb 15 '17 at 23:06
answered Feb 15 '17 at 22:57
fredli74
213
213
add a comment |
add a comment |
Lots of answers, here's another:
function addOrd(n) {
var ords = [,'st','nd','rd'];
var ord, m = n%100;
return n + ((m > 10 && m < 14)? 'th' : ords[m%10] || 'th');
}
// Return date string two weeks from now (14 days) in
// format 13th March 2013
function formatDatePlusTwoWeeks(d) {
var months = ['January','February','March','April','May','June',
'July','August','September','October','November','December'];
// Copy date object so don't modify original
var e = new Date(d);
// Add two weeks (14 days)
e.setDate(e.getDate() + 14);
return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();
}
alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013
add a comment |
Lots of answers, here's another:
function addOrd(n) {
var ords = [,'st','nd','rd'];
var ord, m = n%100;
return n + ((m > 10 && m < 14)? 'th' : ords[m%10] || 'th');
}
// Return date string two weeks from now (14 days) in
// format 13th March 2013
function formatDatePlusTwoWeeks(d) {
var months = ['January','February','March','April','May','June',
'July','August','September','October','November','December'];
// Copy date object so don't modify original
var e = new Date(d);
// Add two weeks (14 days)
e.setDate(e.getDate() + 14);
return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();
}
alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013
add a comment |
Lots of answers, here's another:
function addOrd(n) {
var ords = [,'st','nd','rd'];
var ord, m = n%100;
return n + ((m > 10 && m < 14)? 'th' : ords[m%10] || 'th');
}
// Return date string two weeks from now (14 days) in
// format 13th March 2013
function formatDatePlusTwoWeeks(d) {
var months = ['January','February','March','April','May','June',
'July','August','September','October','November','December'];
// Copy date object so don't modify original
var e = new Date(d);
// Add two weeks (14 days)
e.setDate(e.getDate() + 14);
return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();
}
alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013
Lots of answers, here's another:
function addOrd(n) {
var ords = [,'st','nd','rd'];
var ord, m = n%100;
return n + ((m > 10 && m < 14)? 'th' : ords[m%10] || 'th');
}
// Return date string two weeks from now (14 days) in
// format 13th March 2013
function formatDatePlusTwoWeeks(d) {
var months = ['January','February','March','April','May','June',
'July','August','September','October','November','December'];
// Copy date object so don't modify original
var e = new Date(d);
// Add two weeks (14 days)
e.setDate(e.getDate() + 14);
return addOrd(e.getDate()) + ' ' + months[e.getMonth()] + ' ' + e.getFullYear();
}
alert(formatDatePlusTwoWeeks(new Date(2013,2,13))); // 27th March 2013
answered Mar 13 '13 at 22:28
RobG
97k19103145
97k19103145
add a comment |
add a comment |
I'm a bit late to the party, but this should work:
function ordinal(number) {
number = Number(number)
if(!number || (Math.round(number) !== number)) {
return number
}
var signal = (number < 20) ? number : Number(('' + number).slice(-1))
switch(signal) {
case 1:
return number + 'st'
case 2:
return number + 'nd'
case 3:
return number + 'rd'
default:
return number + 'th'
}
}
function specialFormat(date) {
// add two weeks
date = new Date(+date + 12096e5)
var months = [
'January'
, 'February'
, 'March'
, 'April'
, 'May'
, 'June'
, 'July'
, 'August'
, 'September'
, 'October'
, 'November'
, 'December'
]
var formatted = ordinal(date.getDate())
formatted += ' ' + months[date.getMonth()]
return formatted + ' ' + date.getFullYear()
}
document.body.innerHTML = specialFormat(new Date())
add a comment |
I'm a bit late to the party, but this should work:
function ordinal(number) {
number = Number(number)
if(!number || (Math.round(number) !== number)) {
return number
}
var signal = (number < 20) ? number : Number(('' + number).slice(-1))
switch(signal) {
case 1:
return number + 'st'
case 2:
return number + 'nd'
case 3:
return number + 'rd'
default:
return number + 'th'
}
}
function specialFormat(date) {
// add two weeks
date = new Date(+date + 12096e5)
var months = [
'January'
, 'February'
, 'March'
, 'April'
, 'May'
, 'June'
, 'July'
, 'August'
, 'September'
, 'October'
, 'November'
, 'December'
]
var formatted = ordinal(date.getDate())
formatted += ' ' + months[date.getMonth()]
return formatted + ' ' + date.getFullYear()
}
document.body.innerHTML = specialFormat(new Date())
add a comment |
I'm a bit late to the party, but this should work:
function ordinal(number) {
number = Number(number)
if(!number || (Math.round(number) !== number)) {
return number
}
var signal = (number < 20) ? number : Number(('' + number).slice(-1))
switch(signal) {
case 1:
return number + 'st'
case 2:
return number + 'nd'
case 3:
return number + 'rd'
default:
return number + 'th'
}
}
function specialFormat(date) {
// add two weeks
date = new Date(+date + 12096e5)
var months = [
'January'
, 'February'
, 'March'
, 'April'
, 'May'
, 'June'
, 'July'
, 'August'
, 'September'
, 'October'
, 'November'
, 'December'
]
var formatted = ordinal(date.getDate())
formatted += ' ' + months[date.getMonth()]
return formatted + ' ' + date.getFullYear()
}
document.body.innerHTML = specialFormat(new Date())
I'm a bit late to the party, but this should work:
function ordinal(number) {
number = Number(number)
if(!number || (Math.round(number) !== number)) {
return number
}
var signal = (number < 20) ? number : Number(('' + number).slice(-1))
switch(signal) {
case 1:
return number + 'st'
case 2:
return number + 'nd'
case 3:
return number + 'rd'
default:
return number + 'th'
}
}
function specialFormat(date) {
// add two weeks
date = new Date(+date + 12096e5)
var months = [
'January'
, 'February'
, 'March'
, 'April'
, 'May'
, 'June'
, 'July'
, 'August'
, 'September'
, 'October'
, 'November'
, 'December'
]
var formatted = ordinal(date.getDate())
formatted += ' ' + months[date.getMonth()]
return formatted + ' ' + date.getFullYear()
}
document.body.innerHTML = specialFormat(new Date())
edited Aug 8 '13 at 18:21
answered Aug 8 '13 at 18:15
Gabe
1265
1265
add a comment |
add a comment |
As many has mentioned, here is another answer.
This is directly based on @kennebec's answer, which I found the most simplest way to get this date Ordinal generated for given JavaScript
date:
I created two prototype function
as follows:
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Note: just include the above prototype
functions within your JS Script
and use it as described bellow.
And whenever there is a JS
date I need to generate the date with date ordinal I use that prototype method as follows on that JS
date:
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
And it will print out date with date ordinal as shown in the following live demo:
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
.
add a comment |
As many has mentioned, here is another answer.
This is directly based on @kennebec's answer, which I found the most simplest way to get this date Ordinal generated for given JavaScript
date:
I created two prototype function
as follows:
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Note: just include the above prototype
functions within your JS Script
and use it as described bellow.
And whenever there is a JS
date I need to generate the date with date ordinal I use that prototype method as follows on that JS
date:
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
And it will print out date with date ordinal as shown in the following live demo:
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
.
add a comment |
As many has mentioned, here is another answer.
This is directly based on @kennebec's answer, which I found the most simplest way to get this date Ordinal generated for given JavaScript
date:
I created two prototype function
as follows:
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Note: just include the above prototype
functions within your JS Script
and use it as described bellow.
And whenever there is a JS
date I need to generate the date with date ordinal I use that prototype method as follows on that JS
date:
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
And it will print out date with date ordinal as shown in the following live demo:
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
.
As many has mentioned, here is another answer.
This is directly based on @kennebec's answer, which I found the most simplest way to get this date Ordinal generated for given JavaScript
date:
I created two prototype function
as follows:
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Note: just include the above prototype
functions within your JS Script
and use it as described bellow.
And whenever there is a JS
date I need to generate the date with date ordinal I use that prototype method as follows on that JS
date:
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
And it will print out date with date ordinal as shown in the following live demo:
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
.
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
edited Oct 14 '15 at 20:05
answered Oct 14 '15 at 19:49
Randika Vishman
4,50723857
4,50723857
add a comment |
add a comment |
A short and compact solution:
function format(date, tmp){
return [
(tmp = date.getDate()) +
([, 'st', 'nd', 'rd'][/1?.$/.exec(tmp)] || 'th'),
[ 'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
][date.getMonth()],
date.getFullYear()
].join(' ')
}
// 14 days from today
console.log('14 days from today: ' +
format(new Date(+new Date + 14 * 864e5)));
// test formatting for all dates within a month from today
var day = 864e5, today = +new Date;
for(var i = 0; i < 32; i++) {
console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))
}
(The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)
add a comment |
A short and compact solution:
function format(date, tmp){
return [
(tmp = date.getDate()) +
([, 'st', 'nd', 'rd'][/1?.$/.exec(tmp)] || 'th'),
[ 'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
][date.getMonth()],
date.getFullYear()
].join(' ')
}
// 14 days from today
console.log('14 days from today: ' +
format(new Date(+new Date + 14 * 864e5)));
// test formatting for all dates within a month from today
var day = 864e5, today = +new Date;
for(var i = 0; i < 32; i++) {
console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))
}
(The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)
add a comment |
A short and compact solution:
function format(date, tmp){
return [
(tmp = date.getDate()) +
([, 'st', 'nd', 'rd'][/1?.$/.exec(tmp)] || 'th'),
[ 'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
][date.getMonth()],
date.getFullYear()
].join(' ')
}
// 14 days from today
console.log('14 days from today: ' +
format(new Date(+new Date + 14 * 864e5)));
// test formatting for all dates within a month from today
var day = 864e5, today = +new Date;
for(var i = 0; i < 32; i++) {
console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))
}
(The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)
A short and compact solution:
function format(date, tmp){
return [
(tmp = date.getDate()) +
([, 'st', 'nd', 'rd'][/1?.$/.exec(tmp)] || 'th'),
[ 'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
][date.getMonth()],
date.getFullYear()
].join(' ')
}
// 14 days from today
console.log('14 days from today: ' +
format(new Date(+new Date + 14 * 864e5)));
// test formatting for all dates within a month from today
var day = 864e5, today = +new Date;
for(var i = 0; i < 32; i++) {
console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))
}
(The compact regex-based approach for getting the ordinal suffix appears several places around the web, original source unknown)
function format(date, tmp){
return [
(tmp = date.getDate()) +
([, 'st', 'nd', 'rd'][/1?.$/.exec(tmp)] || 'th'),
[ 'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
][date.getMonth()],
date.getFullYear()
].join(' ')
}
// 14 days from today
console.log('14 days from today: ' +
format(new Date(+new Date + 14 * 864e5)));
// test formatting for all dates within a month from today
var day = 864e5, today = +new Date;
for(var i = 0; i < 32; i++) {
console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))
}
function format(date, tmp){
return [
(tmp = date.getDate()) +
([, 'st', 'nd', 'rd'][/1?.$/.exec(tmp)] || 'th'),
[ 'January', 'February', 'March', 'April',
'May', 'June', 'July', 'August',
'September', 'October', 'November', 'December'
][date.getMonth()],
date.getFullYear()
].join(' ')
}
// 14 days from today
console.log('14 days from today: ' +
format(new Date(+new Date + 14 * 864e5)));
// test formatting for all dates within a month from today
var day = 864e5, today = +new Date;
for(var i = 0; i < 32; i++) {
console.log('Today + ' + i + ': ' + format(new Date(today + i * day)))
}
edited May 16 '17 at 17:16
answered May 14 '17 at 19:33
Tomas Langkaas
2,5081725
2,5081725
add a comment |
add a comment |
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
add a comment |
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
add a comment |
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
Date.prototype.getMonthName = function(shorten) {
var monthsNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var monthIndex = this.getMonth();
var tempIndex = -1;
if (monthIndex == 0){ tempIndex = 0 };
if (monthIndex == 1){ tempIndex = 1 };
if (monthIndex == 2){ tempIndex = 2 };
if (monthIndex == 3){ tempIndex = 3 };
if (monthIndex == 4){ tempIndex = 4 };
if (monthIndex == 5){ tempIndex = 5 };
if (monthIndex == 6){ tempIndex = 6 };
if (monthIndex == 7){ tempIndex = 7 };
if (monthIndex == 8){ tempIndex = 8 };
if (monthIndex == 9){ tempIndex = 9 };
if (monthIndex == 10){ tempIndex = 10 };
if (monthIndex == 11){ tempIndex = 11 };
if (tempIndex > -1) {
this.monthName = (shorten) ? monthsNames[tempIndex].substring(0, 3) : monthsNames[tempIndex];
} else {
this.monthName = "";
}
return this.monthName;
};
Date.prototype.getDateWithDateOrdinal = function() {
var d = this.getDate(); // from here on I've used Kennebec's answer, but improved it.
if(d>3 && d<21) return d+'th';
switch (d % 10) {
case 1: return d+"st";
case 2: return d+"nd";
case 3: return d+"rd";
default: return d+"th";
}
};
var myDate = new Date();
// You may have to check your JS Console in the web browser to see the following
console.log("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
// or I will update the Div. using jQuery
$('#date').html("date with date ordinal: "+myDate.getDateWithDateOrdinal()+" "+myDate.getMonthName()+" "+myDate.getFullYear());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="date"></p>
answered Apr 14 '18 at 19:12
user3516819
12
12
add a comment |
add a comment |
Super simple functional implementation:
const ordinal = (d) => {
const nth = { '1': 'st', '2': 'nd', '3': 'rd' }
return `${d}${nth[d] || 'th'}`
}
const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']
const dateString = (date) => `${ordinal(date.getDate())} ${monthNames[date.getMonth()]} ${date.getFullYear()}`
// Use like this:
dateString(new Date()) // 18th July 2016
Unfortunately this method is too simple and doesn't work for days like the 21st.
– Tom
Oct 22 '16 at 23:36
add a comment |
Super simple functional implementation:
const ordinal = (d) => {
const nth = { '1': 'st', '2': 'nd', '3': 'rd' }
return `${d}${nth[d] || 'th'}`
}
const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']
const dateString = (date) => `${ordinal(date.getDate())} ${monthNames[date.getMonth()]} ${date.getFullYear()}`
// Use like this:
dateString(new Date()) // 18th July 2016
Unfortunately this method is too simple and doesn't work for days like the 21st.
– Tom
Oct 22 '16 at 23:36
add a comment |
Super simple functional implementation:
const ordinal = (d) => {
const nth = { '1': 'st', '2': 'nd', '3': 'rd' }
return `${d}${nth[d] || 'th'}`
}
const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']
const dateString = (date) => `${ordinal(date.getDate())} ${monthNames[date.getMonth()]} ${date.getFullYear()}`
// Use like this:
dateString(new Date()) // 18th July 2016
Super simple functional implementation:
const ordinal = (d) => {
const nth = { '1': 'st', '2': 'nd', '3': 'rd' }
return `${d}${nth[d] || 'th'}`
}
const monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December']
const dateString = (date) => `${ordinal(date.getDate())} ${monthNames[date.getMonth()]} ${date.getFullYear()}`
// Use like this:
dateString(new Date()) // 18th July 2016
answered Jul 19 '16 at 3:50
user2309185
1731210
1731210
Unfortunately this method is too simple and doesn't work for days like the 21st.
– Tom
Oct 22 '16 at 23:36
add a comment |
Unfortunately this method is too simple and doesn't work for days like the 21st.
– Tom
Oct 22 '16 at 23:36
Unfortunately this method is too simple and doesn't work for days like the 21st.
– Tom
Oct 22 '16 at 23:36
Unfortunately this method is too simple and doesn't work for days like the 21st.
– Tom
Oct 22 '16 at 23:36
add a comment |
Strongly inspired by @user2309185's.
const ordinal = (d) => {
return d + (['st', 'nd', 'rd'][d % 10 - 1] || 'th')
}
1
I found an issue with this,13
=13rd
– haakym
Feb 16 '17 at 22:39
add a comment |
Strongly inspired by @user2309185's.
const ordinal = (d) => {
return d + (['st', 'nd', 'rd'][d % 10 - 1] || 'th')
}
1
I found an issue with this,13
=13rd
– haakym
Feb 16 '17 at 22:39
add a comment |
Strongly inspired by @user2309185's.
const ordinal = (d) => {
return d + (['st', 'nd', 'rd'][d % 10 - 1] || 'th')
}
Strongly inspired by @user2309185's.
const ordinal = (d) => {
return d + (['st', 'nd', 'rd'][d % 10 - 1] || 'th')
}
answered Jan 26 '17 at 11:26
Adán Sánchez de Pedro Crespo
273
273
1
I found an issue with this,13
=13rd
– haakym
Feb 16 '17 at 22:39
add a comment |
1
I found an issue with this,13
=13rd
– haakym
Feb 16 '17 at 22:39
1
1
I found an issue with this,
13
= 13rd
– haakym
Feb 16 '17 at 22:39
I found an issue with this,
13
= 13rd
– haakym
Feb 16 '17 at 22:39
add a comment |
Here is an easy solution:
var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd':
(today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));
running this in scratchpad gives "today not defined."
– Nick
Jan 7 '18 at 6:22
add a comment |
Here is an easy solution:
var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd':
(today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));
running this in scratchpad gives "today not defined."
– Nick
Jan 7 '18 at 6:22
add a comment |
Here is an easy solution:
var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd':
(today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));
Here is an easy solution:
var date = today.getDate() + (today.getDate() % 10 == 1 && today.getDate() != 11 ? + 'st': (today.getDate() % 10 == 2 && today.getDate() != 12 ? + 'nd':
(today.getDate() % 10 == 3 && today.getDate() != 13 ? + 'rd':'th')));
edited Jan 7 '18 at 9:07
Ishan Thilina Somasiri
8101021
8101021
answered Jan 7 '18 at 6:00
Jay
1
1
running this in scratchpad gives "today not defined."
– Nick
Jan 7 '18 at 6:22
add a comment |
running this in scratchpad gives "today not defined."
– Nick
Jan 7 '18 at 6:22
running this in scratchpad gives "today not defined."
– Nick
Jan 7 '18 at 6:22
running this in scratchpad gives "today not defined."
– Nick
Jan 7 '18 at 6:22
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15397372%2fjavascript-new-date-ordinal-st-nd-rd-th%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
apart from a missing "of" I have no problem with that date
– mplungjan
Mar 13 '13 at 22:05
try this stackoverflow.com/questions/1056728/…
– ebram khalil
Mar 13 '13 at 22:05
I was wondering how to do exactly the opposite ? I don't want to bring up new thread so if someone has the link I'd appreciate it
– v0d1ch
Nov 25 '14 at 13:40