Kate
Kate

Reputation: 945

How can I set datetimepicker to Monday with customized time?

I am new to c# and I am using windows forms.

I am trying to set the datetimepicker to Monday (first day of the week) with a time of 10:00:00 when button is clicked. I mean regardless of which day it is I want when I click button the datetimepicker goes to Monday of the same week with time of 10:00:00

For example the datetime now is 22/3/2016 11:15:00 I want to set it to the date of Monday of the same week (which is yesterday) at 10am = 21/3/2016 10:00:00

I tried this code but it goes to 23/3/2016 10:00:00 which is tomorrow.

private void button1_Click(object sender, EventArgs e)
{
    dateTimePicker1.Value = 
      DateTime.Today.AddDays((int)DateTime.Today.DayOfWeek - 1).AddHours(10);
}

Anyone knows hows to do that? please help, Thank you

Upvotes: 1

Views: 2750

Answers (3)

Jcl
Jcl

Reputation: 28272

This should do I think:

dateTimePicker1.Value = DateTime
                          .Today
                          .AddDays(-(DateTime.Today.DayOfWeek - DayOfWeek.Monday))
                          .AddHours(10);

I've made a fiddle

Note that if this is called on Sunday, it'll go to the next Monday. This is consistent with the CultureInfo where in most, Sunday is considered the first day of the week.

If you want to account for it using the current culture (if that culture says that first day is monday), you can do:

var now = DateTime.Today;
var firstDayOfWeek = Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
if (now.DayOfWeek == DayOfWeek.Sunday && firstDayOfWeek != DayOfWeek.Sunday)
  now = now.AddDays(-1);
dateTimePicker1.Value = now.AddDays(-(now.DayOfWeek - DayOfWeek.Monday))
                           .AddHours(10);

I can't make a fiddle out of this since dotNetFiddle won't allow to change the culture, and the default one has the first day of week as Sunday, but I've tested it locally and it works.

This all above is if your requirements are what you specified in the question:

I want to set the monday of the week I'm in

If instead you want (which is more likely)

I want to set today at 10:00am is today is monday, or otherwise the previous monday at 10:00"

Which doesn't depend on whether your week is defined as starting on sunday, morning, or anything), then you can do the simpler (although less performant):

var now = DateTime.Today;
while(now.DayOfWeek != DayOfWeek.Monday)
  now = now.AddDays(-1);
dateTimePicker1.Value = now.AddHours(10);

Upvotes: 5

Pikoh
Pikoh

Reputation: 7703

Maybe this is what you want:

int origin = (int)(DateTime.Today.DayOfWeek) - 1;
if (origin < 0) origin = 6;
DateTime.Today.AddDays(-origin).AddHours(10);

Upvotes: 3

Sabrina_cs
Sabrina_cs

Reputation: 421

My solution is

private static DateTime FirstDay(DateTime dateFrom)
{
   dateFrom = dateFrom.AddDays(((int)dateFrom.DayOfWeek) * -1);
   return new DateTime(dateFrom.Year, dateFrom.Month, dateFrom.Day, 10, 0, 0);
}

Upvotes: 1

Related Questions