Sha256: c8e6a15acbc6333b7637dbff3efa9ce4aeeaeb03a6a45d332d5820f6001e2483

Contents?: true

Size: 1.98 KB

Versions: 33

Compression:

Stored size: 1.98 KB

Contents

import { daysToMonths, monthsToDays } from './bubble';
import { normalizeUnits } from '../units/aliases';
import toInt from '../utils/to-int';

export function as (units) {
    if (!this.isValid()) {
        return NaN;
    }
    var days;
    var months;
    var milliseconds = this._milliseconds;

    units = normalizeUnits(units);

    if (units === 'month' || units === 'year') {
        days   = this._days   + milliseconds / 864e5;
        months = this._months + daysToMonths(days);
        return units === 'month' ? months : months / 12;
    } else {
        // handle milliseconds separately because of floating point math errors (issue #1867)
        days = this._days + Math.round(monthsToDays(this._months));
        switch (units) {
            case 'week'   : return days / 7     + milliseconds / 6048e5;
            case 'day'    : return days         + milliseconds / 864e5;
            case 'hour'   : return days * 24    + milliseconds / 36e5;
            case 'minute' : return days * 1440  + milliseconds / 6e4;
            case 'second' : return days * 86400 + milliseconds / 1000;
            // Math.floor prevents floating point math errors here
            case 'millisecond': return Math.floor(days * 864e5) + milliseconds;
            default: throw new Error('Unknown unit ' + units);
        }
    }
}

// TODO: Use this.as('ms')?
export function valueOf () {
    if (!this.isValid()) {
        return NaN;
    }
    return (
        this._milliseconds +
        this._days * 864e5 +
        (this._months % 12) * 2592e6 +
        toInt(this._months / 12) * 31536e6
    );
}

function makeAs (alias) {
    return function () {
        return this.as(alias);
    };
}

export var asMilliseconds = makeAs('ms');
export var asSeconds      = makeAs('s');
export var asMinutes      = makeAs('m');
export var asHours        = makeAs('h');
export var asDays         = makeAs('d');
export var asWeeks        = makeAs('w');
export var asMonths       = makeAs('M');
export var asYears        = makeAs('y');

Version data entries

33 entries across 32 versions & 4 rubygems

Version Path
card-1.100.0 mod/date/vendor/moment/src/lib/duration/as.js
card-1.99.6 mod/date/vendor/moment/src/lib/duration/as.js
card-1.99.5 mod/date/vendor/moment/src/lib/duration/as.js
card-1.99.4 mod/date/vendor/moment/src/lib/duration/as.js
card-1.99.3 mod/date/vendor/moment/src/lib/duration/as.js
card-1.99.2 mod/date/vendor/moment/src/lib/duration/as.js
card-1.99.1 mod/date/vendor/moment/src/lib/duration/as.js
card-1.99.0 mod/date/vendor/moment/src/lib/duration/as.js
card-1.98.3 mod/date/vendor/moment/src/lib/duration/as.js
card-1.98.2 mod/date/vendor/moment/src/lib/duration/as.js
card-1.98.1 mod/date/vendor/moment/src/lib/duration/as.js
card-1.98.0 mod/date/vendor/moment/src/lib/duration/as.js
card-1.97.0.1 mod/date/vendor/moment/src/lib/duration/as.js
card-1.97.0 mod/date/vendor/moment/src/lib/duration/as.js
card-1.96.8 mod/date/vendor/moment/src/lib/duration/as.js
card-1.96.7 mod/date/vendor/moment/src/lib/duration/as.js
card-1.96.6 mod/date/vendor/moment/src/lib/duration/as.js
card-1.96.5 mod/date/vendor/moment/src/lib/duration/as.js
card-1.96.4 mod/date/vendor/moment/src/lib/duration/as.js
card-1.96.3 mod/date/vendor/moment/src/lib/duration/as.js