Skip to content

Instantly share code, notes, and snippets.

@vitto
Last active May 31, 2016 08:28
Show Gist options
  • Save vitto/c2a88c81a8d770c15d5ed522d9a265c0 to your computer and use it in GitHub Desktop.
Save vitto/c2a88c81a8d770c15d5ed522d9a265c0 to your computer and use it in GitHub Desktop.
Google Calendar - add event duration
//cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js
//code.jquery.com/jquery-2.2.3.min.js
var events, checkEvents;
var roundHalf = function(num) {
return Math.round(num*2)/2;
}
var addDuration = function() {
$(".cbrd").each(function(){
var interval, text, time;
text = $(this).find('.chip-caption')
.clone()
.children()
.remove()
.end()
.text();
interval = text.split('–');
interval[0] = $.trim(interval[0]);
if (interval.length === 2) {
interval[1] = $.trim(interval[1]);
var now = moment('2013-01-01T' + interval[0] + ':00.000');
var end = moment('2013-01-01T' + interval[1] + ':00.000');
var duration = moment.duration(end.diff(now));
time = roundHalf(duration.asHours());
} else {
time = 0.5;
}
$(this).find('.chip-caption').text(text + '(' + time + ')');
$(this).addClass('cbrd--count-added');
});
};
checkEvents = setInterval(function(){
var jq = $ || false;
if (jq !== 'undefiend' && moment !== undefined) {
events = $('.cbrd').length;
filled = $('.cbrd--count-added').length;
if (events !== 0 && filled === 0) {
addDuration();
}
}
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment