我始终以90%的纯字符串格式存储“ 90”作为分钟。我想将其转换为schema.org标准的ISO 8601持续时间格式。
例如“ 90”应转换为PT1H30M
答案 0 :(得分:2)
如果要读取的时间间隔对PT90M等值不满意,则可以执行以下操作:
function MinutesToDuration(s) {
var days = Math.floor(s / 1440);
s = s - days * 1440;
var hours = Math.floor(s / 60);
s = s - hours * 60;
var dur = "PT";
if (days > 0) {dur += days + "D"};
if (hours > 0) {dur += hours + "H"};
dur += s + "M"
return dur;
}
console.log(MinutesToDuration("0"));
console.log(MinutesToDuration("10"));
console.log(MinutesToDuration("90"));
console.log(MinutesToDuration(1000));
console.log(MinutesToDuration(10000));
输出:
PT0M
PT10M
PT1H30M
PT16H40M
PT6D22H40M