Time Scheduling in Asp.Net using TimeSpan

I’m working on an asp.net project where doctor’s have start time, end time and duration(All are saved as Time in mssql). In my models folder I have a time_slots.cs file which is supposed to make a list of slots.

I short this is what I want:

john doe : start time -> 08:00:00 end time -> 12:00:00 duration: 01:00:00

Result: 08:00:00, 09:00:00, 10:00:00 , 11:00:00 ,12:00:00

This is code I’m using:

namespace das.Models
{

    public class time_slot
    {
        public List<TimeSpan> TimeSlots(TimeSpan start, TimeSpan end, TimeSpan duration)
        {
            List<TimeSpan> slots = new List<TimeSpan>();

            TimeSpan ToAdd = TimeSpan.Zero;

            while(ToAdd < end)
            {
                ToAdd = start + end;
                slots.Add(ToAdd);
            }
            return slots;
        }
    }
}

I want to show this slots as dropdown in my dashboard after the user logs in to make an appointment. How am I supposed to do that?
And is there any better and easier way to do this is asp.net?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I would use a custom class for the output. Something like this:

public class slot
{
    public TimeSpan StartTime { get; set; }
    public TimeSpan Duration { get; set; }
    public string DisplayString
    {
        get
        {
            return StartTime.ToString(@"hh:mm") + " - " + (StartTime + Duration).ToString(@"hh:mm");
        }
    }
    public slot(TimeSpan StartTime, TimeSpan Duration)
    {
        this.StartTime = StartTime;
        this.Duration = Duration;
    }
}

Then your TimeSlots code would look like this:

public List<slot> TimeSlots(TimeSpan start, TimeSpan end, TimeSpan duration)
{
    List<slot> slots = new List<slot>();
    while (start < end)
    {
        slots.Add(new slot(start, duration));
        start = start + duration;
    }
    return slots;
}

Now you have a list of “slot” objects, with a DisplayString property that you can format any way you want. And you could add a “Value” property if you need to, or just use the slot object itself.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x