What are the best ways to convert UTC timezone to DD MMM,YYYY 15:30:31 format in JavaScript?

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

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(&#39;2023-05-25T12:00:00Z&#39;);

// Convert the date to the desired format
let formattedDate = date.getUTCDate() + &#39; &#39; + getMonthAbbreviation(date.getUTCMonth()) + &#39;, &#39; + date.getUTCFullYear() + &#39; &#39; + padZero(date.getUTCHours()) + &#39;:&#39; + padZero(date.getUTCMinutes()) + &#39;:&#39; + padZero(date.getUTCSeconds());

// Helper function to get the abbreviation of the month
function getMonthAbbreviation(month) {
  const months = [&#39;Jan&#39;, &#39;Feb&#39;, &#39;Mar&#39;, &#39;Apr&#39;, &#39;May&#39;, &#39;Jun&#39;, &#39;Jul&#39;, &#39;Aug&#39;, &#39;Sep&#39;, &#39;Oct&#39;, &#39;Nov&#39;, &#39;Dec&#39;];
  return months[month];
}

// Helper function to pad single digit numbers with a leading zero
function padZero(number) {
  return number &lt; 10 ? &#39;0&#39; + 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.

huangapple
  • 本文由 发表于 2023年5月25日 21:33:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332894.html
匿名

发表评论

匿名网友

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

确定