Here is the sample code for countdown function.
HTML
<script> function flashSaleCountdown() { let flash_sale_selector = "#flash_sale_list .prdList > li"; jQuery(flash_sale_selector).each(function(index, item){ try { var time_selector = jQuery(item).find(".discountPeriod .ec-base-tooltip .content p:nth-of-type(2)"); var raw_str_time = time_selector.text(); if (raw_str_time.indexOf("~") > -1) { var raw_time_split = raw_str_time.split(" ~ "); var start_full_date = raw_time_split[0]; var end_full_date = raw_time_split[1]; var start_date = convertStringToTime(start_full_date); var end_date = convertStringToTime(end_full_date); let showTimeArr = []; let countDownTxt = ""; if(isStarted(start_date)){ //On Flash Sale countDownTxt = "Flash sale"; showTimeTxt = end_full_date; jQuery(item).find('.flash_sale_proceed').show(); } else { //Coming soon countDownTxt = "Coming soon"; showTimeTxt = start_full_date; jQuery(item).find('.flash_sale_start_date').show(); jQuery(item).find(".time_left").text(showTimeTxt); } /* show countdown in circle */ var countDownTimes = countdown(showTimeTxt); jQuery(item).find(".day").text(countDownTimes[0]); jQuery(item).find(".hour").text(countDownTimes[1]); jQuery(item).find(".minute").text(countDownTimes[2]); jQuery(item).find(".second").text(countDownTimes[3]); jQuery(item).find('.flash_sale_time').show(); if (countDownTimes == false) { //END console.log("End"); jQuery(item).find('.flash_sale_time').hide(); jQuery(item).find('.flash_sale_done').show(); jQuery(item).find('.flash_sale_proceed').hide(); } } } catch (err) { console.error(index, err); } }) } setInterval(function(){ flashSaleCountdown(); }, 1000); </script>
Countdown.js
function countdown(fulltime) { var fulltimeArr = convertStringToTime(fulltime); var year = fulltimeArr[0]; var month = fulltimeArr[1]; var day = fulltimeArr[2]; var hour = fulltimeArr[3]; var minute = fulltimeArr[4]; var second = fulltimeArr[5]; let now = new Date().getTime(); let end = new Date(year, month, day, hour, minute, second).getTime(); let timeleft = end - now; var daysLeft = timeToString(Math.floor(timeleft / (1000 * 60 * 60 * 24))); var hoursLeft = timeToString(Math.floor((timeleft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))); var minutesLeft = timeToString(Math.floor((timeleft % (1000 * 60 * 60)) / (1000 * 60))); var secondsLeft = timeToString(Math.floor((timeleft % (1000 * 60)) / 1000)); let times; if (timeleft < 0) { times = false; } else { times = [daysLeft, hoursLeft, minutesLeft, secondsLeft]; } return times; } function timeToString(time){ time_int = Math.abs(parseInt(time)); return time_int < 10 ? "0" + time_int : time_int; } function convertStringToTime(fulltime){ var result = []; var fulltime_split = fulltime.split(" "); var date = fulltime_split[0]; var time = fulltime_split[1]; var date_array = date.split("-"); var year = date_array[2]; var month = date_array[1] - 1; var day = date_array[0]; var time_array = time.split(":"); var hour = time_array[0]; var minute = time_array[1]; var second = "00"; result = [year, month, day, hour, minute, second]; return result; } function isStarted(timeArr) { var start_year = timeArr[0]; var start_month = timeArr[1]; var start_day = timeArr[2]; var start_hour = timeArr[3]; var start_minute = timeArr[4]; var start_second = timeArr[5]; let now = new Date().getTime(); let start_from = new Date(start_year, start_month, start_day, start_hour, start_minute, start_second).getTime(); let start_time_left = start_from - now; if(start_time_left > 0 ) { //Coming Soon return false; }else { //Started return true; } }