Node Script

Seconds to DHMS

Downloads: 2
Download
Ads Support FTD

Description

Converts Seconds into D:H:M:S time

//
// SecondsToDHMS.ts
// Converts Seconds into time format
// Written by @Fusedthreed – https://FusedThreeD.com
//

@customNode()
export class SecondsToDHMS extends BasicScriptNode {

  @input()
  seconds: number = 0;

  @input()
  showDays: boolean = true;

  @input()
  showHours: boolean = true;

  @input()
  showMinutes: boolean = true;

  // NEW TOGGLE
  @input()
  trimLeadingZeroIfFollowed: boolean = true;

  @output()
  result: string = "";

  execute() {

    let total = Math.max(0, Math.floor(this.seconds));

    const days = Math.floor(total / 86400);
    total %= 86400;

    const hours = Math.floor(total / 3600);
    total %= 3600;

    const minutes = Math.floor(total / 60);
    const secs = total % 60;

    const rawParts: number[] = [];

    if (this.showDays) rawParts.push(days);
    if (this.showHours) rawParts.push(hours);
    if (this.showMinutes) rawParts.push(minutes);
    rawParts.push(secs); // seconds always last

    const parts: string[] = [];

    for (let i = 0; i < rawParts.length; i++) {
      const value = rawParts[i];
      const isFirst = i === 0;
      const hasNext = i < rawParts.length - 1;

      if (
        this.trimLeadingZeroIfFollowed &&
        isFirst &&
        value === 0 &&
        hasNext
      ) {
        // Trim ONLY this case → skip entirely
        continue;
      }

      // Padding rules:
      // - First visible unit: no padding
      // - Following units: pad to 2 digits
      const isFirstVisible = parts.length === 0;
      parts.push(isFirstVisible ? value.toString() : this.pad(value));
    }

    // Safety fallback (should never be empty)
    this.result = parts.length > 0 ? parts.join(":") : "0";
  }

  private pad(v: number): string {
    return v < 10 ? "0" + v : v.toString();
  }
}
Ads Support FTD