[英]javascript time 24 hour to 12 hour with a click button
I want to make a javascript code that will show time in 12 hour format (with AM/PM) by default. 我想制作一个JavaScript代码,默认情况下以12小时格式(含AM / PM)显示时间。 And there'll be a button below that, and when i click that button that'll change the time to 24 hour format (Military time).
在其下方将有一个按钮,当我单击该按钮时,该时间将变为24小时制(军事时间)。 Again when I click that button, the time will be changed to previous 12 hour format.
再次单击该按钮时,时间将更改为以前的12小时格式。 How can I do that.
我怎样才能做到这一点。
Here is the link of my code- code 这里是我的代码-链接代码
What i want to do here is- when i click that "ab" button it'll change the first time (12 hour) to 24 hour format. 我想在这里做的是-当我单击该“ ab”按钮时,它将第一次(12小时)更改为24小时格式。 And again when I click that button the first time will be changed to the previous 12 hour format.
再次单击该按钮时,第一次将更改为以前的12小时格式。
The javascript Date when initialized using something like var currentDate = new Date();
使用类似
var currentDate = new Date();
初始化javascript 日期 var currentDate = new Date();
will hold the current date and time. 将保留当前日期和时间。 You can then use the
currentDate.getHours()
method to get the hours in 24 hour format. 然后,您可以使用
currentDate.getHours()
方法以24小时格式获取小时数。
Changing this to 12 hour format first involves checking whether the hour is greater than or equal to 12 (in which case the suffix will be PM, otherwise it will be AM). 将其更改为12小时格式首先需要检查小时是否大于或等于12(在这种情况下,后缀为PM,否则为AM)。 After this you just modulo the hours by 12.
在此之后,您只需将小时数取模12。
The minutes and seconds can be retrieved using currentDate.getMinutes()
and currentDate.getSeconds()
respectively. 分钟和秒可以分别使用
currentDate.getMinutes()
和currentDate.getSeconds()
检索。
Here is a fiddle that demonstrates the above. 这是证明上述内容的小提琴 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.