在Javascript中将时间转换为24小时格式 [英] Convert time to 24 hours format in Javascript

查看:124
本文介绍了在Javascript中将时间转换为24小时格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将JavaScript的新Date()中的本地时间格式隔离并转换为24小时格式?

How can I isolate and convert the local time format in JavaScript's new Date() to a 24 hour format?

示例: 2016-09-22T23:21:56.027Z 刚刚成为当地时间的 22:56

Example: 2016-09-22T23:21:56.027Z just becomes 22:56 in local time.

推荐答案

new Date("2000-01-01 10:30 AM").getHours() // 10

这个 24小时:

new Date("2000-01-01 10:30 PM").getHours() // 22

如果你想要一个更一般的东西:

If you want a more general thing:

function get_hours(time_string) {
    return new Date("2000-01-01 " + time_string).getHours() // 22
}

这篇关于在Javascript中将时间转换为24小时格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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