1

im trying to set endtime(expired/CountDown)

       <p id="demo"></p>

<script>
    // Set the date we're counting down to
    var countDownDate = new Date("Jan 5, 2018 15:37:25").getTime();

    // Update the count down every 1 second
    var x = setInterval(function () {

        // Get todays date and time
        var now = new Date().getTime();

        // Find the distance between now an the count down date
        var distance = countDownDate - now;

        // Time calculations for days, hours, minutes and seconds
        var days = Math.floor(distance / (1000 * 60 * 60 * 24));
        var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        var seconds = Math.floor((distance % (1000 * 60)) / 1000);

        // Output the result in an element with id="demo"
        document.getElementById("demo").innerHTML = days + "d " + hours + "h "
        + minutes + "m " + seconds + "s ";

        // If the count down is over, write some text 
        if (distance < 0) {
            clearInterval(x);
            document.getElementById("demo").innerHTML = "EXPIRED";
        }
    }, 1000);
</script>

this code worked but the date is static i need to change var countDownDate = new Date("Jan 5, 2018 15:37:25").getTime(); to value from database how do that ?

in asp.net and entitiyframework

  • Two ways come to mind: 1) retrieve the value from the DB using AJAX 2) bind the value to a hidden input when the page is loaded. See also http://stackoverflow.com/questions/1244094/converting-json-results-to-a-date on how to convert the AJAX response to a JS Date object. – Georg Patscheider Mar 27 '17 at 15:07
  • Did you solve the the problem? – Marius Orha Apr 15 '17 at 08:02

1 Answers1

0

You can create an ApiController, with a method that gets data from the database and returns it, then using ajax request you get your data in javascript. You have here an example:

public class YourApiController : ApiController
{

    [HttpGet]
    [Route("api/getDateFromServer")]
    public DateTime GetDateFromServer()
    {
        //TODO retrieve data from where you want, by now we will return DateTime.Now
        return DateTime.Now;
    }
}

Then, in your javascript you add this:

    $.ajax({
        type: 'GET',
        context: this, 
        url: '/api/getDateFromServer',
    }).done(function (data) {
       //Here you have your data from server in your javascript
       countDownDate = data;
    });
Marius Orha
  • 670
  • 1
  • 9
  • 26