How to get time from dynamic time zone using moment.js

时间:2016-04-04 17:38:12

标签: php jquery timezone momentjs

I want to get user local time from his timezone. For this i am using following code :

<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>

I got accurate timezone from this code but did not able to get actual time from current timezone. I have included moment.js and jstz.js in my code already. How can i get the user local time ?

1 个答案:

答案 0 :(得分:1)

You do not need to actually know what the time zone identifier is to work in the local time zone, so there's no need to use jsTimeZoneDetect (aka jstz).

Since local time is the default, you can just do:

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