如何只显示javascript date.toLocaleTimeString()的小时和分钟? [英] How to show only hours and minutes from javascript date.toLocaleTimeString()?

查看:246
本文介绍了如何只显示javascript date.toLocaleTimeString()的小时和分钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我如何从HH:MM:SS am / pm格式获得HH:MM am / pm格式。

Can any body please help me how to get the HH:MM am/pm format from HH:MM:SS am/pm format.

   My javascript code is :

     function prettyDate2(time){
        var date = new Date(parseInt(time));
        var localeSpecificTime = date.toLocaleTimeString();
        return localeSpecificTimel;
      } 

但它以这种格式返回时间HH:MM:SS am / pm。但我的客户要求是HH:MM am / pm。请帮帮我。谢谢。

But it returns time in this format HH:MM:SS am/pm .But my client requirement is HH:MM am/pm. Please help me.Thanks in advance.

推荐答案

你可以这样做:

function prettyDate2(time){
    var date = new Date(parseInt(time));
    var localeSpecificTime = date.toLocaleTimeString();
    return localeSpecificTime.replace(/:\d+ /, ' ');
}

正则表达式正在从该字符串中剥离秒数。

The regex is stripping the seconds from that string.

来自 @ CJLopez的答案的更通用版本:

function prettyDate2(time) {
  var date = new Date(parseInt(time));
  return date.toLocaleTimeString(navigator.language, {
    hour: '2-digit',
    minute:'2-digit'
  });
}

这篇关于如何只显示javascript date.toLocaleTimeString()的小时和分钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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