util.js 1005 Bytes
/**
 * Created by yoho on 2016/6/7.
 */
var util = {
    dateFormat: function(data, format) {
        if (!isNaN(data)) {
            var date = {
                "M+": data.getMonth() + 1,
                "d+": data.getDate(),
                "h+": data.getHours(),
                "m+": data.getMinutes(),
                "s+": data.getSeconds(),
                "q+": Math.floor((data.getMonth() + 3) / 3),
                "S+": data.getMilliseconds()
            };
            if (/(y+)/i.test(format)) {
                format = format.replace(RegExp.$1, (data.getFullYear() + '').substr(4 - RegExp.$1.length));
            }
            for (var k in date) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
                }
            }
            return format;
        } else {
            return "";
        }
    }
}

module.exports= util;