How do I share variables between helpers?

I’m really new to Meteor and Javascript, so I might be doing this in the most convoluted way but any help or advice would be greatly appreciated!

I have all these variables that are repeated between helpers in the same template. Is there a way to condense this?

Template.eventinfo.helpers({
	'displayDate': function(){
		var datestart = this.datestart;
		var d = new Date();
		var curr_date = d.getDate();
		var curr_month = d.getMonth() + 1;
		var curr_year = d.getFullYear();
		var today = (curr_year + "-" + curr_month + "-" + curr_date);
		var todayday = today.slice(8)
		var startday = datestart.slice(8);
		if (datestart >= today){
			return startday;
		} else {
			return todayday;
		}
	},
	'displayMonth': function(){
		var datestart = this.datestart;
		var d = new Date();
		var curr_date = d.getDate();
		var curr_month = d.getMonth() + 1;
		var curr_year = d.getFullYear();
		var today = (curr_year + "-" + curr_month + "-" + curr_date);
		var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
		var startmostr = datestart.slice(5,7);
		var thismonth = monthNames[d.getMonth()];
		var startmonth = monthNames[startmostr-1];
		if (datestart >= today){
			return startmonth;
		} else {
			return thismonth;
		}
	}
});

Basically I’m comparing a date that the user submits through a form that is formatted as yyyy-mm-dd to new Date() and then picking out the different parts so I can style them differently.

function getToday() {
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1;
    var curr_year = d.getFullYear();
    var today = curr_year + "-" + curr_month + "-" + curr_date

    return today
}

Template.eventinfo.helpers({
	'displayDate': function(){
		var datestart = this.datestart;
		var today = getToday()
		var todayday = today.slice(8)
		var startday = datestart.slice(8);
		if (datestart >= today){
			return startday;
		} else {
			return todayday;
		}
	},
	'displayMonth': function(){
		var datestart = this.datestart;
		var today = getToday()
		var monthNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
		var startmostr = datestart.slice(5,7);
		var thismonth = monthNames[d.getMonth()];
		var startmonth = monthNames[startmostr-1];
		if (datestart >= today){
			return startmonth;
		} else {
			return thismonth;
		}
	}
});

You should have a look at this http://momentjs.com/ :wink: Meteor package: https://atmospherejs.com/momentjs/moment

1 Like

Oh! I didn’t know that could be done! Thanks!

1 Like

Awesome! Thank you! It’s so useful!

For anyone who stumbles upon this and is doing some date formatting, momentjs is the way to go.

I simplified most of my code to things like:

function getToday() { var today = moment().format('YYYY-MM-DD'); return today; }

moment() is equal to moment(new Date())

Or instead of splicing the month out of date strings with an array, I can do:

var datestart = this.datestart; var startmonth = moment(datestart).format('MMM');

1 Like

We’re here to share information :+1:

1 Like