JavaScript/TypeScript如何构建类似Java中的OffsetDateTime?

huangapple go评论88阅读模式
英文:

JavaScript/TypeScript how to build OffsetDateTime like in Java?

问题

我对JavaScript非常不熟,如何在JavaScript中构建偏移日期时间,我要用它来调用一个以字符串形式获取日期时间的API。

2020-08-27T22:54:36.922Z -> 就像这样在JavaScript/TypeScript中

在Java中,我会这样做。

OffsetDateTime.now().toString()

请告诉我。前端使用的是JavaScript,我计划在API中获取偏移日期作为字符串,然后在后端的Java处理程序中处理它。

英文:

I am very new to java script, how to build offset date time in javascript, which I use to use to call and API which gets date time in string.

2020-08-27T22:54:36.922Z -> Like this in JavaScript/TypeScript

The samething in Java i will do something like this.

OffsetDateTime.now().toString()

Please let me know. The frontend is Javascript and I am planning to get offset date as string in the API and process that in the Java Handler in the backend.

答案1

得分: 2

好的,以下是您要翻译的内容:

似乎您想要使用 Date 上的 toISOString() 方法。您可以在新创建的 new Date() 实例上调用它,该实例会初始化为当前时间。

console.log(new Date().toISOString())

此处为文档链接

英文:

It sounds like you want the toISOString() method on Date. You would call that on a freshly created new Date() instance that initializes to the current time.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

console.log(new Date().toISOString())

<!-- end snippet -->

Documentation here

答案2

得分: 0

在 JavaScript 中获取偏移量:

var now = new Date();
var offset = now.getTimezoneOffset();

您可以根据您的特定实现进行数学计算例如 / 60)。
英文:

To get the offset in JavaScript:

var now = new Date();
var offset = now.getTimezoneOffset();

You can do math from there for your specific implementation (i.e./ 60).

huangapple
  • 本文由 发表于 2020年8月28日 07:00:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63625226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定