2017-05-30 13 views
1

2つの日付間の月と日を計算したいと思います。次に、開始日と終了日の間にユーザーが支払うコストを計算する必要があります。2つの日付間の正確な月と日を見つける方法 - angularjs

コスト= 10000.00; 周期性= 3(四半期);

いいえ月月日=(開始日 - 終了日);

お支払いが必要です=((月および日/周期性なし)*コスト);

私は2つの日付の時間差を見つけようとしますが、それは私のためにはうまくいきません。誰でも助けてくれますか?

var date = "2017-06-01"; 
      console.log("date: "+date); 
      var currentDate = $filter('date')(date, "yyyy-MM-dd HH:mm:ss"); 

      $scope.userdob = "2017-08-31"; 
      var dobdate = $filter('date')($scope.userdob, "yyyy-MM-dd HH:mm:ss"); 

      console.log("dob: "+dobdate); 

      /* differentiate Date */    
      var date1 = $filter('date')($scope.userdob, "yyyy-MM-dd"); 
      var date2 = $filter('date')(date, "yyyy-MM-dd"); 

      date1 = date1.split('-'); 
      date2 = date2.split('-'); 

      // Now we convert the array to a Date object, which has several helpful methods 
      date1 = new Date(date1[0], date1[1], date1[2]); 
      date2 = new Date(date2[0], date2[1], date2[2]); 

      if (date1 < date2) 
      { 
       var start_date = date1; 
       var end_date = date2; 
       var inverse = false; 

       end_date = new Date(end_date); //If you don't do this, the original date passed will be changed. Dates are mutable objects. 
       end_date.setDate(end_date.getDate() + 1); 

       // Calculate the differences between the start and end dates 
       var yearsDifference = end_date.getFullYear() - start_date.getFullYear(); 

       var monthsDifference = end_date.getMonth() - start_date.getMonth(); 

       var daysDifference = end_date.getDate() - start_date.getDate(); 

       var d1 = new Date(end_date.getFullYear(), end_date.getMonth(), 0); 
       var noOfDays = d1.getDate(); 

       $scope.noOfMonths = (inverse ? -1 : 1) * (yearsDifference * 12 + monthsDifference + daysDifference/noOfDays); // Add fractional month 
+0

月は何を意味しますか? 30日間全体? –

+0

日付を使って遊ぶのは苦痛です。私のアドバイスは[momentjs](https://momentjs.com/)のようなものをチェックすることです。これはあなたの質問に答えないのですが、このライブラリは私に1トンの時間を節約しました:)それは実際のカレンダー月と閏年などを分解する –

答えて

0

日付の操作は悪くもないことが知られています。うるう年、うるう秒、その他のさまざまな時間調整が行われます。大きくて、どこにも至りません。チェックアウトFalsehoods programmers believe about timeMore falsehoods programmers believe about timeです。日時を重視しない何かをするときには、momentのような良いライブラリを使うのが一番の賭けです。それはもちろん、すべての権利を得ることはありませんが、ほとんどの目的のために十分な仕事をするでしょう。

それだけでなく、あなたのための構文解析を扱うことができるので、前のコードは次のようなものになることができます:

const date1 = moment($filter('date')($scope.userdob, "yyyy-MM-dd")); 
const date2 = moment($filter('date')(date, "yyyy-MM-dd")); 

const diffInMonths = date1.diff(date1, 'months'); 
const diffInDays = date1.diff(date2, 'days'); 
+0

あなたの提案をありがとう。 – sudarsan

1
function findDateDifferenceInDays(sdate,edate) { 
    var oneDay = 24*60*60*1000; 
    var firstDate = new Date(sdate); 
    var secondDate = new Date(edate); 

    return Math.round(Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay))); 
} 
+0

ありがとうございます。 – sudarsan

0

これは私の問題を解決します。

$scope.startDate = new Date(startDate); 

       $scope.endDate = new Date(endDate); 

       console.log($scope.startDate); 
       console.log($scope.endDate); 

       /* differentiate Date */    
       var date1 = $filter('date')($scope.startDate, "yyyy-MM-dd"); 
       var date2 = $filter('date')($scope.endDate, "yyyy-MM-dd"); 

       console.log("date1 : "+date1); 
       console.log("date2 : "+date2); 

       date1 = date1.split('-'); 
       date2 = date2.split('-'); 

       // Now we convert the array to a Date object, which has several helpful methods 
       /*date1 = new Date(date1[0], date1[1], date1[2]); 
       date2 = new Date(date2[0], date2[1], date2[2]);*/ 

       console.log("date1 : "+date1); 
       console.log("date2 : "+date2); 
       if ($scope.startDate < $scope.endDate) 
       { 
        var start_date = date1; 
        var end_date = date2; 
        var inverse = false; 
        console.log("end_date : "+end_date); 

        end_date = new Date(end_date); //If you don't do this, the original date passed will be changed. Dates are mutable objects. 
        console.log("end_date : "+end_date); end_date.setDate(end_date.getDate() + 1); 

        start_date = new Date(start_date); 


        // Calculate the differences between the start and end dates 
        var yearsDifference = end_date.getFullYear() - start_date.getFullYear(); 

        var monthsDifference = end_date.getMonth() - start_date.getMonth(); 

        var daysDifference = end_date.getDate() - start_date.getDate(); 

        var d1 = new Date(end_date.getFullYear(), end_date.getMonth(), 0); 
        var noOfDays = d1.getDate(); 

        if(noOfDays >= 30) { 
         noOfDays = 30; 
        } 

        var days = daysDifference/noOfDays; 

        console.log(days); 

        $scope.noOfMonths = (inverse ? -1 : 1) * (yearsDifference * 12 + monthsDifference + days); // Add fractional month 

        console.log($scope.noOfMonths); 
関連する問題