You must Sign In to post a response.
  • Category: JavaScript

    How to add min to current date/specific date and compare both dates in javascript?

    Session contain time(min).Either positive or Negative.I want to add that time in current date.If positive,it should get added.If negative,it should get reduced from that.I want to do it from javascript.

    1 :
    var Time = '<%=Session["Time"]%>';
    var d1 = new Date(),
    d2 = new Date(d1);
    d2.setMinutes(d1.getMinutes() + Time);
    alert(d2);
    When I am writing like this (above code) I am getting invalid date.

    var Time = '<%=Session["Time"]%>';
    var d1 = new Date(),
    d2 = new Date(d1);
    d2.setMinutes(d1.getMinutes() + 30);
    alert(d2);
    When I am writing like this (above code) I am getting proper date.Is it a right way to add min to current date.

    2 : I am selecting date from date picker,that gives me date like this '03/30/2016',I have another dropdownlist for selecting hours and min. I want to add hours and time in selected date.Hours is in 24.Time in 60.

    var d = new Date('03/30/2016');
    d.setHours(d.getHours() + 17);
    d.setMinutes(d.getMinutes() + 54);
    alert(d);
    Is it a right way to do?

    3 : I want compare both date d and d2. d should be greater than d2.If not,should show message to user.
  • #765330
    Please watch at my blog:

    https://anjalibansal1234.wordpress.com/2015/03/30/compare-two-dates-using-javascript/

    It will help you ...

  • #765336
    You can use given code snippet to compare current date/specific date in java script
    <scripttype="text/javascript"language="javascript">



    function CompareDate() {

    var dateOne = new Date(2015, 02, 15); //Year, Month, Date

    var dateTwo = new Date(2016, 02, 15); //Year, Month, Date

    if (dateOne > dateTwo) {

    alert("Date One is greater then Date Two.");

    }else {

    alert("Date Two is greater then Date One.");

    }

    }



    CompareDate();

    </script>

    Useful reference
    http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript
    https://blog.udemy.com/javascript-date-comparison/
    http://www.c-sharpcorner.com/UploadFile/8911c4/how-to-compare-two-dates-using-javascript/


Sign In to post your comments