Skip to content

better way to format seconds #34

@pietrop

Description

@pietrop

As noted in this fork clowdr-app/slate-transcript-editor - src/util/export-adapters/subtitles-generator/compose-subtitles/util/format-seconds.js there might be a better way to format seconds using
d3-format

- const formatSeconds = seconds => new Date(seconds.toFixed(3) * 1000).toISOString().substr(11, 12);
+
+ import * as d3 from "d3-format"
+
+ function formatSeconds(totalSeconds) {
+     const hours = Math.floor(totalSeconds / 3600);
+     const minuteSeconds = totalSeconds - hours * 3600;
+     const minutes = Math.floor(minuteSeconds / 60);
+    const secondSeconds = minuteSeconds - minutes * 60;
+    const seconds = Math.floor(secondSeconds);
+    const millis = (secondSeconds - seconds) * 1000;
+
+    const hoursPart = d3.format("02d")(hours);
+    const minutesPart = d3.format("02d")(minutes);
+    const secondsPart = d3.format("02d")(seconds);
+    const millisPart = d3.format("03d")(millis);
+
+    return `${hoursPart}:${minutesPart}:${secondsPart},${millisPart}`;
+}

or could use this function pietrop/slate-transcript-editor/src/util/timecode-converter/src/secondsToTimecode.js#L25 ?

However keep an eye out for the purpose of the timecode, eg if it's for srt captions files, milliseconds might need to be 3 digits separated by a comma(?)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions