Refer The Below code for your reference how you can pass From Time , To Time and time inteval And get The Diffrent time interval values as string list which you can use ton insert then into Database by making loop.
C#
protected void Page_Load(object sender, EventArgs e)
{
// Pass startTime,toTime and interval value
SplitWithInterval(Convert.ToDateTime("00:00 AM"), Convert.ToDateTime("01:31 AM"), 15);
}
private void SplitWithInterval(DateTime fromTime, DateTime toTime, int interval)
{
string breakTime = string.Empty;
List<string> breakIntervals = new List<string>();
while (fromTime < toTime)
{
DateTime timeInterval1, timeInterval2;
timeInterval1 = fromTime;
fromTime = fromTime.AddMinutes(interval);
timeInterval2 = fromTime;
if (fromTime < toTime)
{
breakIntervals.Add(timeInterval1.ToLongTimeString() + " to " + timeInterval2.ToLongTimeString());
}
else
{
breakIntervals.Add(timeInterval1.ToLongTimeString() + " to " + toTime.ToLongTimeString());
}
}
// you can loop it and save value in database from list breakIntervals
// Currently just displaying result in GridView
GvtimeDiffrence.DataSource = breakIntervals;
GvtimeDiffrence.DataBind();
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
' Pass startTime,toTime and interval value
SplitWithInterval(Convert.ToDateTime("00:00 AM"), Convert.ToDateTime("01:31 AM"), 15)
End Sub
Private Sub SplitWithInterval(fromTime As DateTime, toTime As DateTime, interval As Integer)
Dim breakTime As String = String.Empty
Dim breakIntervals As New List(Of String)()
While fromTime < toTime
Dim timeInterval1 As DateTime, timeInterval2 As DateTime
timeInterval1 = fromTime
fromTime = fromTime.AddMinutes(interval)
timeInterval2 = fromTime
If fromTime < toTime Then
breakIntervals.Add(timeInterval1.ToLongTimeString() + " to " + timeInterval2.ToLongTimeString())
Else
breakIntervals.Add(timeInterval1.ToLongTimeString() + " to " + toTime.ToLongTimeString())
End If
End While
' you can loop it and save value in database from list breakIntervals
' Currently just displaying result in GridView
GvtimeDiffrence.DataSource = breakIntervals
GvtimeDiffrence.DataBind()
End Sub
Output
Item |
12:00:00 AM to 12:15:00 AM |
12:15:00 AM to 12:30:00 AM |
12:30:00 AM to 12:45:00 AM |
12:45:00 AM to 1:00:00 AM |
1:00:00 AM to 1:15:00 AM |
1:15:00 AM to 1:30:00 AM |
1:30:00 AM to 1:31:00 AM |