Skip to content

Commit

Permalink
Now there is a function that apply locale and timezone and each helpe…
Browse files Browse the repository at this point in the history
…r format the date
  • Loading branch information
Boris Schapira committed Jun 24, 2015
1 parent b3fc6a8 commit 7278da6
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions lib/plugins/helper/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ var moment = require('moment-timezone');
var isMoment = moment.isMoment;
var isDate = require('util').isDate;

function output(date, format, lang, timezone){
function getMoment(date, lang, timezone){
if (date == null) date = moment();
if (!isMoment(date)) date = moment(isDate(date) ? date : new Date(date));

if (lang) date = date.locale(lang);
if (timezone) date = date.tz(timezone);

if(format != null) {
return date.format(format);
} else {
return date.fromNow();
}
return date;
}

function toISOString(date){
Expand All @@ -32,26 +28,30 @@ function toISOString(date){

function dateHelper(date, format){
/* jshint validthis: true */
var config = this.config;
return output(date, format || config.date_format, getLanguage(this), config.timezone);
var config = this.config,
moment = getMoment(date, getLanguage(this), config.timezone);
return moment.format(format || config.date_format);
}

function fromNowHelper(date){
/* jshint validthis: true */
var config = this.config;
return output(date, null, getLanguage(this), config.timezone);
var config = this.config,
moment = getMoment(date, getLanguage(this), config.timezone);
return moment.fromNow();
}

function timeHelper(date, format){
/* jshint validthis: true */
var config = this.config;
return output(date, format || config.time_format, getLanguage(this), config.timezone);
var config = this.config,
moment = getMoment(date, getLanguage(this), config.timezone);
return moment.format(format || config.time_format);
}

function fullDateHelper(date, format){
/* jshint validthis: true */
if (format){
return output(date, format, getLanguage(this), this.config.timezone);
var moment = getMoment(date, getLanguage(this), this.config.timezone);
return moment.format(format);
} else {
return this.date(date) + ' ' + this.time(date);
}
Expand Down

0 comments on commit 7278da6

Please sign in to comment.