英文:
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 -->
答案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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论