locked
How to Get Current Date Time javascript ??? RRS feed

  • Question

  • User1002483009 posted

    I need Current date time in Server not allow localsytem and need future date is not select  give alert in javacsript fuction

    Thursday, September 5, 2013 1:20 AM

Answers

  • User440347874 posted

    Hello Jayajgiri

    The other way would be if you know the time zone of your server, you can manipulate that in Javascript itself based on the timezone. Basically the Date Object does the trick and javascript knows only the UDT and current time zone. So, for example if you want the time of EDT, get the UDT - 4 hours which would give the EDT.

    May be the below example would help you as I got this from the internet.

    var t = new Date();
    t =calcTime(0);
    alert(t);
    function calcTime(offset) {

        // create Date object for current location
        d = new Date();

        // convert to msec
        // add local time zone offset
        // get UTC time in msec
        utc = d.getTime() + (d.getTimezoneOffset() * 60000);
        /*alert(utc);
        alert(new Date(utc + (3600000*offset)));
        */
        return new Date(utc + (3600000*offset));

    }

    Thanks

    -Srini

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 5, 2013 6:06 AM

All replies

  • User-573138384 posted

    Assign server date to hidden field and do your validation, if time is not much of importance.

    Else, use jQuery ajax to get server date time and do validation.

    http://www.codeproject.com/Articles/17203/Using-jQuery-for-AJAX-in-ASP-NET

    http://forums.asp.net/post/4452433.aspx

    Thursday, September 5, 2013 2:13 AM
  • User440347874 posted

    Hello Jayajgiri

    The other way would be if you know the time zone of your server, you can manipulate that in Javascript itself based on the timezone. Basically the Date Object does the trick and javascript knows only the UDT and current time zone. So, for example if you want the time of EDT, get the UDT - 4 hours which would give the EDT.

    May be the below example would help you as I got this from the internet.

    var t = new Date();
    t =calcTime(0);
    alert(t);
    function calcTime(offset) {

        // create Date object for current location
        d = new Date();

        // convert to msec
        // add local time zone offset
        // get UTC time in msec
        utc = d.getTime() + (d.getTimezoneOffset() * 60000);
        /*alert(utc);
        alert(new Date(utc + (3600000*offset)));
        */
        return new Date(utc + (3600000*offset));

    }

    Thanks

    -Srini

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 5, 2013 6:06 AM