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.