简体   繁体   English

如何在12小时内检索当前的“ DateTime”?

[英]How can I retrieve the current `DateTime` in 12-hour time?

What is best way to get the current datetime as string from JavaScript formatted like: 从JavaScript格式中获取当前日期时间作为字符串的最佳方法是:

11/5/2011 1:56:44 PM

instead of (24-hour / "military" time) 而不是(24小时/“军事”时间)

11/5/2011 13:56:44

Demo: http://jsfiddle.net/k6g4g/ 演示: http//jsfiddle.net/k6g4g/

var date = new Date,
    day = date.getDate(),
    month = date.getMonth() + 1,
    year = date.getFullYear(),
    hour = date.getHours(),
    minute = date.getMinutes(),
    seconds = date.getSeconds(),
    ampm = hour > 12 ? "PM" : "AM";

hour = hour % 12;
hour = hour ? hour : 12; // zero = 12

minute = minute > 9 ? minute : "0" + minute;
seconds = seconds > 9 ? seconds : "0" + seconds;
hour = hour > 9 ? hour : "0" + hour;


date = month + "/" + day + "/" + year + " " + hour + ":" + minute + ":" + seconds + " " + ampm;
// date holds "12/16/2011 08:14:30 PM"

The link below has a formatDate() method that will help you: 下面的链接具有formatDate()方法,可以帮助您:

http://www.mattkruse.com/javascript/date/ http://www.mattkruse.com/javascript/date/

You can use this to format it however you like before returning it. 您可以使用它来格式化它,然后再返回它。

function formatTime(time, options) {
            month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
            var a_p = "";
            var Time = {};
            Time.hour = new Date(time).getHours(); //military time?
            if (Time.hour < 12) {
                a_p = "AM";
            } else {
                Time.hour = Time.hour - 12;
                a_p = "PM";
            }
            Time.minute = new Date(time).getMinutes();
            Time.minute = Time.minute + "";
            if (Time.minute.length == 1) {
                Time.minute = "0" + Time.minute
            }
            Time.month = new Date(time).getMonth();
            Time.month = month_names[Time.month];
            Time.date = new Date(time).getDate();
            Time.year = new Date(time).getFullYear();
            formattedTime = Time.month + ' ' + Time.date + ', ' + Time.year + ' &#8212; ' + Time.hour + ':' + Time.minute + a_p;
            return formattedTime
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用正则表达式(正则表达式)验证 12 小时时间 - How to validate 12-Hour Time with regex (regular Expression) 显示 12 小时制和 24 小时制时间 - Display 12-hour and 24-hour time JavaScript以12小时格式随机生成时间 - JavaScript random generate time in 12-hour format Javascript:将 24 小时时间字符串转换为带有 AM/PM 且无时区的 12 小时时间 - Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone Javascript -- 检测用户的区域设置是否设置为使用 12 小时或 24 小时时间格式 - Javascript -- Detect if user's locale are set to use 12-hour or 24-hour time format Moment.js接受12小时和24小时时间 - Moment.js Accept both 12-hour and 24-hour time JavaScript 确定浏览器是否使用 12 小时或 24 小时格式显示时间输入 - JavaScript to determine if browser is displaying the time input using a 12-hour or 24-hour format 整小时和小数小时将双精度值转换为12小时制时钟格式时间 - Whole and fractional hours Double value convert to 12-hour Clock Format time 以 12 小时制显示当前时间? - Javascript - Display current time in 12 hour time? - Javascript 如何在高图表中显示从当前时间(hh:mm)12小时格式开始的最近五个小时的时间? - how to display the last five hours time from current time(hh:mm ) 12 hour format in high charts?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM