如何获取当前时间格式hh:mm AM / PM in Javascript? [英] How to get current time in a format hh:mm AM/PM in Javascript?

查看:139
本文介绍了如何获取当前时间格式hh:mm AM / PM in Javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Javascript,我需要粘贴当前时间格式为HH:MM AM / PM。有一个catch - 我需要把时间从现在开始,在两个小时之内,所以例如,而不是下午7:23我需要下午9:23等。

I have a Javascript in which I need to paste the current time in a format HH:MM AM/PM. There's one catch - I need to put the time that starts in two hours from now, so for example, instead of 7:23PM I need to put 9:23PM, etc.

我试图做一些事情: var dateFormat = new Date(hh:mm a)但它没有工作。我也尝试使用:

I tried to do something like: var dateFormat = new Date("hh:mm a") but it didn't work. I also tried to use:

var today = new Date();
var time = today.toLocaleTimeString().replace(/([\d]+:[\d]{2})(:[\d]{2})(.*)/, "$1$3")
alert(time);

但是我看到的都是例如18:23而不是6:23 PM(可能是因为toLocaleTimeString()和我在欧洲的位置) - 也许有一些统一的方式来做,这将在世界各地工作?此外,我不知道如何添加2个小时的最终结果。你可以帮我吗?
谢谢!

but all I've seen was e.g. 18:23 instead of 6:23 PM (probably because of toLocaleTimeString() and my location in Europe) - maybe there's some unified way to do that that will work all around the World?. Also, I don't know exactly how to add the 2 hours to the final result. Can you help me? Thanks!

推荐答案

使用 Date 检索时间并构造一个时间字符串,这些代码片段。

Use Date methods to set and retrieve time and construct a time string, something along the lines of the snippet.

[编辑]只是为了好玩:添加了一个更通用的方法,使用2 Date.prototype extension。

[edit] Just for fun: added a more generic approach, using 2 Date.prototype extensions.

var now = new Date();
now.setHours(now.getHours()+2);
var isPM = now.getHours() >= 12;
var isMidday = now.getHours() == 12;
var result = document.querySelector('#result');
var time = [now.getHours() - (isPM && !isMidday ? 12 : 0), 
            now.getMinutes(), 
            now.getSeconds() || '00'].join(':') +
           (isPM ? ' pm' : 'am');
            
result.innerHTML = 'the current time plus two hours = '+ time;

// a more generic approach: extend Date
Date.prototype.addTime = addTime;
Date.prototype.showTime = showTime;

result.innerHTML += '<h4>using Date.prototype extensions</h4>';
result.innerHTML += 'the current time plus twenty minutes = '+ 
                      new Date().addTime({minutes: 20}).showTime();
result.innerHTML += '<br>the current time plus one hour and twenty minutes = '+ 
                      new Date().addTime({hours: 1, minutes: 20}).showTime();
result.innerHTML += '<br>the current time <i>minus</i> two hours (format military) = '+ 
                      new Date().addTime({hours: -2}).showTime(true);
result.innerHTML += '<br>the current time plus ten minutes (format military) = '+ 
                      new Date().addTime({minutes: 10}).showTime(true);


function addTime(values) {
  for (var l in values) {
    var unit = l.substr(0,1).toUpperCase() + l.substr(1);
    this['set' + unit](this['get' + unit]() + values[l]);
  }
  return this;
}

function showTime(military) {
  var zeroPad = function () {
    return this < 10 ? '0' + this : this;
  };
  
  if (military) {
    return [ zeroPad.call(this.getHours()),
             zeroPad.call(this.getMinutes()),
             zeroPad.call(this.getSeconds()) ].join(':');
  }
  var isPM = this.getHours() >= 12;
  var isMidday = this.getHours() == 12;
  return time = [ zeroPad.call(this.getHours() - (isPM && !isMidday ? 12 : 0)),
                  zeroPad.call(this.getMinutes()),
                  zeroPad.call(this.getSeconds()) ].join(':') +
                (isPM ? ' pm' : ' am');

  
 
}

<div id="result"></div>

这篇关于如何获取当前时间格式hh:mm AM / PM in Javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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