【问题标题】:How to get time from dynamic time zone using moment.js如何使用 moment.js 从动态时区获取时间
【发布时间】:2016-07-24 09:15:21
【问题描述】:

我想从他的时区获取用户当地时间。为此,我使用以下代码:

<script type="text/javascript">
            $(document).ready(function(){
                var tz = jstz.determine(); // Determines the time zone of the browser client      
                var timestamp = '<?php echo $value->created_at->sec; ?>';        
                var localtime = moment.unix(timestamp).utcOffset(tz).format('MM/DD/YY hh:mm');  
                console.log(timestamp+"==>"+tz.name()+'==>'+localtime);        

            });        

            </script>

我从这段代码中获得了准确的时区,但无法从当前时区获得实际时间。 我已经在我的代码中包含了 moment.js 和 jstz.js。 如何获取用户当地时间?

【问题讨论】:

    标签: php jquery timezone momentjs


    【解决方案1】:

    您实际上不需要知道在本地时区工作的时区标识符是什么,因此无需使用 jsTimeZoneDetect(又名jstz)。

    由于本地时间是默认时间,您可以这样做:

    moment.unix(timestamp).format('MM/DD/YY hh:mm')
    

    【讨论】:

      猜你喜欢
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-03
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      相关资源
      最近更新 更多