Skip to content

Instantly share code, notes, and snippets.

@TastyToast
Last active October 5, 2015 18:18
Show Gist options
  • Save TastyToast/2853317 to your computer and use it in GitHub Desktop.
Save TastyToast/2853317 to your computer and use it in GitHub Desktop.
Show elements based on difference in time with moment.js
<script src="https://s3.amazonaws.com/wildfireapp/assets/momentjs/moment.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var currentDate = moment().utc();
//Time is set to PST (UTC minus 8 for time zone offset)
//var startDate = moment('1-24-2013 14 -08:00', 'MM-DD-YYYY HH Z').utc();
// Start Date should be set in following format: 1-24-2013
// Start Time should be set in 24 hour format. So, for 2pm, use following: 14
var startDate = moment('{{ start_date }} {{ start_time }} -08:00', 'MM-DD-YYYY HH Z').utc();
//Setting true as third argument for diff to allow for fractions so number doesn't round itself
var difference = currentDate.diff(startDate, 'days', true);
$('.calendar li').each(function(){
var indexNumber = $(this).index();
if (indexNumber > difference - 1) {
$(this).addClass('currentDay');
$('.currentDay ~ li').removeClass('currentDay').addClass('futureDay');
$('.currentDay').prevAll().addClass('pastDay');
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment