私は、日付に時間を追加し、新しい日付を就業日の周りにまとめたいという状況があります。私はこの新しい日付を決定する機能を盛り上げたが、何かを忘れていないことを確認したい。就業日の周りを包む最も良い方法は何ですか?
追加する時間は「遅延」と呼ばれます。代わりに関数のパラメータになり易いでしょう。
提案を投稿してください。 [VB.NETの警告]
Private Function GetDateRequired() As Date
''// A decimal representation of the current hour
Dim hours As Decimal = Decimal.Parse(Date.Now.Hour) + (Decimal.Parse(Date.Now.Minute)/60.0)
Dim delay As Decimal = 3.0 ''// delay in hours
Dim endOfDay As Decimal = 12.0 + 5.0 ''// end of day, in hours
Dim startOfDay As Decimal = 8.0 ''// start of day, in hours
Dim newHour As Integer
Dim newMinute As Integer
Dim dateRequired As Date = Now
Dim delta As Decimal = hours + delay
''// Wrap around to the next day, if necessary
If delta > endOfDay Then
delta = delta - endOfDay
dateRequired = dateRequired.AddDays(1)
newHour = Integer.Parse(Decimal.Truncate(delta))
newMinute = Integer.Parse(Decimal.Truncate((delta - newHour) * 60))
newHour = startOfDay + newHour
Else
newHour = Integer.Parse(Decimal.Truncate(delta))
newMinute = Integer.Parse(Decimal.Truncate((delta - newHour) * 60))
End If
dateRequired = New Date(dateRequired.Year, dateRequired.Month, dateRequired.Day, newHour, newMinute, 0)
Return dateRequired
End Sub
注:遅延は9時間以上の長さである場合、これはおそらく動作しません。それは決して3から通り抜けるべきではありません。
編集: 目標は、現在の時間に数時間を追加した結果として得られる日時です。これは、提出期日のデフォルト値を決定するために使用されます。私は現在の時間に3時間を追加して、期日の時刻を取得します。しかし、私は当日の午後5時を超える納期を望んでいません。だから今日の終わりに1時間が追加されているので、(今日、午後5時まで)と(明日、午前8時から)時間を分割して、午後3時を追加すると午前19時を与えるようにしました。明日の始まりに時間が追加されます。
はこれが答えではないが、それはupvoteです。 –