Use moment to render dates everywhere

This commit is contained in:
Pavel Djundik 2017-04-29 13:31:04 +03:00
parent d6ca06b12b
commit 700d3c1ff2
6 changed files with 14 additions and 20 deletions

View File

@ -4,10 +4,10 @@ const moment = require("moment");
module.exports = function(time) { module.exports = function(time) {
// See http://momentjs.com/docs/#/displaying/calendar-time/ // See http://momentjs.com/docs/#/displaying/calendar-time/
return moment(new Date(time)).calendar(null, { return moment(time).calendar(null, {
sameDay: "[Today]", sameDay: "[Today]",
lastDay: "[Yesterday]", lastDay: "[Yesterday]",
lastWeek: "L", // Locale lastWeek: "D MMMM YYYY",
sameElse: "L" sameElse: "D MMMM YYYY"
}); });
}; };

View File

@ -1,5 +1,7 @@
"use strict"; "use strict";
const moment = require("moment");
module.exports = function(time) { module.exports = function(time) {
return new Date(time).toLocaleDateString(); return moment(time).format("D MMMM YYYY");
}; };

View File

@ -1,5 +1,7 @@
"use strict"; "use strict";
const moment = require("moment");
module.exports = function(time) { module.exports = function(time) {
return new Date(time).toLocaleString(); return moment(time).format("D MMMM YYYY, HH:mm:ss");
}; };

View File

@ -1,17 +1,7 @@
"use strict"; "use strict";
const moment = require("moment");
module.exports = function(time) { module.exports = function(time) {
time = new Date(time); return moment(time).format("HH:mm");
var h = time.getHours();
var m = time.getMinutes();
if (h < 10) {
h = "0" + h;
}
if (m < 10) {
m = "0" + m;
}
return h + ":" + m;
}; };

View File

@ -18,7 +18,7 @@ describe("friendlydate Handlebars helper", () => {
it("should not render any friendly dates prior to the day before", () => { it("should not render any friendly dates prior to the day before", () => {
[2, 7, 30, 365, 1000].forEach(day => { [2, 7, 30, 365, 1000].forEach(day => {
const time = new Date().getTime() - 24 * 3600 * 1000 * day; const time = new Date().getTime() - 24 * 3600 * 1000 * day;
expect(friendlydate(time)).to.equal(moment(time).format("L")); expect(friendlydate(time)).to.equal(moment(time).format("D MMMM YYYY"));
}); });
}); });
}); });

View File

@ -14,6 +14,6 @@ describe("localetime Handlebars helper", () => {
// Pretend local timezone is UTC by moving the clock of that offset // Pretend local timezone is UTC by moving the clock of that offset
const time = date.getTime() + offset; const time = date.getTime() + offset;
expect(localetime(time)).to.equal("5/22/2014, 12:00:00 PM"); expect(localetime(time)).to.equal("22 May 2014, 12:00:00");
}); });
}); });