Skip to content

Instantly share code, notes, and snippets.

@Pustur
Last active August 26, 2019 18:38
Show Gist options
  • Select an option

  • Save Pustur/9b867bb2934803b4ff2314c0d2e21c79 to your computer and use it in GitHub Desktop.

Select an option

Save Pustur/9b867bb2934803b4ff2314c0d2e21c79 to your computer and use it in GitHub Desktop.

Revisions

  1. Pustur revised this gist Aug 26, 2019. 1 changed file with 22 additions and 19 deletions.
    41 changes: 22 additions & 19 deletions timestamp-to-string.js
    Original file line number Diff line number Diff line change
    @@ -1,23 +1,26 @@
    function timestamp2string(timestamp, useMilliseconds) {
    var string = '',
    periods = {
    day: 86400000,
    hour: 3600000,
    minute: 60000,
    second: 1000,
    millisecond: 1
    };
    function timestampToString(timestamp, useMilliseconds) {
    const periods = {
    day: 86400000,
    hour: 3600000,
    minute: 60000,
    second: 1000,
    millisecond: 1,
    };

    if (!useMilliseconds) {
    timestamp *= 1000;
    delete periods.millisecond;
    }
    if (!useMilliseconds) {
    timestamp *= 1000;
    delete periods.millisecond;
    }

    for (var period in periods) {
    var number = Math.floor(timestamp / periods[period]);
    timestamp -= (number * periods[period]);
    string += (number !== 0 ? number + ' ' + period + ((number !== 1) ? 's' : '') + ' ' : '');
    }
    return Object.entries(periods)
    .reduce((result, [period, value]) => {
    const num = Math.floor(timestamp / value);
    const plural = num === 1 ? '' : 's';
    const str = `${num} ${period}${plural} `;

    return string.trim();
    timestamp -= num * value;

    return `${result}${num === 0 ? '' : str}`;
    }, '')
    .trim();
    }
  2. Loris Bettazza renamed this gist Jun 19, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Loris Bettazza revised this gist Apr 26, 2016. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions timestamp2string.js
    Original file line number Diff line number Diff line change
    @@ -6,15 +6,14 @@ function timestamp2string(timestamp, useMilliseconds) {
    minute: 60000,
    second: 1000,
    millisecond: 1
    },
    period;
    };

    if (!useMilliseconds) {
    timestamp *= 1000;
    delete periods.millisecond;
    }

    for (period in periods) {
    for (var period in periods) {
    var number = Math.floor(timestamp / periods[period]);
    timestamp -= (number * periods[period]);
    string += (number !== 0 ? number + ' ' + period + ((number !== 1) ? 's' : '') + ' ' : '');
  4. Loris Bettazza created this gist Apr 26, 2016.
    24 changes: 24 additions & 0 deletions timestamp2string.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    function timestamp2string(timestamp, useMilliseconds) {
    var string = '',
    periods = {
    day: 86400000,
    hour: 3600000,
    minute: 60000,
    second: 1000,
    millisecond: 1
    },
    period;

    if (!useMilliseconds) {
    timestamp *= 1000;
    delete periods.millisecond;
    }

    for (period in periods) {
    var number = Math.floor(timestamp / periods[period]);
    timestamp -= (number * periods[period]);
    string += (number !== 0 ? number + ' ' + period + ((number !== 1) ? 's' : '') + ' ' : '');
    }

    return string.trim();
    }