user2329438
user2329438

Reputation: 295

Given a DayOfTheWeek how do i get the date from an existing datetime

I have a jquery calender that a user selects a date from. I also have a list from Mon - Sun that the user can click on that would be the Monday through Sunday for the selected week from the calendar.

I can get the current day of the week using c#'s DayOfWeek method from jquery's selected date. I have the user selected day of the week from the mon-sun list (1-7)

but how do i form a new date based of what day the user clicks on from the mon - sun list?

an example would be user selects Friday the 16th of Jan 2015 from Jquery Calendar.

They then click on the Monday from the Select list.

I should be able to calculate the Monday is the 12th of Jan 2015

cheers

Upvotes: 0

Views: 345

Answers (2)

Rohit Prakash
Rohit Prakash

Reputation: 1972

Here's you go,

        DateTime selectedDate = DateTime.Now; //your selected date

        DayOfWeek s = selectedDate.DayOfWeek; // your selected day of the selecteddate.
        DayOfWeek selectedWeek = DayOfWeek.Friday; //your selected weekday.

        DateTime output = selectedDate.AddDays((int)selectedWeek - (int)s); //output.

Upvotes: 2

Jens
Jens

Reputation: 2702

DateTime dateTime = DateTime.Now;
var date = dateTime.DayOfWeek.ToString();

and with

DateTime dateTime = new DateTime(2015, 1, 12);

you can get the DayOfWeek from a specific Day.

DayOfWeek is an enum. So when you have Monday it may have e.g. integer 1 behind. When you have Friday now you have e.g. integer 5 behind.

I think now you can calculate that Friday is 4 days later than Monday. so it is the DateTime(2015, 1, 12 + 4) for the Friday

Does this help?

Upvotes: 1

Related Questions