javascript - 我需要从“1609891200000”开始的正确时间而不使用 Moment
问题描述
var now = moment(1609891200000, "x").format('MMM DD h:mm A');
var x = new Date(1609891200000);
console.log(now); // Prints Jan 06 12:00 AM
console.log(x.toLocaleTimeString()); // Prints 05:30:00
我不知道为什么我的时间一直在 5:30。我需要一种方法来获得正确的时间,即12:00 AM
不使用 Moment。
我怎样才能做到这一点?
解决方案
您应该提供时区代码,如果没有代码,则默认提供
var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
// toLocaleTimeString() without arguments depends on the implementation,
// the default locale, and the default time zone
console.log(date.toLocaleTimeString());
// → "7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles
相反,您可以使用
var x = new Date(1609891200000);
x.toGMTString() // "Wed, 06 Jan 2021 00:00:00 GMT"
x.toUTCString() // "Wed, 06 Jan 2021 00:00:00 GMT"
x.toISOString() // "2021-01-06T00:00:00.000Z"
推荐阅读
- javascript - 避免多次调用函数 ReactJs
- rust - 给定文件的“Path”或“PathBuf”,我如何以编程方式知道文件的属性
- google-cloud-build - 在 GCP 生产项目中使用 Cloud 源代码库
- html - 带有粘性顶部导航栏的溢出父容器中的粘性表标题
- c# - C#:根据设定的周期增加发票编号
- python - 是否可以在 Python 1 或 Python 2 中创建新类型(但不是新类)?
- r - 在 R 中创建多个双变量图
- python - Tornado websocket_connect() 内存分配
- javascript - 我如何在反应中处理来自 api 调用的错误
- docker - Docker 卷挂载到 Kubernetes 卷