我尝试了在这里找到的所有解决方案,但没有任何效果。 我只是在尝试:
var a = new Date();
console.log(a);
//2020-01-12T05:05:17.320Z
//Time in my timezone: 2020-01-12T02:05:17.320Z
我来自巴西,时区是 -3:00
我已经安装了 moment.js 并尝试过:
var moment = require('moment-timezone');
moment().tz("America/Sao_Paulo").format();
var a = new Date();
console.log(a);
但我一直在没有我的时区的情况下。我还尝试在没有 moment.js 的情况下设置 TZ,但没有成功。
我无法使用某些解决方案来更改调用“new Date()”的方式,因为我必须将字符串解析为包含日期的对象,并且我使用提示模块,该模块从控制台获取已经返回的日期一个约会。我不知道还能做什么。
感谢您的帮助。
*我在 Windows 上运行,时间正确,配置指向正确的时区
编辑1:更多信息
最佳答案
我发现最好的方法是使用 moment.js 添加“getTimezoneOffset”(分钟)
//Will returns the current "wrong" time in formart '2020-09-29T19:47:46.411Z'
//Expect: 2020-09-29T16:47:46.411Z
console.log(new Date())
因此,如果您想查看任何对象的完整日期('DD-MM-YYYY HH:mm')格式
var moment = require('moment')
// specificDay is a Date (type: Object) like '2020-09-22T00:00:00.000Z'
// previously retrieved from the database without information about the time('HH:mm')
// Only with 'YYYY-MM-DD'
moment(specificDay, "YYYY-MM-DD").add(new Date().getTimezoneOffset(),'minute').format('DD-MM-YYYY HH:mm')
关于javascript - 在 Node.js 中获取错误的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59701260/