How can I manipulate a date time in the format PT#M#S with JavaScript?
for example: PT5M33S
I'd like to output as hh:mm:ss
.
How can I manipulate a date time in the format PT#M#S with JavaScript?
for example: PT5M33S
I'd like to output as hh:mm:ss
.
Here's the basic code to get total number of seconds and other parts. I feel uneasy doing this, as the rule says that any time you want to date logic you shouldn't :) But anyways, here it goes - Google made it not easy, providing total seconds in the getduration player API, and providing totally different format in the gdata api.
var reptms = /^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/;
var hours = 0, minutes = 0, seconds = 0, totalseconds;
if (reptms.test(input)) {
var matches = reptms.exec(input);
if (matches[1]) hours = Number(matches[1]);
if (matches[2]) minutes = Number(matches[2]);
if (matches[3]) seconds = Number(matches[3]);
totalseconds = hours * 3600 + minutes * 60 + seconds;
}
duration: "PT26M"
–
Beverlybevers PT36.53S
. Here's and updated regex : ^PT(?:(\d+\.*\d*)H)?(?:(\d+\.*\d*)M)?(?:(\d+\.*\d*)S)?$
–
Librate Here is how you can get a youtube video data via Youtube API (v3) in a simple way and convert the video duration (ISO 8601) to seconds. Don't forget to change the { YOUR VIDEO ID } and { YOUR KEY } attribute in URL to your video id and your public google key. You can create a public key accessing the google developer console.
$.ajax({
url: "https://www.googleapis.com/youtube/v3/videos?id={ YOUR VIDEO ID }&part=contentDetails&key={ YOUR KEY }",
dataType: "jsonp",
success: function (data) { youtubeCallback (data); }
});
function youtubeCallback(data) {
var duration = data.items[0].contentDetails.duration;
alert ( convertISO8601ToSeconds (duration) );
}
function convertISO8601ToSeconds(input) {
var reptms = /^PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/;
var hours = 0, minutes = 0, seconds = 0, totalseconds;
if (reptms.test(input)) {
var matches = reptms.exec(input);
if (matches[1]) hours = Number(matches[1]);
if (matches[2]) minutes = Number(matches[2]);
if (matches[3]) seconds = Number(matches[3]);
totalseconds = hours * 3600 + minutes * 60 + seconds;
}
return (totalseconds);
}
duration: "PT26M"
–
Beverlybevers While these answers are technically correct; you should check out momentjs if you plan to do a lot of with time and durations. Also check out moment-duration-formats which makes formatting durations just as simple as regular momentjs time
An example of how easy these 2 modules make this
moment.duration('PT5M33S').format('hh:mm:ss')
that will output 05:33. And there's plenty of other uses.
Though there's a reason youtube uses ISO8601 format since it's a standard, so keep that in mind.
TypeError: moment.duration(...).format is not a function
I am getting this with Node 8.10 , moment 2.22.2 –
Butterfly moment().day("Monday").year(year_val).week(week_val).format('YYYY-MM-DD')
and it is working. –
Butterfly At the 'moment' in 2023 Moment.js is in legacy mode.
You can use the Duration class of Luxon.js (an offspring of Moment.js) to parse ISO8601 durations.
import { Duration } from 'luxon'
// OP's desired hh:mm:ss format
> Duration.fromISO('PT20S').toISOTime({suppressMilliseconds: true})
'00:00:20'
// other useful outputs
> Duration.fromISO('PT2M42S').toHuman()
'2 minutes, 42 seconds'
> Duration.fromISO('PT1H16M5S').toMillis()
4565000
> Duration.fromISO('P1DT2H1S').toObject()
{ days: 1, hours: 2, seconds: 1 }
I approuched this by checking the character two indices to the left of the unit (H,M,S) and checking if it is a number, if not the unit is single digit and I prepend an extra '0'. Otherwise I return the two digit number.
function formatTimeUnit(input, unit){
var index = input.indexOf(unit);
var output = "00"
if(index < 0){
return output; // unit isn't in the input
}
if(isNaN(input.charAt(index-2))){
return '0' + input.charAt(index-1);
}else{
return input.charAt(index-2) + input.charAt(index-1);
}
}
I do this for hours, minutes and seconds. I also drop hours if there aren't any in the input, this is ofcourse optional.
function ISO8601toDuration(input){
var H = formatTimeUnit(input, 'H');
var M = formatTimeUnit(input, 'M');
var S = formatTimeUnit(input, 'S');
if(H == "00"){
H = "";
}else{
H += ":"
}
return H + M + ':' + S ;
}
then just call it like this
duration = ISO8601toDuration(item.duration);
I use this to format youtube data API video durations. Hope this helps somebody
The simplest way is to use moment-duration-formats. Still, below is a custom function to convert the YouTube API duration response to hh:mm:ss format.
const convertISO8601ToStringWithColons = string => {
let hours, minutes, seconds;
if (string.includes("H")) {
hours = string.slice(2, string.indexOf("H"));
} else {
hours = false;
}
if (string.includes("S")) {
// checks if number is one-digit and inserts 0 in front of it
if (isNaN(parseInt(string.charAt(string.indexOf("S") - 2)))) {
seconds = "0" + string.charAt(string.indexOf("S") - 1)
} else {
seconds = string.slice(-3, -1)
}
} else {
seconds = "00"
}
// determines how minutes are displayed, based on existence of hours and minutes
if (hours) {
if (string.includes("M")) {
if (string.indexOf("M") - string.indexOf("H") === 3) {
minutes = string.slice(string.indexOf("H") + 1, string.indexOf("M"))
} else {
minutes = "0" + string.charAt(string.indexOf("M") - 1)
}
} else {
minutes = "00"
}
} else {
if (string.includes("M")) {
minutes = string.slice(2, (string.indexOf("M")))
} else {
minutes = "0"
}
}
// distinction because livestreams (P0D) are not considered
return string === "P0D" ? "Live" : `${hours ? hours + ":" : ""}${minutes}:${seconds}`
}
const textExamples = ["PT11H57M30S", "PT7H2M", "PT10H37S", "PT8H", "PT4M58S", "PT39M", "PT7S", "P0D"]
textExamples.forEach(element => {
console.log(convertISO8601ToStringWithColons(element))
});
© 2022 - 2024 — McMap. All rights reserved.