2 * time-stamp <https://github.com/jonschlinkert/time-stamp>
4 * Copyright (c) 2015, Jon Schlinkert.
5 * Licensed under the MIT License.
11 * Parse the given pattern and return a formatted
14 * @param {String} `pattern` Date pattern.
15 * @param {Date} `date` Date object.
19 module.exports = function timestamp(pattern, date) {
20 if (typeof pattern !== 'string') {
22 pattern = 'YYYY:MM:DD';
24 date = date || new Date();
25 return pattern.replace(/([YMDHms]{2,4})(:\/)?/g, function(_, key, sep) {
26 var increment = method(key);
27 if (!increment) return _;
30 var res = '00' + String(date[increment[0]]() + (increment[2] || 0));
31 return res.slice(-increment[1]) + sep;
35 function method(key) {
37 YYYY: ['getFullYear', 4],
38 YY: ['getFullYear', 2],
39 // getMonth is zero-based, thus the extra increment field
40 MM: ['getMonth', 2, 1],
43 mm: ['getMinutes', 2],
44 ss: ['getSeconds', 2],
45 ms: ['getMilliseconds', 3]