Skip to content

Instantly share code, notes, and snippets.

@michael-milette
Created December 13, 2018 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michael-milette/249166416d2fb393c450b4cfc48beb80 to your computer and use it in GitHub Desktop.
Save michael-milette/249166416d2fb393c450b4cfc48beb80 to your computer and use it in GitHub Desktop.
/**
* Description: This JavaScript enables you to set a launch and/or date and time when
* content will appear as well as when it will be removed from a page.
*
* Usage:
* <div class="AtDate" data-launch="2018-12-01T00:00:01-08:00" data-expire="2018-12-14T23:59:59-08:00">Limite lifespan content.</div>
* <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
* <script src="atdate.js"></script>
*
* Notes:
* - Need not have both data-launch and data-expire. Can just have one or the other.
* - You can apply the class and data to almost any HTML tag, not just a div.
*
* Author: Michael Milette
* Copyright 2018 TNG Consulting Inc. - www.tngconsulting.ca
* License: GPLv3+ license.
*/
$(document).ready(function() {
$(".AtDate").each(function() {
var dateExpire = Date.parse($(this).data('expire'));
if(!isNaN(dateExpire) && Date.now() - dateExpire > 0) {
$(this).remove(); // Expired!
} else {
var dateLaunch = Date.parse($(this).data('launch'));
if(!isNaN(dateLaunch)) {
if(Date.now() - dateLaunch > 0) {
$(this).show(); // Time to launch!
} else {
$(this).hide(); // Not yet!
}
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment