From 90788a32b9f36928308be67cce2d81d236ca7813 Mon Sep 17 00:00:00 2001 From: Iskren Chernev Date: Sun, 8 Nov 2015 18:47:30 -0800 Subject: [PATCH] Add Hmm and Hmmss tokens --- src/lib/format/format.js | 2 +- src/lib/units/hour.js | 23 +++++++++++++++++++++++ src/test/moment/create.js | 8 ++++++++ src/test/moment/format.js | 11 +++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/lib/format/format.js b/src/lib/format/format.js index 6c1e46d58..773bbff46 100644 --- a/src/lib/format/format.js +++ b/src/lib/format/format.js @@ -1,6 +1,6 @@ import zeroFill from '../utils/zero-fill'; -export var formattingTokens = /(\[[^\[]*\])|(\\)?(hmmss|hmm|Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Q|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,9}|x|X|zz?|ZZ?|.)/g; +export var formattingTokens = /(\[[^\[]*\])|(\\)?([Hh]mm(ss)?|Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Q|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|mm?|ss?|S{1,9}|x|X|zz?|ZZ?|.)/g; var localFormattingTokens = /(\[[^\[]*\])|(\\)?(LTS|LT|LL?L?L?|l{1,4})/g; diff --git a/src/lib/units/hour.js b/src/lib/units/hour.js index a21757300..63919de88 100644 --- a/src/lib/units/hour.js +++ b/src/lib/units/hour.js @@ -26,6 +26,15 @@ addFormatToken('hmmss', 0, 0, function () { zeroFill(this.seconds(), 2); }); +addFormatToken('Hmm', 0, 0, function () { + return '' + this.hours() + zeroFill(this.minutes(), 2); +}); + +addFormatToken('Hmmss', 0, 0, function () { + return '' + this.hours() + zeroFill(this.minutes(), 2) + + zeroFill(this.seconds(), 2); +}); + function meridiem (token, lowercase) { addFormatToken(token, 0, 0, function () { return this.localeData().meridiem(this.hours(), this.minutes(), lowercase); @@ -54,6 +63,8 @@ addRegexToken('hh', match1to2, match2); addRegexToken('hmm', match3to4); addRegexToken('hmmss', match5to6); +addRegexToken('Hmm', match3to4); +addRegexToken('Hmmss', match5to6); addParseToken(['H', 'HH'], HOUR); addParseToken(['a', 'A'], function (input, array, config) { @@ -78,6 +89,18 @@ addParseToken('hmmss', function (input, array, config) { array[SECOND] = toInt(input.substr(pos2)); getParsingFlags(config).bigHour = true; }); +addParseToken('Hmm', function (input, array, config) { + var pos = input.length - 2; + array[HOUR] = toInt(input.substr(0, pos)); + array[MINUTE] = toInt(input.substr(pos)); +}); +addParseToken('Hmmss', function (input, array, config) { + var pos1 = input.length - 4; + var pos2 = input.length - 2; + array[HOUR] = toInt(input.substr(0, pos1)); + array[MINUTE] = toInt(input.substr(pos1, 2)); + array[SECOND] = toInt(input.substr(pos2)); +}); // LOCALES diff --git a/src/test/moment/create.js b/src/test/moment/create.js index 7d4670a1e..e58ca2660 100644 --- a/src/test/moment/create.js +++ b/src/test/moment/create.js @@ -1016,4 +1016,12 @@ test('hmm', function (assert) { assert.equal(moment('112345', 'hmmss', true).format('HH:mm:ss'), '11:23:45', '112345 with hmmss'); assert.equal(moment('112345a', 'hmmssA', true).format('HH:mm:ss'), '11:23:45', '112345a with hmmssA'); assert.equal(moment('112345p', 'hmmssA', true).format('HH:mm:ss'), '23:23:45', '112345p with hmmssA'); + + assert.equal(moment('023', 'Hmm', true).format('HH:mm:ss'), '00:23:00', '023 with Hmm'); + assert.equal(moment('123', 'Hmm', true).format('HH:mm:ss'), '01:23:00', '123 with Hmm'); + assert.equal(moment('1234', 'Hmm', true).format('HH:mm:ss'), '12:34:00', '1234 with Hmm'); + assert.equal(moment('1534', 'Hmm', true).format('HH:mm:ss'), '15:34:00', '1234 with Hmm'); + assert.equal(moment('12345', 'Hmmss', true).format('HH:mm:ss'), '01:23:45', '12345 with Hmmss'); + assert.equal(moment('112345', 'Hmmss', true).format('HH:mm:ss'), '11:23:45', '112345 with Hmmss'); + assert.equal(moment('172345', 'Hmmss', true).format('HH:mm:ss'), '17:23:45', '112345 with Hmmss'); }); diff --git a/src/test/moment/format.js b/src/test/moment/format.js index f12a62ce6..0e33700a8 100644 --- a/src/test/moment/format.js +++ b/src/test/moment/format.js @@ -381,3 +381,14 @@ test('hmm and hmmss', function (assert) { assert.equal(moment('01:34:56', 'HH:mm:ss').format('hmmss'), '13456'); assert.equal(moment('13:34:56', 'HH:mm:ss').format('hmmss'), '13456'); }); + +test('Hmm and Hmmss', function (assert) { + assert.equal(moment('12:34:56', 'HH:mm:ss').format('Hmm'), '1234'); + assert.equal(moment('01:34:56', 'HH:mm:ss').format('Hmm'), '134'); + assert.equal(moment('13:34:56', 'HH:mm:ss').format('Hmm'), '1334'); + + assert.equal(moment('12:34:56', 'HH:mm:ss').format('Hmmss'), '123456'); + assert.equal(moment('01:34:56', 'HH:mm:ss').format('Hmmss'), '13456'); + assert.equal(moment('08:34:56', 'HH:mm:ss').format('Hmmss'), '83456'); + assert.equal(moment('18:34:56', 'HH:mm:ss').format('Hmmss'), '183456'); +}); -- 2.47.2