简体   繁体   English

使用moment js将日期时间从AWS ec2实例发送到Gmail

[英]Sending datetime from AWS ec2 instance to Gmail with moment js

I supposed to get time from current datetime of new Date() in javascript using moment js and convert it to local time . 我应该使用moment jsjavascriptnew Date()当前datetime获取time ,并将其转换为local time

var time = moment.utc(new Date()).local().format('HH:mm');
console.log(time);

And send the time to gmail . 并将time发送给gmail

If I send it from my local server, the time display in the email as what i want. 如果我是从本地服务器发送的,则时间会根据需要显示在电子邮件中。 But if i send the time from remote server(aws instance), it display the utc time in gmail. 但是,如果我从远程服务器(aws实例)发送时间,它将以gmail显示utc时间。

Sample: 样品:

new Date() = 2018-04-24T08:20:54.622Z

time in email from localserver = 16:20 (+8 in my country), correct for me.

time in email from aws ec2 instance = 08:20, I don't want it.

Anyone have an idea about this? 有人对此有想法吗? Thank you. 谢谢。

Is this Node? 这是节点吗? If so AWS servers are set so their local time is UTC. 如果是这样,则将AWS服务器设置为本地时间为UTC。 These should be the same on AWS and different on your local: 这些在AWS上应该是相同的,而在本地则应该不同:

var utctime = moment.utc().format('HH:mm');
console.log(utctime);
var localtime = moment.utc().local().format('HH:mm');
console.log(localtime);

If this is running in your browser and you are getting different behavior pointed at your local server and the AWS server, then something very weird is going on because your machine and the browser should be used for the local time. 如果此操作在您的浏览器中运行,并且您针对本地服务器和AWS服务器获得不同的行为,则说明发生了非常奇怪的事情,因为您的计算机和浏览器应用于本地时间。

If I understand your problem correctly, you can solve this issue using moment-timezone . 如果我正确理解了您的问题,则可以使用moment-timezone解决此问题。 You can hard code your local time-zone so that it will always convert date into that specific timezone. 您可以对本地时区进行硬编码,以便它将始终将日期转换为该特定时区。

Example: 例:

moment(date).tz('Asia/Tokyo').format(format)

Instead of Asia/Tokyo put your local time zone. 而不是Asia/Tokyo放您当地的时区。

If you don't know your local time zone, use guess() API of moment-timezone library: 如果您不知道本地时区,请使用moment-timezone库的guess() API:

var zoneName = moment.tz.guess(); // just to get your local time zone name. Don't keep it in code otherwise it will pick local time zone of AWS instance

Below is working code snippet: 以下是工作代码段:

 var zoneName = moment.tz.guess(); console.log(zoneName); var time = moment('2018-04-24T08:20:54.622Z').tz(zoneName).format('HH:mm'); console.log(time); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.11/moment-timezone-with-data.js"></script> 

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

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