英文:
What are the best ways to convert UTC timezone to DD MMM,YYYY 15:30:31 format in JavaScript?
问题
Tried toLocaleTimeString('en-US')
and other available options
new Date("2023-05-25T12:55:41.8382011Z");
// Thu May 25 2023 18:25:41 GMT+0530 (India Standard Time)
new Date("2023-05-25T12:55:41.8382011Z").toLocaleTimeString('en-US');
// '6:25:41 PM'
Expecting to convert UTC timezone to 'DD MMM,YYYY 15:30:31' format
英文:
Tried toLocaleTimeString('en-US')
and other available options
new Date("2023-05-25T12:55:41.8382011Z");
// Thu May 25 2023 18:25:41 GMT+0530 (India Standard Time)
new Date("2023-05-25T12:55:41.8382011Z").toLocaleTimeString('en-US');
// '6:25:41 PM'
Expecting to convert UTC timezone to DD MMM,YYYY 15:30:31
format
答案1
得分: 1
You can use the built-in Date object in JavaScript to convert UTC time to any desired format.
// 创建一个新的 Date 对象,使用 UTC 时间
let date = new Date('2023-05-25T12:00:00Z');
// 将日期转换为所需的格式
let formattedDate = date.getUTCDate() + ' ' + getMonthAbbreviation(date.getUTCMonth()) + ', ' + date.getUTCFullYear() + ' ' + padZero(date.getUTCHours()) + ':' + padZero(date.getUTCMinutes()) + ':' + padZero(date.getUTCSeconds());
// 获取月份缩写的辅助函数
function getMonthAbbreviation(month) {
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[month];
}
// 在单个数字前添加前导零的辅助函数
function padZero(number) {
return number < 10 ? '0' + number : number;
}
formattedDate
然后将包含您需要的值。
更多关于这段代码的解释:
首先,我们使用字符串 '2023-05-25T12:00:00Z' 创建一个新的 Date 对象,其中 Z 表示时间在 UTC 中。
然后,我们使用 getUTCDate()
、getUTCMonth()
和 getUTCFullYear()
方法来获取日期的 UTC 中的日、月和年组成部分。
我们还使用了两个辅助函数,一个用于获取月份的缩写,另一个用于在单个数字前添加前导零。
最后,我们连接所有日期组件并返回格式化的日期字符串。
您可以将 UTC 时间字符串替换为要转换为所需格式的任何 UTC 时间字符串。
英文:
You can use the built-in Date object in JavaScript to convert UTC time to any desired format.
// Create a new Date object with the UTC time
let date = new Date('2023-05-25T12:00:00Z');
// Convert the date to the desired format
let formattedDate = date.getUTCDate() + ' ' + getMonthAbbreviation(date.getUTCMonth()) + ', ' + date.getUTCFullYear() + ' ' + padZero(date.getUTCHours()) + ':' + padZero(date.getUTCMinutes()) + ':' + padZero(date.getUTCSeconds());
// Helper function to get the abbreviation of the month
function getMonthAbbreviation(month) {
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[month];
}
// Helper function to pad single digit numbers with a leading zero
function padZero(number) {
return number < 10 ? '0' + number : number;
}
the formattedDate
will then have the value you need.
more explaination for the code:
we first create a new Date object with the UTCtime using the string '2023-05-25T12:00:00Z'. The Z at the end indicates that the time is in UTC.
We then use the getUTCDate()
, getUTCMonth()
, and getUTCFullYear()
methods to get the day, month, and year components of the date in UTC.
We also use two helper functions to get the abbreviation of the month and to pad single digit numbers with a leading zero.
Finally, we concatenate all the date components and return the formatted date string.
You can replace the UTC time string with any UTC time string you want to convert to the desired format.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论