如何自定义时间和日期?
How to make custom time and date?
我查看了 this 代码,他们似乎只是每天用 HTML 更新他们的代码,但我知道有一种方法可以通过将星期一更改为 Morndas 来自定义日期, Sunday to be Sundas, January as Morning Star, 等等。我将如何为我自己的网站编写代码,因为它是 Elder Scrolls 网站上的一个很酷的小功能,而且我并不总是可以每晚更新它半夜。
谢谢
我认为这最好使用服务器端脚本来完成,例如 PHP。但下面我给出了一个 Javascript 实现,您可以轻松地嵌入它。一旦您了解了服务器端脚本,将此代码翻译成另一种语言应该是微不足道的。
window.addEventListener('DOMContentLoaded', function() {
// Function to determine the postfix of the month number
function numberPostFix(nr) {
nr = nr % 100;
if (nr >= 10 && nr < 20) return 'th';
nr = nr % 10;
if (nr == 1) return 'st';
if (nr == 2) return 'nd';
if (nr == 3) return 'rd';
return 'th';
}
// Arrays of custom month names.
var monthNames = [
"Morning Star",
"Sun's Dawn",
"First Seed",
"Rain's Hand",
"Second Seed",
"Mid Year",
"Sun's Height",
"Last Seed",
"Hearthfire",
"Frostfall",
"Sun's Dusk",
"Evening Star"
];
// Array of custom weekday names.
var dayNames = [
"Sundas",
"Morndas",
"Tirdas",
"Middas",
"Turdas",
"Fredas",
"Loredas"
];
// Get all relevant parts.
var date = new Date();
var month = date.getMonth();
var monthday = date.getDate();
var weekday = date.getDay();
// Construct the day text.
var dateHtml = "Today is " + dayNames[weekday] + ", " + monthday + "<sup>" + numberPostFix(monthday) + "</sup> of " + monthNames[month];
// Write to document.
document.getElementById('date').innerHTML = dateHtml;
});
<header>
<h2>Welcome, this is your page header.</h2>
<span id="date"></span>
</header>
我查看了 this 代码,他们似乎只是每天用 HTML 更新他们的代码,但我知道有一种方法可以通过将星期一更改为 Morndas 来自定义日期, Sunday to be Sundas, January as Morning Star, 等等。我将如何为我自己的网站编写代码,因为它是 Elder Scrolls 网站上的一个很酷的小功能,而且我并不总是可以每晚更新它半夜。
谢谢
我认为这最好使用服务器端脚本来完成,例如 PHP。但下面我给出了一个 Javascript 实现,您可以轻松地嵌入它。一旦您了解了服务器端脚本,将此代码翻译成另一种语言应该是微不足道的。
window.addEventListener('DOMContentLoaded', function() {
// Function to determine the postfix of the month number
function numberPostFix(nr) {
nr = nr % 100;
if (nr >= 10 && nr < 20) return 'th';
nr = nr % 10;
if (nr == 1) return 'st';
if (nr == 2) return 'nd';
if (nr == 3) return 'rd';
return 'th';
}
// Arrays of custom month names.
var monthNames = [
"Morning Star",
"Sun's Dawn",
"First Seed",
"Rain's Hand",
"Second Seed",
"Mid Year",
"Sun's Height",
"Last Seed",
"Hearthfire",
"Frostfall",
"Sun's Dusk",
"Evening Star"
];
// Array of custom weekday names.
var dayNames = [
"Sundas",
"Morndas",
"Tirdas",
"Middas",
"Turdas",
"Fredas",
"Loredas"
];
// Get all relevant parts.
var date = new Date();
var month = date.getMonth();
var monthday = date.getDate();
var weekday = date.getDay();
// Construct the day text.
var dateHtml = "Today is " + dayNames[weekday] + ", " + monthday + "<sup>" + numberPostFix(monthday) + "</sup> of " + monthNames[month];
// Write to document.
document.getElementById('date').innerHTML = dateHtml;
});
<header>
<h2>Welcome, this is your page header.</h2>
<span id="date"></span>
</header>