Calculate how many days have passed since today

Here is my problem:

I am trying to calculate how many days have passed since today and display it in a shortcut. The problem is that the user changes the time, the result that I get is in the fraction.

Please, help

Here is my code in JS:

 //=============================================== //
   // How many days passed from the current day     //
   // ============================================ //

   function BanDateChanged(sender, args) {
       var banDate = $find("<%= rdtpBan.ClientID %>");
       var TodayDate = new Date();
       var today = new Date();
       var todayFormat = new Date(today.getMonth() + 1 + "/" + today.getDate() + "/" + today.getFullYear());
       var banSelectedDate = banDate.get_selectedDate();
       var countedBannedDays = document.getElementById("<%= lblBanCountDays.ClientID %>");
       var one_day = 1000 * 60 * 60 * 24;
       var countedDays = (Math.ceil(todayFormat - banSelectedDate) / one_day);
       if (countedDays == 0) {
           countedBannedDays.innerHTML = "";
       } else {
           countedBannedDays.innerHTML = countedDays + " Days ago";
       }
   }
+4
source share
3 answers

Javascript dates are just a temporary value equal to milliseconds since 1970-01-01T00: 00: 00Z. To get the number of milliseconds between two dates, simply subtract one from the other:

var date0 = new Date(2015, 0, 1); // 1 Jan 2015
var date1 = new Date(2015, 1, 1); // 1 Feb 2015

var numberOfDays = Math.ceil((date1 - date0) / 8.64e7); // 31

Do not use the Date constructor to parse strings. If you know the values, pass them directly (remember that zero months are indexed).

, , :

var now = new Date();
var daysSinceStartOfMonth = now.getDate() - 1;

1- 0, 1 . - ( , , ), :

// Create a date object for the current instant
var now = new Date();

// Make an exact copy
var startOfMonth = new Date(+now);

// Set the copy date to the start of the month
startOfMonth.setDate(1);

// Get the difference in time value and calculate whole days
var daysSinceStartOfMonth = Math.ceil((now - startOfMonth) / 8.64e7); // 17 on 18 June

Math.ceil - , . , .

+2

- , "" , . . - , setHours() .. - Date banSelectedDate, , :

var cDate = new Date(banSelectedDate.getFullYear(), banSelectedDate.getMonth(), banSelectedDate.getDate());

cDate .

+2

, , , .

, : Moment.js.

, :

var days = moment().diff("2015-06-02", "days");

, , , .. - . . DEMO.

:

:

var ago = moment.duration(moment().diff("2015-06-05")).humanize() + " ago";

, :

  • "15 days ago"
  • "13 hours ago"
  • "5 years ago"

countedBannedDays.innerHTML, . . DEMO.

, banSelectedDate , - DOM , :

    var banDate = $find("<%= rdtpBan.ClientID %>");
    var TodayDate = new Date();
    var today = new Date();
    var todayFormat = new Date(today.getMonth() + 1 + "/" + today.getDate() + "/" + today.getFullYear());
    var banSelectedDate = banDate.get_selectedDate();
    var one_day = 1000 * 60 * 60 * 24;
    var countedDays = (Math.ceil(todayFormat - banSelectedDate) / one_day);

:

    var banDate = $find("<%= rdtpBan.ClientID %>");
    var banSelectedDate = banDate.get_selectedDate();
    var countedDays = moment().diff(banSelectedDate, "days");

:

var countedDays = moment().diff(banSelectedDate, "days");

, , , , , - .

:

. .

With good libraries available to handle such common tasks, we don’t need to write complex code with many potential problems, such as those that you had here. Reusing code not only avoids potential problems, but also makes your own code much more convenient.

-1
source

All Articles