简体   繁体   中英

Setting a different timezone for client in javascript

I wanted to know if this is possible?

I looked everywhere and I don't want to change the time integer of the Date object because I use it at service requests.

I encounter this problem with some other devices. new Date() from device returns Fri Nov 02 2018 09:27:33 GMT+0200 (EET) and new Date() from chrome returns Fri Nov 02 2018 10:27:33 GMT+0200 (EET)

but new Date().getTime() from device and new Date().getTime() from chrome returns same value and I want this value to stay like this.

There is a similar question to this one, so i sugest you to browser or to click on this link so you can view enought answers to your preocupation. Thanks for following the link and upvoting this answers so it can help other to trust the link

How to format a JavaScript date

You can use libraries like Moment-js and Moment-timezone js

I wrote a example code

 <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.23/moment-timezone-with-data-2012-2022.min.js"></script> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> </head> <body> <div>time of My Country is <span id="mycountryTime"></span></div> <div>time of New York is <span id="newyorkTime"></span></div> </body> <script> $(document).ready(function(){ var mycountryTime = moment(); var newyorkTime = moment.tz(moment(), "America/New_York"); $('#mycountryTime').html(mycountryTime.format()); $('#newyorkTime').html(newyorkTime.format()); }); </script> </html>

moment-js is for formatting date. moment-timezone js is for getting date based on timezone.

more details on this are in https://momentjs.com/docs/

I hope it would be helpful.


and you don't need to set date for everywhere each client's browser will make date about their own country

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM