简体   繁体   English

当部署在Amazon Web Services上时,如何在ExpressJS应用中获取浏览器时区?

[英]How can I get the browser timezone in my ExpressJS app when deployed on Amazon Web Services?

I've built a small chat room app using Express JS that I deployed on an AWS EC2 instance. 我使用部署在AWS EC2实例上的Express JS构建了一个小型聊天室应用程序。 When I developed the app, I was able to grab a local date from my machine to use to generate time stamps in the chat window. 开发应用程序时,我可以从机器上获取本地日期,以在聊天窗口中生成时间戳。 However, now that I've deployed my machine to AWS, the time stamps generated are that of the virtual machine, and not that of the client. 但是,既然我已经将机器部署到了AWS,那么生成的时间戳就是虚拟机的时间戳,而不是客户端的时间戳。

I've tried using Moment JS and Moment Timezone JS to generate time stamps -- I've tried using moment.tz.guess() client-side -- all which work fine when locally deployed, but I'm still getting the UTC timezone and inaccurate time stamps on my AWS instance. 我已经尝试过使用Moment JS和Moment Timezone JS来生成时间戳记-我已经尝试过使用moment.tz.guess()客户端-所有这些在本地部署时都可以正常工作,但是我仍然可以使用UTC我的AWS实例上的时区和不正确的时间戳。 My desired behavior is to get the timestamp of the browser sending the request. 我想要的行为是获取发送请求的浏览器的时间戳。

I've looked all over StackOverflow, and I'm sorry if I'm missing something or am not fully understanding things. 我到处都是StackOverflow,如果我缺少某些东西或没有完全理解这些东西,我感到抱歉。 All of the methods I could find give me my local system time, but once deployed, my timestamps are off, reflecting that of the AWS instance. 我可以找到的所有方法都给了我本地系统时间,但是一旦部署,我的时间戳就会消失,反映出AWS实例的时间戳。

How can I get the client's time to appear as the timestamp, rather than the time of the server? 如何获得客户端的时间显示为时间戳,而不是服务器的时间? Should I change my AWS instance timezone settings? 我应该更改我的AWS实例时区设置吗? Even if I did change the timezone on the AWS server, how would I be able to gather the timezone for a user whom connects outside of the timezone in which the virtual machine is living? 即使我确实更改了AWS服务器上的时区,如何为在虚拟机所在时区之外连接的用户收集时区?

Thank you genuinely for any help pointing me in the right direction. 衷心感谢您为我指明正确方向的任何帮助。 I've got myself confused and my strategy is likely incorrect :( 我很困惑,我的策略可能不正确:(

I was able to resolve my issue to get my chat app to display client time, not server time, without changing server time in any way. 我能够解决问题,使我的聊天应用显示客户端时间,而不是服务器时间,而无需以任何方式更改服务器时间。

Here's what I did: 这是我所做的:

Imported Moment.JS library into my client side: 将Moment.JS库导入我的客户端:

<script src="/moment/moment.js"></script>
<script src="/moment-timezone/builds/moment-timezone-with-data.js"></script>

Converted server message timestamp to client timezone: 服务器消息时间戳转换为客户端时区:

message.timestamp = moment(message.timestamp).tz(moment.tz.guess()).format('h:mm:ss A');

The timestamp recorded by the server ( message.timestamp ) is converted to whatever timezone that moment.js can gather ( moment.tz.guess() ). 服务器记录的时间戳( message.timestamp )转换为moment.js可以收集的任何时区( moment.tz.guess() )。 And then is converted to my desired format. 然后转换为我想要的格式。

I did try to spoof it with a VPN but it appears not to use geo-locating but literally gathers it from the user's system time. 我曾尝试用VPN欺骗它,但它似乎不使用地理定位,而是从用户的系统时间收集它。 So even connected via a VPN from the other side of the country, I was still getting local system time. 因此,即使通过来自该国另一端的VPN连接,我仍然可以获取本地系统时间。 I was able to confirm conversion is working, as a family member in another timezone was able to try my app and saw that the time did successfully convert. 我能够确认转换正在正常进行,因为另一个时区的家庭成员能够尝试我的应用程序,并且看到时间确实转换成功 Previously on my deployed machine the timestamp would not match my local machine. 以前在部署的计算机上,时间戳记与本地计算机不匹配。 But now, it converts to my timezone and to others' -- Cool! 但是现在,它转换为我的时区和其他人的-酷!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该如何为我的应用程序构建Amazon Web Services? - How should I architect Amazon Web Services for my app? 如何通过Amazon Web Services获得上传成功状态? - How can I get upload success status with Amazon Web Services? 如何限制对部署到 Amazon Web Services 的网站的访问? - How to limit access to web site deployed to Amazon Web Services? 如何使用Amazon SES / Web服务创建电子邮件自动回复应用程序? - How can I create an email autoresponder app using Amazon SES/web services? 如何在Java应用程序中使用Amazon Web Services策略声明? - How can I use Amazon Web Services policy statement in my Java application? 如何控制使用Amazon Web Services通过AutoScalingGroup删除哪些EC2实例? - How can I control which EC2 instances get removed by an AutoScalingGroup using Amazon Web Services? 在这种情况下,我如何开始使用Amazon Web Services? - How do i get started with Amazon Web Services for this scenario? 如何将Couchbase服务器部署到Amazon Web Services? - How can I deploy Couchbase server to Amazon Web Services? 如何在没有付费的情况下尝试亚马逊网络服务 - How can I try Amazon Web Services without pay something 如何在我的Web应用程序中公开Amazon Lambda函数的状态? - How can I expose the status of an Amazon Lambda function in my web app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM