Created
October 21, 2015 01:14
-
-
Save OzuYatamutsu/e1aef4dd2deb10e6b780 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// daysuntil.js | |
// (C) 2012+ by Steakscorp Labs and Jinhai Steakhouse | |
// Calculates the number of days between two dates, or | |
// the number of days between today and some date in | |
// the future | |
// Number of milliseconds per day | |
const ONE_DAY = 86400000; | |
const MONTHS = ["January", "February", "March", "April", "May", | |
"June", "July", "August", "September", "October", | |
"November", "December"] | |
// If no parser string, default to this one | |
const defaultDateString = "from=xxxxxxxx&to=11082015" | |
function runAll() { | |
// Parses URL, determines date, determines length between dates | |
// and finally posts it on the webpage | |
var dates = formatMyan(parseURL()); | |
var dateArray = converterMyan(dates); | |
var daysCount = daysUntil(dateArray[0], dateArray[1]); | |
webPost(daysCount, dateTranslate(dateArray[1])); | |
} | |
function parseURL() { | |
// Parses URL | |
var parser = document.createElement('a'); | |
parser.href = document.URL; | |
if (parser.search == "") { | |
parser.search = defaultDateString; | |
} | |
return parser.search; | |
} | |
function formatMyan(str) { | |
// Parses search string and returns an array of dates | |
var elementOne = str.split('&')[0].substr(6); | |
var elementTwo = str.split('&')[1].substr(3); | |
var formatArray = [elementOne, elementTwo]; | |
return formatArray; | |
} | |
function converterMyan(dateArray) { | |
// Takes in an string array and returns an array of dates | |
var finalArray = new Array(); | |
if (dateArray[0] == "xxxxxxxx") { | |
finalArray[0] = new Date(); | |
// Below: get Date in current timezone | |
//finalArray[0] = new Date(getFullYear(), getMonth(), | |
// getDate(), getHours(), getMinutes(), | |
// getSeconds(), getMilliseconds()); | |
} | |
else { | |
finalArray[0] = new Date( | |
parseInt(dateArray[0].substr(4,8)), | |
parseInt(dateArray[0].substr(0,2)) - 1, | |
parseInt(dateArray[0].substr(2,2))); | |
} | |
finalArray[1] = new Date( | |
parseInt(dateArray[1].substr(4,8)), | |
parseInt(dateArray[1].substr(0,2)) - 1, | |
parseInt(dateArray[1].substr(2,2))); | |
return finalArray; | |
} | |
function daysUntil(date1, date2) { | |
// Conversion of both dates to milliseconds | |
var date1_ms = date1.getTime(); | |
var date2_ms = date2.getTime(); | |
var difference; | |
if (date1_ms < date2_ms) { | |
// Calculate difference | |
difference = Math.abs(date1_ms - date2_ms); | |
} | |
else { | |
difference = 0; | |
} | |
// Now convert to days | |
return Math.round(difference/ONE_DAY); | |
} | |
function dateTranslate(date) { | |
// Translates a number date into a full text date | |
var month = MONTHS[date.getMonth()]; | |
var day = date.getDate(); | |
var year = date.getFullYear(); | |
// Put it together | |
var textDate = month + " " + day + ", " + year; | |
return textDate; | |
} | |
function webPost(text, text2) { | |
// Finally, post it to the webpage | |
document.getElementById('daysuntil').innerHTML = text; | |
document.getElementById('enddate').innerHTML = text2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment