如何以整数形式获取带有时区的日期?

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

How to get Date with timezone as int?

问题

如何在JS中以这种格式获取日期?
我尝试使用.toISOString(),但我没有完全得到我需要的结果。

2022-12-22 11:35:23 -0400

谢谢

英文:

how can I get date in this format in JS?
I tried using .toISOString() but I don't quite get the result that I need.

2022-12-22 11:35:23 -0400

Thanks

答案1

得分: 0

我不知道任何快速的方法,但你可以使用类似以下的代码来获取JavaScript中的日期格式 "YYYY-MM-DD HH:mm:ss ZZ":

const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1; // 月份是从零开始计数的,所以我们需要加1
const day = date.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const timeZoneOffset = date.getTimezoneOffset();
const timeZoneOffsetHours = Math.floor(Math.abs(timeZoneOffset) / 60);
const timeZoneOffsetMinutes = Math.abs(timeZoneOffset) % 60;
const timeZoneOffsetString = (timeZoneOffset < 0 ? '+' : '-') +
    (timeZoneOffsetHours < 10 ? '0' : '') + timeZoneOffsetHours +
    ':' + (timeZoneOffsetMinutes < 10 ? '0' : '') + timeZoneOffsetMinutes;

const formattedDate = `${year}-${month < 10 ? '0' : ''}${month}-${day < 10 ? '0' : ''}${day} ${hours}:${minutes}:${seconds} ${timeZoneOffsetString}`;

这将为你提供一个类似 "2022-12-22 11:35:23 -0400" 的字符串。

英文:

I don't know any fast way, but you can use something like this:
To get a date in the format "YYYY-MM-DD HH:mm:ss ZZ" in JavaScript, you can use the following code:

const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1; // months are zero-indexed, so we need to add 1
const day = date.getDate();
const hours = date.getHours();
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const timeZoneOffset = date.getTimezoneOffset();
const timeZoneOffsetHours = Math.floor(Math.abs(timeZoneOffset) / 60);
const timeZoneOffsetMinutes = Math.abs(timeZoneOffset) % 60;
const timeZoneOffsetString = (timeZoneOffset &lt; 0 ? &#39;+&#39; : &#39;-&#39;) +
    (timeZoneOffsetHours &lt; 10 ? &#39;0&#39; : &#39;&#39;) + timeZoneOffsetHours +
    &#39;:&#39; + (timeZoneOffsetMinutes &lt; 10 ? &#39;0&#39; : &#39;&#39;) + timeZoneOffsetMinutes;

const formattedDate = `${year}-${month &lt; 10 ? &#39;0&#39; : &#39;&#39;}${month}-${day &lt; 10 ? &#39;0&#39; : &#39;&#39;}${day} ${hours}:${minutes}:${seconds} ${timeZoneOffsetString}`;

This will give you a string like "2022-12-22 11:35:23 -0400".

答案2

得分: 0

这在纯JavaScript中做起来有些复杂,但使用像luxon这样的库很简单。

我们创建一个新的DateTime对象,date,然后调用DateTime.toFormat()来以所需的格式呈现日期。

const { DateTime } = luxon;

const date = DateTime.now();
console.log('Formatted date:', date.toFormat('yyyy-MM-dd HH:mm:ss ZZZ'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/3.0.1/luxon.min.js" integrity="sha512-6ZJuab/UnRq1muTChgrVxJhSgygmL2GMLVmSJN7pcBEqJ1dWPbqN9CiZ6U3HrcApTIJsLnMgXYBYgtVkJ8fWiw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
英文:

This is tricky to do with vanilla JavaScript, however it's simple to do using a library such as luxon.

We create a new DateTime object, date, then call DateTime.toFormat() to present the date in the desired format.

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

const { DateTime } = luxon;

const date = DateTime.now();
console.log(&#39;Formatted date:&#39;, date.toFormat(&#39;yyyy-MM-dd HH:mm:ss ZZZ&#39;))

<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/3.0.1/luxon.min.js" integrity="sha512-6ZJuab/UnRq1muTChgrVxJhSgygmL2GMLVmSJN7pcBEqJ1dWPbqN9CiZ6U3HrcApTIJsLnMgXYBYgtVkJ8fWiw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script><!-- end snippet -->

<!-- end snippet -->

答案3

得分: -1

你想要什么格式?你是否使用了Date?这里有一个我经常使用的非常有用的库,Date FNS,它有一个格式化函数,你可以设置你想要的格式。该函数返回一个字符串。

英文:

What format do you want? Did you use Date? Here is very useful library I use a lot. Date FNS there is format function and you can set format you want. Function returns string

huangapple
  • 本文由 发表于 2023年1月9日 06:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051637.html
匿名

发表评论

匿名网友

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

确定