英文:
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 < 0 ? '+' : '-') +
(timeZoneOffsetHours < 10 ? '0' : '') + timeZoneOffsetHours +
':' + (timeZoneOffsetMinutes < 10 ? '0' : '') + timeZoneOffsetMinutes;
const formattedDate = `${year}-${month < 10 ? '0' : ''}${month}-${day < 10 ? '0' : ''}${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('Formatted date:', date.toFormat('yyyy-MM-dd HH:mm:ss ZZZ'))
<!-- 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,它有一个格式化函数,你可以设置你想要的格式。该函数返回一个字符串。
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论