ProtoTyPus ProtoTyPus - 3 years ago 197
HTML Question

Reading the Date from div

I'm making a web page and I have to make a countdown that read the date from a div (the div read the date from database).

When it takes the string, it behaves as it has not read my input date, although it reads the famous "1 january 1970".

JavaScript:

<script language="JavaScript">

var gcor = new Date();
var dscad = new Date(document.getElementById("caramello"));
var difftempo = (dscad - gcor) / 1000;
var minuto = 60;
var ora = 60 * 60;
var giorno = 60 * 60 * 24;
var giorest = Math.floor(difftempo / giorno)
var orerest = Math.floor((difftempo - giorest * giorno) / ora)
var minrest = Math.floor((difftempo - giorest * giorno - orerest * ora) / minuto)
var secrest = Math.floor((difftempo - giorest * giorno - orerest * ora - minrest * minuto))

ID = window.setTimeout("update();", 1000);

function update() {
gcor = new Date();
difftempo = (dscad - gcor) / 1000;
minuto = 60;
ora = 60 * 60;
giorno = 60 * 60 * 24;
giorest = Math.floor(difftempo / giorno)
orerest = Math.floor((difftempo - giorest * giorno) / ora)
minrest = Math.floor((difftempo - giorest * giorno - orerest * ora) / minuto)
secrest = Math.floor((difftempo - giorest * giorno - orerest * ora - minrest * minuto))

document.getElementById("Label1").innerText = giorest;
if (giorest < 10) {
document.getElementById("Label2").innerText = "0" + orerest;
}
else {
document.getElementById("Label2").innerText = orerest;
}
if (minrest < 10) {
document.getElementById("Label3").innerText = "0" + minrest;
}
else {
document.getElementById("Label3").innerText = minrest;
}
if (secrest < 10) {
document.getElementById("Label4").innerText = "0" + secrest;
}
else {
document.getElementById("Label4").innerText = secrest;
}
ID = window.setTimeout("update();", 1000);
}


and this is the html/asp that is linked with it:

<body>
<form runat="server">
<div>
<div id="caramello" onclick="prova();">18 july 2013</div><br />
<br />
<asp:Label ID="Label6" runat="server" Text="Label"></asp:Label>
<br />
<asp:Label ID="Label1" runat="server" Text="00"></asp:Label>g
<asp:Label ID="Label2" runat="server" Text="00"></asp:Label>h
<asp:Label ID="Label3" runat="server" Text="00"></asp:Label>m
<asp:Label ID="Label4" runat="server" Text="00"></asp:Label>s
</div>
</form>



Answer Source

Try something like my jsfiddle: http://jsfiddle.net/YTAXM/

<div id="caramello">July 18 2013</div>
<br />
<br />
<div ID="Label6"></div>
<br />
<div ID="Label1">00</div>g
<div ID="Label2">00</div>h
<div ID="Label3">00</div>m
<div ID="Label4">00</div>s

var dscad = new Date(document.getElementById("caramello").innerHTML);

function update() {

    var gcor = new Date();
    difftempo = (dscad - gcor) / 1000;
    minuto = 60;
    ora = 60 * 60;
    giorno = 60 * 60 * 24;
    giorest = Math.floor(difftempo / giorno)
    orerest = Math.floor((difftempo - giorest * giorno) / ora)
    minrest = Math.floor((difftempo - giorest * giorno - orerest * ora) / minuto)
    secrest = Math.floor((difftempo - giorest * giorno - orerest * ora - minrest * minuto))

    document.getElementById("Label1").innerHTML = giorest;

    document.getElementById("Label2").innerHTML = orerest;

    document.getElementById("Label3").innerHTML = minrest;

    document.getElementById("Label4").innerHTML = secrest;
}



setInterval(function () {
     update();
}, 1000);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download