Add Simple countdown timer to your shopify store via JavaScript

Before you start it

  • Dawn theme version at this post is 6.0.2
  • Duplicate your theme

Quick Look 👀

Getting started ✏️

flash-sale-script.liquid

  • Online store > themes > Actions > Edit code > Snippets > ➕ Create a new snippet

  • Paste code and save

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<script>
/*
Javascript count down timer
data format: 2021-10-7 18:00:00
*/
(function () {

function convertDateForIos(date) {
var arr = date.split(/[- :]/);
date = new Date(arr[0], arr[1]-1, arr[2], arr[3], arr[4], arr[5]).getTime();
return date;
}
const second = 1000,
minute = second * 60,
hour = minute * 60,
day = hour * 24;
// get all timer placeholder by class
const timers = document.querySelectorAll('.CountdownTimer');

let endDay = "2022-08-29 18:00:00",
countDown = convertDateForIos(endDay);
let x = '';
let timerHtml = '';
x = setInterval(function ins1() {
// lock time zone
let c = new Date().toLocaleString('en-US', { timeZone: 'Europe/London' });
let sensibleFormat = new Date(c);

let now = sensibleFormat.getTime(),
distance = countDown - now;
let dayText = Math.floor(distance / (day));
let hoursText = Math.floor((distance % (day)) / (hour));
let minutesText = Math.floor((distance % (hour)) / (minute));
let secondText = Math.floor((distance % (minute)) / second);
if(dayText){
timerHtml = '<b>'+dayText+'</b>d <b>'+hoursText+'</b>h <b>'+minutesText+'</b>m <b>'+secondText.toString().padStart(2, '0')+'</b>s';
}else{
timerHtml = '<b>'+hoursText+'h '+minutesText+'m '+secondText+'s</b>';
}
timers.forEach(function(element){
element.innerHTML = timerHtml;
});
//do something later when date is reached
if (distance < 0) {
clearInterval(x);
}
return ins1;
//seconds
}(), 1000);
}());

</script>

theme.liquid

Paste code before </body>

1
{% include 'flash-sale-script' %}

How to use

announcement-bar.liquid

  • Online store > themes > Actions > Edit code > Sections > announcement-bar

  • Paste code at the position you want ( timer is right after announcement bar text

1
<span class="CountdownTimer"></span>