最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trying to get number of weeks between two date in Jquery - Stack Overflow

programmeradmin1浏览0评论

I have a hidden form like this

HTML

<h2> First Date</h2>
<input type="hidden" class="datepicker" id="startDate" value="29-10-2015" />

<h2> Second Date</h2>
<input type="hidden" class="datepicker" id="endDate" value="29-12-2015" />

<button type="button" id="test">Click It</button>
<p>Total Weeks</p>

JAVAScript

$("input").datepicker();

    $('#test').click(function () {
        var end = $('#endDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();
        var start = $('#startDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();

        var $weekDiff = Math.floor((end - start + 1) / (1000 * 60 * 60 * 24) / 7);

        alert('Total Weeks :'+$weekDiff);
    });

When I am printing the Dates in hidden input field from Database and it is in the format that I have pasted here

JSFiddle

I have a hidden form like this

HTML

<h2> First Date</h2>
<input type="hidden" class="datepicker" id="startDate" value="29-10-2015" />

<h2> Second Date</h2>
<input type="hidden" class="datepicker" id="endDate" value="29-12-2015" />

<button type="button" id="test">Click It</button>
<p>Total Weeks</p>

JAVAScript

$("input").datepicker();

    $('#test').click(function () {
        var end = $('#endDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();
        var start = $('#startDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();

        var $weekDiff = Math.floor((end - start + 1) / (1000 * 60 * 60 * 24) / 7);

        alert('Total Weeks :'+$weekDiff);
    });

When I am printing the Dates in hidden input field from Database and it is in the format that I have pasted here

JSFiddle

Share Improve this question edited Oct 13, 2015 at 12:38 Vikram Anand Bhushan asked Oct 13, 2015 at 11:02 Vikram Anand BhushanVikram Anand Bhushan 4,89615 gold badges74 silver badges133 bronze badges 2
  • So what is the issue? – hindmost Commented Oct 13, 2015 at 11:08
  • 1 How to get the date from jQuery UI datepicker – Andreas Commented Oct 13, 2015 at 11:09
Add a ment  | 

5 Answers 5

Reset to default 4

Convert to ms and divide by ms per week.

// date1 = $('#startDate').datepicker('getDate');
// date2 = $('#endDate').datepicker('getDate');

function nWeeks(date1, date2) {
    var WEEK = 1000 * 60 * 60 * 24 * 7;

    var date1ms = date1.getTime();
    var date2ms = date2.getTime();

    var diff = Math.abs(date2ms - date1ms);

    return Math.floor(diff / WEEK);
}

Here is a reference about JS date objects.

Here is a simple way to do it:

$("input").datepicker()

$('#test').on('click', () => {
  const end = $('#endDate').datepicker('getDate'),
    start = $('#startDate').datepicker('getDate'),
    $weekDiff = Math.floor((end - start + 1) / (1000 * 60 * 60 * 24) / 7);

  $('#total').html($weekDiff)
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="https://code.jquery./ui/1.10.3/jquery-ui.min.js"></script>
<h2> First Date</h2>
<input type="text" class="datepicker" id="startDate" value="12/10/2015" />
<h2> Second Date</h2>
<input type="text" class="datepicker" id="endDate" value="12/29/2015" />
<button type="button" id="test">Click It</button>
<p>Total Weeks</p><span id="total"></span>

You are trying to convert JS strings to integers in mathematical expressions, which will result in NaN.

So you need to first do Date.parse( string ).

With your fiddle it would be:

$("input").datepicker();

$('#test').click(function () {
    var end = $('#endDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();
    var start = $('#startDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();

    end = Date.parse( end );
    start = Date.parse( start );

    alert( [end, start] );

    var $weekDiff = Math.floor((end - start + 1) / (1000 * 60 * 60 * 24) / 7);

    alert('Total Weeks :'+$weekDiff);
});

With es6, and some shortening:

$("input").datepicker();

$('#test').click( () => {
    const [end, start] = ["#endDate", "startDate"]
        .map( e => Date.parse( e.datepicker({ dateFormat: 'dd-mm-yyyy'}).val() ) );

    const diff = Math.floor((end - start + 1) / (1000 * 60 * 60 * 24) / 7);
    console.log('Total Weeks :' + diff);
});

It works for me.

$('#test').click(function () {
    var end = $('#endDate').datepicker({dateFormat: 'dd-mm-yyyy'}).val();
    var start = $('#startDate').datepicker({dateFormat: 'dd-mm-yyyy'}).val();
    var aDate1 = end.split('-');
    var aDate2 = start.split('-');
    end = Date.UTC(aDate1[2], aDate1[1] - 1, aDate1[0]);
    start = Date.UTC(aDate2[2], aDate2[1] - 1, aDate2[0]);
    alert(Math.floor((end - start) / (1000 * 60 * 60 * 24)));
});

You can try something like this.

$("input").datepicker();

$('#test').click(function () {
    var end = $('#endDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();
    var start = $('#startDate').datepicker({ dateFormat: 'dd-mm-yyyy'}).val();

    var $weekDiff = Math.floor((end - start + 1) / (1000 * 60 * 60 * 24) / 7);

    alert(daydiff(parseDate(start), parseDate(end)));
});



function parseDate(str) {
    var mdy = str.split('/')
    return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function daydiff(first, second) {
    return Math.round((second-first)/(1000*60*60*24) / 7);
}

Updated your jsfiddle

发布评论

评论列表(0)

  1. 暂无评论