Today I m using .toLocaleTimeString()
, which by default returns something like "08:30:00" - based on the users browser locale.
you can force a locale like this:
(new Date()).toLocaleTimeString("de-DE")
第2个选项参数更令人感兴趣:
(new Date()).toLocaleTimeString(
"de-DE",
{
hours: "2-digit",
minutes: "2-digit",
seconds: "2-digit",
}
)
如果在备选办法中删除<代码>seconds,则只印制小时数和分钟。
So based on the original question, today I would use this:
var current;
window.onload = function () {
current = new Date();
document.getElementById("clock")
.innerHTML = current.toLocaleTimeString(
navigator.language,
{
hours: "2-digit",
minute: "2-digit"
}
);
• 小型清洁/园:
var current;
var timeOptions = {
hours: "2-digit",
minute: "2-digit"
};
window.onload = function () {
current = new Date();
document
.getElementById("clock")
.innerHTML = current.toLocaleTimeString(navigator.language, timeOptions);