简体   繁体   English

javascript时间,点击按钮24小时至12小时

[英]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.

相关问题 使用 Javascript 将 24 小时制 JSON 数据转换为 12 小时制 - Converting JSON data 24 hour time to 12 hour with Javascript 12 小时 Javascript 时钟显示 24 时间,错误的 AM/PM - 12 hour Javascript Clock Showing 24 time, Wrong AM/PM Javascript:将 24 小时时间字符串转换为带有 AM/PM 且无时区的 12 小时时间 - Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone 使用 Javascript 将 24 小时制转换为 12 小时制 - Converting 24 hour time to 12 hour time w/ AM & PM using Javascript 如何使用javascript将时间从24小时格式转换为12小时格式? - How to convert time from 24 hour format to 12 hour format using javascript? Javascript -- 检测用户的区域设置是否设置为使用 12 小时或 24 小时时间格式 - Javascript -- Detect if user's locale are set to use 12-hour or 24-hour time format JavaScript 确定浏览器是否使用 12 小时或 24 小时格式显示时间输入 - JavaScript to determine if browser is displaying the time input using a 12-hour or 24-hour format 显示 12 小时制和 24 小时制时间 - Display 12-hour and 24-hour time 新的Date()12/24小时格式的JavaScript? - new Date() 12/24 hour format JavaScript? Javascript 12 小时到 24 小时格式转换器 - Javascript 12 Hour to 24 Format convertor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM