JS date picker for sales ending on the 15th or last day of month

Posted on Posted in Tips

I needed to write a script to show sale ending dates so I would not have to update them site-wide. I used moment.js. Some might argue that document.write is deprecated but I’m not using it in a function. Get a copy of moment.js and call it into your page. Copy and paste code replacing comments with actual opening and closing script tags. Good luck.


Sale ends 
<--open script tag-->
var currentDay = moment().format('DD'); //curr day
var currentMo = moment().format('MM'); //curr month
var currentYear = moment().format('Y'); //curr year
var ldcm = moment().endOf('month').format('DD'); //last day of current month

if (currentDay < 15) {
	document.write('Sale ends' + currentMo + '/15/' + currentYear + 'or while supplies last.');
 }else{
 if (currentDay == 15){
 	document.write ('Sale ends today. Don't wait.');
 }else{
 	if (currentDay == ldcm){
 		document.write (' Sale ends today ');
 	}else{
document.write('Sale ends' + currentMo + '/' + ldcm + '/' +currentYear);
 }
}}
 
<--closing script tag-->