在我的nodejs应用中,我需要使用Y-m-d H:i:s
格式的日期,我使用此简单代码
console.log(new Date().toLocaleString());
我在本地计算机上
2019-1-8 04:14:28
是正确的格式,但实时服务器中的相同代码却给了我1/8/2019, 4:14:00 AM
这不是我想要的....为什么这样做以及如何解决?
答案 0 :(得分:3)
来自MDN web docs:
toLocaleString()方法返回对语言敏感的字符串 此日期的表示形式。新的语言环境和选项参数使 应用程序指定应使用其格式约定的语言 用于定制功能的行为。在老 忽略语言环境和选项参数的实现, 使用的语言环境和返回的字符串形式完全是 取决于实现。
示例:
var event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// British English uses day-month-year order and 24-hour time without AM/PM
console.log(event.toLocaleString('en-GB', { timeZone: 'UTC' }));
// expected output: 20/12/2012, 03:00:00
// Korean uses year-month-day order and 12-hour time with AM/PM
console.log(event.toLocaleString('ko-KR', { timeZone: 'UTC' }));
// expected output: 2012. 12. 20. 오전 3:00:00
您没有将位置参数传递给toLocaleString
,因此将使用当前位置。您会在计算机和远程服务器上看到不同的输出,因为它们实际上位于不同的国家/地区。
答案 1 :(得分:0)
您可以使用moment.js进行日期和时间操作。
moment("2010-10-20 4:30:12", "YYYY-MM-DD HH:mm:ss"); // parsed as 4:30:12 local time
moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC
更多信息,请访问https://github.com/Natalia504/react-native-weatherApp。