Is this the right way to display all the records between two dates? It looks like a cowboy job; and is there any proper way to display all the records properly?

This is what I'm using to display all the records:

 var todayDate = new Date();
    var endDate = todayDate.getDate() + '/' + (todayDate.getMonth() + 1) + '/' + (todayDate.getFullYear() + 100);
    var d = new Date();
    var st = d.setDate(todayDate.getDate() - 111365);
    var startDate = d.getDate() + '/' + (d.getMonth() + 1) + '/' + d.getFullYear();
    $('#allrecordsstart').val(startDate);
    $('#allrecordsend').val(endDate);
    $('#TopPlayedInVenueContainer1').jtable('load', {
        StartDate: startDate,
        EndDate: endDate
    });

The proper way I'm displaying for last seven days:

var todayDate = new Date();
    var endDate = todayDate.getDate() + '/' + (todayDate.getMonth() + 1) + '/' + todayDate.getFullYear();
    var d = new Date();
    var st = d.setDate(todayDate.getDate() - 7);
    var startDate = d.getDate() + '/' + (d.getMonth() + 1) + '/' + d.getFullYear();
    $('#lastWeekstart').val(startDate);
    $('#lastWeekend').val(endDate);
    $('#TopPlayedInVenueContainer1').jtable('load', {
        StartDate: startDate,
        EndDate: endDate
    });

Any suggestions would be great. Thanks in advance :)

有帮助吗?

解决方案

JavaScript interpreter actually thinks of a date as the number of milliseconds that have passed since midnight on January 1, 1970. For example, Wednesday, February 1, 2012 is actually 131328083200000 to the JavaScript interpreter (funny eh?).

So, to create a date that’s one week from now, you could do the following:

var now = new Date(); // today
var nowMS = now.getTime(); // get # milliseconds for today
var week = 1000*60*60*24*7; // milliseconds in one week
var oneWeekFromNow = new Date(nowMS + week);

The first line stores the current date and time in a variable named now. Next, the getTime()method extracts the number of milliseconds that have elapsed from January 1, 1970 to today. The third line calculates the total number of milliseconds in a single week (1000 milliseconds * 60 seconds * 60 minutes * 24 hours * 7 days). Finally, the code creates a new date by adding the number of milliseconds in a week to today.

I took this example from Javascript & jQuery: The Missing Manual, by David Sawyer book. Hope it helped!

其他提示

Whenever I need to deal with dates in JavaScript, I always use a 3rd party library. Usually moment.js

var dateFormat = "DD/MM/YYYY";
var startDate = new moment().subtract("days", 7).format(dateFormat);
var endDate = new moment().format(dateFormat);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top