如何使用 .toLocaleTimeString() 而不显示秒数? [英] How do I use .toLocaleTimeString() without displaying seconds?

查看:19
本文介绍了如何使用 .toLocaleTimeString() 而不显示秒数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试显示用户的时间而不显示秒数.有没有办法使用 Javascript 的 .toLocaleTimeString() 来做到这一点?

I'm currently attempting to display the user's time without displaying the seconds. Is there a way I can do this using Javascript's .toLocaleTimeString()?

做这样的事情:

var date = new Date();
var string = date.toLocaleTimeString();

将显示每个单位的用户时间,例如目前它显示下午 3:39:15.我可以显示相同的字符串,只是没有秒吗?(例如下午 3:39)

will display the user's time with every unit, e.g. currently it displays 3:39:15 PM. Am I able to display that same string, just without the seconds? (e.g. 3:39 PM)

推荐答案

您可以随时设置选项,基于此 page 你可以设置,去掉秒,像这样

You can always set the options, based on this page you can set, to get rid of the seconds, something like this

var dateWithouthSecond = new Date();
dateWithouthSecond.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});

受 Firefox、Chrome、IE9+ 和 Opera 支持.在您的网络浏览器控制台上试试.

Supported by Firefox, Chrome, IE9+ and Opera. Try it on your web browser console.

这篇关于如何使用 .toLocaleTimeString() 而不显示秒数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆