How to get temporary local time TimeZone using moment.js

I work with two js libraries to get browser timezone id and local browser time

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jstimezonedetect/1.0.4/jstz.min.js"></script> <script src="Scripts/moment-timezone.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function () { var tz = jstz.determine(); // Determines the time zone of the browser client alert(tz.name()); alert(moment.tz(tz.name()).format()); }); </script> 

this code below returns the perfect timezone id

  var tz = jstz.determine(); // Determines the time zone of the browser client alert(tz.name()); 

but this code does not work alert(moment.tz(tz.name()).format()); does not give the user local time.

Am I missing something in the code? Do I need to add any file related to other events?

please guide me. I want to get the user's local time using moment.js. thanks

Working version of UPDATE

 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script> <script type="text/javascript" src="Scripts/jstz.min.js"></script> <script src="Scripts/moment.min.js" type="text/javascript"></script> <script src="Scripts/moment-timezone-with-data-2010-2020.min.js" type="text/javascript"></script> </head> <body> <form method="post" action="WebForm1.aspx" id="form1"> <div> <script type="text/javascript"> $(document).ready(function () { var tz = jstz.determine(); // Determines the time zone of the browser client alert(tz.name()); var format = 'YYYY/MM/DD HH:mm:ss ZZ'; alert(moment.tz('Europe/London').format(format)); alert(moment.tz(tz.name()).format(format)); }); </script> </div> </form> </body> </html> 
+5
source share
1 answer

My solution to this case (this is part of the page code):

 function toLocalTime(time) { if (time <= 0) return ''; var m = moment.tz(time, 'America/Chicago'); //CDT var tz = jstz.determine(); // Determines the time zone of the browser client m.tz(tz.name()); // Convert CDT to local time return m.format('HH:mm:ss'); } 
 <script src="js/jstz.min.js"></script> <script src="js/moment.js"></script> <script src="js/moment-timezone-with-data-2010-2020.js"></script> ........... <script th:inline="javascript">document.write(toLocalTime(<<TIME IN MILLISECONDS HERE>>));</script> 
+2
source

Source: https://habr.com/ru/post/1208871/


All Articles