正如许多人提到的,这是另一个答案。
这直接基于@kennebec的回答,我找到了最简单的方法来获取为给定日期生成的OrdinalJavaScript
日期:
我创建了两个prototype function
如下:
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;
};
注意:只需将上述prototype
功能包含在您的内部JS Script
并按照如下所述使用它。
每当有JS
日期时,我需要使用日期序号生成日期,我在该日期使用该原型方法,如下所示JS
:
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());
它会打印出带有日期序号的日期,如以下现场演示所示:
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>
.