如何解析YouTube的持续时间?

时间:2019-04-30 23:18:21

标签: dart youtube-data-api

我无法解析YouTube持续时间到日期或日期时间,我搜索并访问了许多网站,但是无法解析youtube字符串持续时间

Youtube持续时间示例为:PT15M33S

Please click for youtube api documentation

1 个答案:

答案 0 :(得分:1)

它的ISO 8601 durations的格式为 P [n] Y [n] M [n] DT [n] H [n] M [n] S

示例:

20秒:

PT20.0S

一年,3个月,3天,4小时,5分钟,10秒:

P1Y3M3DT4H5M10S

很遗憾,dart没有提供解析器api Duration class 因此,您必须自己分析并输入Duration并转换为Datatime

您可以尝试Match-class

String durationString = "P2Y5M3W4DT5H4M2S";
String regxPattern= "^P(\\d+Y)?(\\d+M)?(\\d+W)?(\\d+D)?(T(\\d+H)?(\\d+M)?(\\d+S)?)?\$";

  print(regxPattern);

RegExp re = new RegExp(regxPattern);

if (re.hasMatch(durationString)) {

   for (var m in re.allMatches(durationString)) {


     print("total:"+m.group(0));
     print("Y:"+m.group(1));
     print("M:"+m.group(2));
     print("W:"+m.group(3));
     print("D:"+m.group(4));
     print("Time:"+m.group(5));
     print("H:"+m.group(6));
     print("M:"+m.group(7));
     print("S:"+m.group(8));
     Duration fastestMarathon = new Duration(hours:m.group(6), 
                                             minutes:m.group(7), 
                                             second,m.group(8));

   }

}