]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
[feature] add format constants to support output to HTML5 input type formats (see...
authorTuan Nguyen <lightbringer1991@yahoo.com>
Sun, 17 Dec 2017 00:28:21 +0000 (11:28 +1100)
committerKunal Marwaha <marwahaha@berkeley.edu>
Sun, 17 Dec 2017 00:32:59 +0000 (19:32 -0500)
src/moment.js
src/test/moment/format.js

index c023962883f362320bff32975c7b9e6a46d2eddc..1f97597bf2d6de52583fcc059bcb45af5d2c1f28 100644 (file)
@@ -79,4 +79,17 @@ moment.relativeTimeThreshold = relativeTimeThreshold;
 moment.calendarFormat        = getCalendarFormat;
 moment.prototype             = fn;
 
+// currently HTML5 input type only supports 24-hour formats
+moment.HTML5_FMT = {
+    DATETIME_LOCAL: 'YYYY-MM-DDTHH:mm',             // <input type="datetime-local" />
+    DATETIME_LOCAL_SECONDS: 'YYYY-MM-DDTHH:mm:ss',  // <input type="datetime-local" step="1" />
+    DATETIME_LOCAL_MS: 'YYYY-MM-DDTHH:mm:ss.SSS',   // <input type="datetime-local" step="0.001" />
+    DATE: 'YYYY-MM-DD',                             // <input type="date" />
+    TIME: 'HH:mm',                                  // <input type="time" />
+    TIME_SECONDS: 'HH:mm:ss',                       // <input type="time" step="1" />
+    TIME_MS: 'HH:mm:ss.SSS',                        // <input type="time" step="0.001" />
+    WEEK: 'YYYY-[W]WW',                             // <input type="week" />
+    MONTH: 'YYYY-MM'                                // <input type="month" />
+};
+
 export default moment;
index 195469a7807773f7fc55beb0699e4eae8d3f1d49..a637d51b28f2866df19bbaa8437517f755df8646 100644 (file)
@@ -4,6 +4,19 @@ import moment from '../../moment';
 
 module('format');
 
+test('format using constants', function (assert) {
+    var m = moment('2017-09-01T23:40:40.678');
+    assert.equal(m.format(moment.HTML5_FMT.DATETIME_LOCAL), '2017-09-01T23:40', 'datetime local format constant');
+    assert.equal(m.format(moment.HTML5_FMT.DATETIME_LOCAL_SECONDS), '2017-09-01T23:40:40', 'datetime local format constant');
+    assert.equal(m.format(moment.HTML5_FMT.DATETIME_LOCAL_MS), '2017-09-01T23:40:40.678', 'datetime local format constant with seconds and millis');
+    assert.equal(m.format(moment.HTML5_FMT.DATE), '2017-09-01', 'date format constant');
+    assert.equal(m.format(moment.HTML5_FMT.TIME), '23:40', 'time format constant');
+    assert.equal(m.format(moment.HTML5_FMT.TIME_SECONDS), '23:40:40', 'time format constant with seconds');
+    assert.equal(m.format(moment.HTML5_FMT.TIME_MS), '23:40:40.678', 'time format constant with seconds and millis');
+    assert.equal(m.format(moment.HTML5_FMT.WEEK), '2017-W35', 'week format constant');
+    assert.equal(m.format(moment.HTML5_FMT.MONTH), '2017-09', 'month format constant');
+});
+
 test('format YY', function (assert) {
     var b = moment(new Date(2009, 1, 14, 15, 25, 50, 125));
     assert.equal(b.format('YY'), '09', 'YY ---> 09');