eliseumds
4/18/2015 - 11:17 PM

Date formatting

Date formatting

import DateFormatter from './DateFormatter';

export default class ZonedDateFormatter extends DateFormatter {
    /**
     * @param  {*} [date]
     * @return {moment}
     */
    _parseDate(date) {
        return moment.parseZone(date || new Date());
    }
}
import DateFormattingStrategy from './DateFormattingStrategy';

export default class WeekRangeFormattingStrategy extends DateFormattingStrategy {
    /**
     * @param  {moment} date
     * @return {string}
     */
    format(date) {
        const startDate = date.startOf('week');
        const endDate = startDate.clone().endOf('week');

        return startDate.format('DD.MM') + ' - ' + endDate.format('DD.MM');
    }
}
import DateFormattingStrategy from './DateFormattingStrategy';

export default class MomentFormatFormattingStrategy extends DateFormattingStrategy {
    /**
     * @constructor
     * @param {string} formatString - moment format string
     */
    construtor(formatString) {
        this.formatString = formatString;
    }

    /**
     * @param  {moment} date - moment instance
     * @return {string}
     */
    format(date) {
        return date.format(this.formatString);
    }
}
export default class DateFormattingStrategy {
    format() {
        throw 'MethodNotImplementedException';
    }
}
import DateFormattingStrategy from './strategies/DateFormattingStrategy';
import MomentFormatFormattingStrategy from './strategies/MomentFormatFormattingStrategy';
import WeekRangeFormattingStrategy from './strategies/WeekRangeFormattingStrategy';

export default class DateFormatter() {
    static FULL_HOUR = new MomentFormatFormattingStrategy('HH:00');
    static HOUR_MINUTE = new MomentFormatFormattingStrategy('HH:mm');
    
    static DAY_MONTH = new MomentFormatFormattingStrategy('DD.MM');
    static LONG_DAY_MONTH = new MomentFormatFormattingStrategy('dddd DD.MM');
    
    static WEEK_DAY = new MomentFormatFormattingStrategy('dddd');
    static LONG_WEEK_DAY = new MomentFormatFormattingStrategy('dddd');
    static SHORT_WEEK_DAY = new MomentFormatFormattingStrategy('ddd');
    
    static WEEK_NUMBER = new MomentFormatFormattingStrategy('[KW]W');
    static WEEK_RANGE = new WeekRangeFormattingStrategy();
    
    static MONTH_YEAR = new MomentFormatFormattingStrategy('MMM YY');

    /**
     * @param  {*} [date]
     * @return {moment}
     */
    _parseDate(date) {
        return moment(date || new Date());
    };

    /**
     * @param  {string} format
     * @return {function}
     */
    createCallback(format) {
        return this.format.bind(this, format);
    };

    /**
     * @param  {DateFormattingStrategy} strategy
     * @return {function}
     */
    createCallbackUsing(strategy) {
        return this.formatUsing.bind(this, strategy);
    };

    /**
     * @param  {string} formatString
     * @param  {Date|string|moment} date
     * @return {string}
     */
    format(formatString, date) {
        return this._parseDate(date).format(formatString);
    };

    /**
     * @param  {DateFormattingStrategy} strategy
     * @param  {moment}                 date
     * @return {string}
     * @throws {TypeError}
     */
    formatUsing(strategy, date) {
        if (strategy instanceof DateFormattingStrategy) {
            return strategy.format(this._parseDate(date));
        }

        throw new TypeError(
            '`strategy` must be an instance of DateFormattingStrategy'
        );
    };
}

Date Formatter: how to use

var formatter = new DateFormatter();

// get a formatting callback(date) -> string
var myChartSettings = {
    tickFormatter: formatter.createCallbackUsing(DateFormatter.FULL_HOUR)
};

// custom formatting? No problem
var myChartSettings = {
    tickFormatter: formatter.createCallback('DD/MM')
};

// get the formatted string
var myStr = formatter.format('MM/DD/YYYY', Date.now());

// with a custom strategy
var myStr = formatter.formatUsing(new CustomStrategy(), Date.now());

ZonedDateFormatter

An extension of DateFormatter that parses dates using moment.parseZone.