亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

計算可配置小時之間的分鐘數

計算可配置小時之間的分鐘數

C#
躍然一笑 2022-06-18 16:32:03
我有一個Journey具有 aDateTime JourneyStartTime { get; set; }和 a的對象DateTime JourneyEndTime { get; set; }。我想計算旅程在晚上 10 點到早上 6 點之間花費的總分鐘數(注意這個時間超過了午夜)。我嘗試使用 TimeSpans 來指示晚上 10 點和早上 6 點,但我不確定這是否是最好的數據類型。此邏輯的域是基于保險的。X 公司想要對在 X - Y 小時之間駕駛的司機進行評分。這些時間應該是可配置的。這是一個場景:同一天下午 5 點到 6 點之間進行旅程。公司 X Inurance 對晚上 10 點到早上 6 點之間的旅程感興趣。該旅程在 X 公司感興趣的時間段內花費了多少分鐘?上面的答案是:0,但我的代碼給了 60 分鐘(這里是dotnetFiddle)。這是代碼。代碼using System;public class Program{    public static void Main()    {        var shortSameDayJourney = new Journey {            JourneyId = 1,            // start of journey - 5pm - start            JourneyStartTime = new DateTime(2018, 12, 17, 17, 00, 00, DateTimeKind.Utc),            // end of journey - 6pm - end            JourneyEndTime = new DateTime(2018, 12, 17, 18, 00, 00, DateTimeKind.Utc)        };        var scoreTimePeriod = new InsurerTimePeriodScoreSetting {            // start of insurer's time period.            StartOfTimePeriod = DateTime.Now + TimeSpan.FromHours(22),            // end of insurer's time period.            EndOfTimePeriod = DateTime.Now + TimeSpan.FromHours(30)             };        var minutesInTimePeriod = getNumberOfMinutesThatJourneyWasInTimePeriod(shortSameDayJourney, scoreTimePeriod);        Console.WriteLine("Number of minutes the journey was within the time period the insurer had sepcified:");               Console.WriteLine(minutesInTimePeriod + " minutes");    }    public static double getNumberOfMinutesThatJourneyWasInTimePeriod(        Journey journey,        InsurerTimePeriodScoreSetting insurerTimePeriod) {        var JourneyStart = journey.JourneyStartTime;        var JourneyEnd = journey.JourneyEndTime;        var timeSpan = insurerTimePeriod.EndOfTimePeriod - insurerTimePeriod.StartOfTimePeriod;        var startDif = (JourneyStart - insurerTimePeriod.StartOfTimePeriod);        var endDif =  (insurerTimePeriod.EndOfTimePeriod - JourneyEnd);        var time = timeSpan - startDif - endDif;        return time.TotalMinutes;    }}
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

時間跨度只給你2之間的原始時間,DateTime's 所以我不得不改變你的Journey初始化,這樣我就可以在同一天進行比較


   var shortSameDayJourney = new Journey

   {

       JourneyId = 1,

       // start of journey - 5pm - start

       JourneyStartTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 17, 00, 00, DateTimeKind.Utc),

       // end of journey - 6pm - end

       JourneyEndTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 18, 00, 00, DateTimeKind.Utc)

    };  

同樣的 InsurerTimePeriodScoreSetting


 var scoreTimePeriod = new InsurerTimePeriodScoreSetting

 {

     // start of insurer's time period. 18/12 22:00

      StartOfTimePeriod = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 22, 0, 0, DateTimeKind.Utc),   // DateTime.Now + TimeSpan.FromHours(22),

     // end of insurer's time period. 19/12 6:00

     EndOfTimePeriod = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day + 1, 6, 0, 0, DateTimeKind.Utc)  // DateTime.Now + TimeSpan.FromHours(30)

 };

現在您需要做的只是一個簡單的檢查 - 旅程時間是否介于InsurerTimePeriodScoreSetting


if (JourneyStart >= insurerTimePeriod.StartOfTimePeriod && JourneyEnd <= insurerTimePeriod.EndOfTimePeriod)

{

// your same calculation here

}

else

   return 0;


查看完整回答
反對 回復 2022-06-18
  • 1 回答
  • 0 關注
  • 111 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號