【问题标题】:change time format to 24 hours in javascript在javascript中将时间格式更改为24小时
【发布时间】:2014-03-12 09:46:38
【问题描述】:

我有一个时间格式,例如:2011 年 12 月 16 日下午 3:49:37,我得到了这个格式:

var newDate = new Date(timeFromat);
timeFormat = newDate.toLocaleString();

我的实际格式是格林威治标准时间,我使用上面的代码将其转换为我的鲍尔斯时间。

我想将其更改为 24 小时时间格式,因此我希望将此日期更改为:2011 年 12 月 16 日 15:49:37,并且我想在 javascript 中进行。

我就是这么做的

var firstPartOftimeFormat = timeFormat.substring(0,9);
var secondPartOftimeFormat = timeFormat.substring(10,20);

但是当日期格式如下时它不起作用:3/16/2011。但以下部分有效。

var time = $("#starttime").val();
var hours = Number(secondPartOftimeFormat.match(/^(\d+)/)[1]);
var minutes = Number(secondPartOftimeFormat.match(/:(\d+)/)[1]);
var AMPM = secondPartOftimeFormat.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
alert(sHours + ":" + sMinutes);

您能建议其他方法吗?

谢谢

【问题讨论】:

  • 各种解决你的问题:stackoverflow.com/questions/1056728/…
  • 当您需要特定的输出格式时,为什么要使用toLocaleString(对于不同的语言环境表现不同)?我会在这里使用手动格式。
  • 不设置任何时间格式 javascript Date 默认返回时间为24小时。
  • 谢谢,但我的时间格式是格林威治标准时间,我通过 var newDate = new Date(timeFromat); 将其转换为我的浏览器时间。 timeFormat = newDate.toLocaleString();时间是过去的时间。

标签: javascript


【解决方案1】:

使用dateObj.toLocaleString([locales[, options]])

选项 1 - 使用语言环境

var date = new Date();
console.log(date.toLocaleString('en-GB'));

选项 2 - 使用选项

var options = { hour12: false };
console.log(date.toLocaleString('en-US', options));

【讨论】:

  • 由于某种原因现在似乎不起作用,它仍然以 12 小时格式返回时间。
  • 应该选答案!
  • 我错误地先尝试了date.toLocaleString(options),虽然没用但没有失败,后来改成date.toLocaleString('en-US', options),确实有效。
【解决方案2】:

hourCycle: 'h23' 是完美的

感谢https://stackoverflow.com/users/12425695/luis-serrano

new Date().toLocaleString('ru-RU', {
    timeZone: 'Europe/Moscow',
    hourCycle: 'h23',
    year: "numeric",
    month: "2-digit",
    day: "2-digit",
    hour: "2-digit",
    minute: "2-digit",
    second: "2-digit"
});

【讨论】:

    【解决方案3】:

    要获取 24 小时格式:

    const date = new Date();
    console.log(date.toLocaleTimeString([], {
        hourCycle: 'h23',
        hour: '2-digit',
        minute: '2-digit'
    }));

    【讨论】:

      【解决方案4】:

      您可以使用以下代码获取 24 小时格式

      new Date("3/16/2011 3:49:37 PM").getHours() // 15
      new Date("3/16/2011 3:49:37 PM").getMinutes() // 49
      

      【讨论】:

        【解决方案5】:

        使用一些资源
        Date/toLocaleDateStringMDN
        Date/toLocaleTimeStringMDN

        const lang = navigator.language || navigator.languages[0];
        const date = new Date();
        const date_locale = date.toLocaleDateString(lang, {
          day: 'numeric',
          month: 'short',
          year: 'numeric'
        });
        const time_locale = date.toLocaleTimeString(lang);
        
        const formatted = `${date_locale} ${time_locale}`;
        console.log(formatted)

        上面我们从Window's Navigator 对象推断出当前语言。
        如果lang 最终变成undefined,那很好,将使用默认值。

        强制使用所需格式,您可以手动将lang 设置为:'en-US''eu''en-GB''de-DE''hr-HR' 等...

        这是一个时间示例:

        const date = new Date();
        console.log(date.toLocaleTimeString('en-US')) // 12h
        console.log(date.toLocaleTimeString('en-GB')) // 24h
        console.log(date.toLocaleTimeString())        // Default

        【讨论】:

          【解决方案6】:

          试试这个:

          // British English uses day-month-year order and 24-hour time without AM/PM
          console.log(date.toLocaleString('en-GB'));
          // → "20/12/2012 03:00:00"
          

          参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString

          【讨论】:

            【解决方案7】:

            试试这个,它对我来说非常好用。 它提供 24 小时时间格式

            var Date= new Date();
            
            var TimeFormat= date.toLocaleString('en-GB');
            

            你的答案是 2018 年 12 月 14 日星期五 18:00:00 GMT+0530(印度标准时间)

            【讨论】:

              【解决方案8】:

              试试这个

              var d = new Date();
              alert(d.getHours());
              alert(d.getMinutes());
              

              【讨论】:

                【解决方案9】:

                试试这个功能

                <script type="text/javascript">
                    <!--
                    function displayTime() {
                        var currentDate = new Date();
                        var currentHour = currentDate.getHours();
                        var currentMinute = currentDate.getMinutes();
                        var currentSecond = currentDate.getSeconds();
                        document.getElementById('timeDiv').innerHTML = 'Hour: ' + currentHour + ' Minute: ' + currentMinute + ' Second: ' + currentSecond;
                    }
                
                    window.onload = function() {
                        window.setInterval('displayTime()', 1000);
                    }
                    // -->
                </script>
                

                【讨论】:

                • 谢谢,但我的时间格式是格林威治标准时间,我通过 var newDate = new Date(timeFromat); 将其转换为我的浏览器时间。 timeFormat = newDate.toLocaleString();时间是过去的时间。但你的解决方案不起作用。因为当我在 timeFormat = newDate.toLocaleString().getHourse(); 中应用 getHours() 时它不起作用
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2010-12-19
                • 2021-08-05
                • 1970-01-01
                • 2018-10-26
                • 1970-01-01
                相关资源
                最近更新 更多