Simple Countdown based on Date using JavaScript | Sanwebcorner

Simple Countdown based on Date using JavaScript

This Post help you how to create countdown based on your given date. It automatically takes the countdown using date you are provided. It displays the years, months, days, hours, minutes and seconds. It is very useful display the counter for the event launching and any other new product launching system.This countdown display with animation countdown cube.If you don't want display some fields like years then you should remove the fields in JavaScript.

You can change the date in your index.php script. Find the script code target: new Date( 'Feb 10, 2018 7:47:00' ), in index file and change the date according to your choice...

You can able to change the time zone in the below javscript code i am using here Asia/Kolkata the default time zone is UTC, It will convert the utc to Asia/Kolkata. This concept is build with given end time, It automatically calculated the years, months, days, hours, minutes, seconds and display the results in the cube div.


The below html code is used to display the full countdown
<div id='counter' class='counter'></div>
And the countdowncube.css file used design the counter boxes in a perfect manner you can able to change the styles and colors according to your choice to edit the countdowncube.css file. and the countdowncube.js have the javascripts to display the counter you can edit and customize the counters according to your choice.

I hope this simple concept is very useful to you. This example have the demo page please check out the demo page and download the full code and use it to your website.

Countdown based on given date:

<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Simple Countdown based on Date using Jquery</title>
<meta name="description" content="Personal Website of Ali Cigari, jack of all trades...">
<meta name="viewport" content="initial-scale=1.0,width=device-width,user-scalable=0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<link rel="stylesheet" href="countdowncube.css">
<style class="inlinestyle">
.counter {
margin: 10 auto;
text-align: center;
}
body
{
background-color: rebeccapurple;
}
</style>
</head>
<body>
<div id='counter' class='counter'></div>

<script src="countdowncube.js"></script>
<script>
var targetDate = new Date();
targetDate.setMonth(targetDate.getMonth() + 18)
targetDate.setDate(1)
targetDate.setHours(11)
targetDate.setMinutes(0)
targetDate.setSeconds(0)
targetDate.setMilliseconds(0);

var targetDateStringUTC = targetDate.toISOString()
.replace(':00.000', '');
var targetDateString = targetDateStringUTC.replace('Z', '');

var targetLADate = moment.tz(targetDateStringUTC,
'Asia/Kolkata').format();
var tzLAOffset = targetLADate.substr(-6);
var targetDateStringOffset = targetDateString.replace('Z', '')
.replace(':00.000', '') +
tzLAOffset;

var tzOldFormatOffset = targetDateString.substr(-5);
var targetDateOldFormatString = targetDate.toLocaleDateString() +
' ' + tzOldFormatOffset;

var pastDate = new Date();
pastDate.setMonth(targetDate.getMonth() - 18)
pastDate.setMinutes(0);
pastDate.setSeconds(0);
pastDate.setMilliseconds(0);
var targetDateStringPast = pastDate.toISOString()
.replace(':00.000', '');
var nearFutureDate = new Date();
nearFutureDate.setSeconds(nearFutureDate.getSeconds() + 10);
nearFutureDate.setMilliseconds(0);
var nearFutureDateString = nearFutureDate.toISOString()
function reloadCounter(element_id,
new_target,
targetTimezone,
cubeSize,
background,
color,
onEnd,
triggerEnd) {

$(element_id).empty();

$(element_id).countdownCube( {
target: new_target,
targetTimezone: targetTimezone,
cubeSize: cubeSize,
background:  background,
color: color,
onEnd: onEnd,
triggerEnd: triggerEnd,
} );
};
</script>
<script>
$('#counter').countdownCube( {
target: targetDateStringUTC,
cubeSize: 150,
background:  '#ffff00',
color: 'blue',
} );

$('#counter-end-notrigger').countdownCube( {
target: targetDateStringPast,
targetTimezone: 'UTC',
cubeSize: 150,
background:  'lightcyan',
color: 'darkcyan',
/*
target is in the past and triggerEnd is false,
so onEnd is not triggered when the page is loaded
*/
onEnd: function(e) {
$("#counter-end-notrigger")
.text('This was the end, but it was not triggered! ' +
'(counter original target: ' +
e.options.targetDateObject.toISOString() +
')');
}
} );
$('#counter-end-trigger').countdownCube( {
target: targetDateStringPast,
targetTimezone: 'UTC',
cubeSize: 150,
background:  'lightcyan',
color: 'cyan',
onEnd: function(e) {
$("#counter-end-trigger")
.text('This was the end and it was triggered! ' +
'(counter original target: ' +
e.options.targetDateObject.toISOString() +
')');
},
triggerEnd: true,
} );

$('#counter').countdownCube({
target: new Date( 'Feb 10, 2018 7:47:00' ),
cubeSize: 50
});
</script>
</body>
</html>