Skip to content

Instantly share code, notes, and snippets.

@lasryaric
Last active December 11, 2015 07:38
Show Gist options
  • Select an option

  • Save lasryaric/4567192 to your computer and use it in GitHub Desktop.

Select an option

Save lasryaric/4567192 to your computer and use it in GitHub Desktop.
producteevApp.dateHelper = function (date, region, timezoneOffset)
{
//For timezone handling, check: http://stackoverflow.com/questions/10438579/getting-moment-js-to-show-datetimes-appropriate-to-info-being-viewed
//TODO: test this function for cross browser compatibility
//example : moment("2013-01-15T06:34:09+0000");
var momentInstance = moment(date);
momentInstance = (momentInstance && momentInstance.isValid()) ? momentInstance : moment(); //valid date || now()
if (_.isUndefined(timezoneOffset)) {
throw ("timezoneOffset not defined");
}
//Reset the moment to the UTC time
momentInstance.add('minutes', moment().zone());
//Set the moment to the user timezoneOffset time
momentInstance.add('seconds', timezoneOffset);
//first region will be the defualt region
var regions = ['us', 'eu'];
region = (regions.indexOf(region) > -1) ? region : regions[0];
var formats = {
'us': {
'dateHumanFriendly':'ddd, MMMM D', //Fri, January 13
'date': 'MM/DD/YYYY', //12/31/2012
'dateTime': 'MM/DD/YYYY - hh:mma', //12/31/2012 - 03:30pm
'time': 'hh:mma', //03:30pm
'jqueryTimePicker': 'hh:mma' //03:30pm
},
'eu': {
'dateHumanFriendly':'ddd, MMMM D', //Fri, January 13
'date': 'DD/MM/YYYY', //31/12/2013
'dateTime': 'DD/MM/YYYY - hh:mma', //31/12/2013 - 15:30pm
'time': 'HH:mm', //23:30
'jqueryTimePicker': 'HH:mm' //23:30
}
};
var jqueryTimePickerFormatStrings = {
'us': 'h:ia',
'eu': 'H:i'
};
this.getHumanFriendy = function() {
var format = formats[region]['dateHumanFriendly'];
return momentInstance.format(format);
};
this.getDate = function() {
var format = formats[region]['date'];
return momentInstance.format(format);
};
this.getDateTime = function() {
var format = formats[region]['dateTime'];
return momentInstance.format(format);
};
this.getTime = function() {
var format = formats[region]['time'];
return momentInstance.format(format);
};
this.jqueryTimePickerFormat = function() {
var format = jqueryTimePickerFormatStrings[region];
return format;
};
this.getAgo = function() {
var momentNow = moment();
return momentInstance.from(momentNow);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment