Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save biancalpadilla/9355bc153b669402125a5a0c370f9c2a to your computer and use it in GitHub Desktop.
Save biancalpadilla/9355bc153b669402125a5a0c370f9c2a to your computer and use it in GitHub Desktop.
Estimated date
<span style="text-align:center" id="delivery-date"></span>
<script>
if (new Date().getHours() < 13) {
var str = '#{firstDate} - #{secondDate}';
} else {
var str = '#{firstDate} - #{secondDate}';
}
function turnAroundDate(startDate, daysToAdd) {
var endDate = "", count = 0;
while (count < daysToAdd){
// add a day to endDate until "days added" equals "days to add"
endDate = new Date(startDate.setDate(startDate.getDate() + 1));
// if endDate is not Sunday or Saturday
if (endDate.getDay() != 0 && endDate.getDay() != 6) {
// add a day to "days added"
count++;
}
}
return endDate;
}
function formatDate(date) {
var dayOfWeek = [ 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday','Saturday' ][date.getDay()];
var month = [ "January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November","December" ][date.getMonth()];
return dayOfWeek + ', ' + month + ' ' + date.getDate();
}
function formatString(firstDate, secondDate) {
return str.replace('#{firstDate}', formatDate(firstDate))
.replace('#{secondDate}', formatDate(secondDate));
}
if (new Date().getDay() != 0 && new Date().getDay() != 6) {
var time = new Date().getHours() < 13 ? 0 : 1;
var start = 2 + time
var end = 3 + time
} else {
var start = 3
var end = 4
}
var firstDate = turnAroundDate(new Date(), start);
var secondDate = turnAroundDate(new Date(), end);
document.getElementById('delivery-date').innerHTML = formatString(firstDate, secondDate);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment