admin管理员组文章数量:1327581
When using:
Date.toLocaleTimeString()
is there any way to have it display 01:42:35 PM
instead of 1:42:35 PM
?
I have tried adding in this:
Date.toLocaleTimeString(navigator.language, {hour: '2-digit', minute:'2-digit', second: '2-digit'})
but it still only produces a single digit hour (unless it is after 10).
When using:
Date.toLocaleTimeString()
is there any way to have it display 01:42:35 PM
instead of 1:42:35 PM
?
I have tried adding in this:
Date.toLocaleTimeString(navigator.language, {hour: '2-digit', minute:'2-digit', second: '2-digit'})
but it still only produces a single digit hour (unless it is after 10).
Share Improve this question asked Aug 26, 2016 at 17:40 rjbogzrjbogz 8701 gold badge16 silver badges36 bronze badges 1- There is no Date.toLocaleTimeString, there is Date.prototype.toLocaleTimeString though. It's part of the internationalisation API and not supported by all browsers in use. – RobG Commented Aug 28, 2016 at 22:50
4 Answers
Reset to default 4If my reading is right, according to the relevant spec, it appears this is intentional.
- Else if f is "2-digit", then
- Let fv be FormatNumber(nf2, v).
- If the length property of fv is greater than 2, let fv be the substring of fv containing the last two characters.
I can't understand why it would be specified that way, but there it is. If you want to do zero-padding, it seems you'll just need to prepend it yourself.
It worked in this way for me
var datelocal = new Date(2016, 01, 01, 1, 1, 1).toLocaleTimeString(navigator.language, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
i'm also attached a plunker.
NOTE: you have to provide new Date() instead of passing date parameters to Date method to get the current date.
Instead of relying on Date.prototype.toLocaleTimeString or similar functions to get the right format in such situations, I tend to use Moment.js in my JavaScript project. Moment.js is a JavaScript library which is specifically designed to parse, validate, manipulate and display dates. To get a string for the time in the format you described, you could use:
var s = moment().format('hh:mm:ss A');
console.log(s);
This uses the current date. If you want to use another date, you can do so, too:
var s = moment(new Date(2016, 8, 19, 1, 10)).format('hh:mm:ss A');
console.log(s);
It saves you the hassle of doing your own zero-padding and other similar stuff.
The correct answer is to use "2-digit" in hour
const options = {hour: '2-digit', minute: '2-digit', second: '2-digit'};
function ut() {
let d = new Date();
document.getElementById('time').innerHTML = d.toLocaleTimeString('de-DE', options);
}
window.setInterval(ut, 1000);
#time {
font-size: 40px;
text-align: center;
white-space: nowrap;
color: black
}
<div id="time"></div>
本文标签: dateJavaScript display hour with a zero before it if it is less than 10Stack Overflow
版权声明:本文标题:date - JavaScript display hour with a zero before it if it is less than 10 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742168015a2426190.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论