1

I need to get Date only given I have string day name, So far I have tried this

var now = DateTime.Now.Date;

var currentDay = now.DayOfWeek;
int days = (int)currentDay+1;
dt1 = now.AddDays(-days);
dt1 = dt1.AddHours(hour);

example "Tuesday". Get todays date bc today is tuesday

But no luck

user786
  • 3,902
  • 4
  • 40
  • 72
  • 1
    Some more info would be helpfull. What is your expected result for 01.01.2019 for example. – Mighty Badaboom Jan 15 '19 at 11:07
  • 1
    Can you give a sample input and expected output? – Jasmeet Jan 15 '19 at 11:09
  • @Noname updated question – user786 Jan 15 '19 at 11:14
  • 1
    @Alex the hard part is *parsing* a localized day name into a `DayOfWeek`. Once you have that value you can calculate the difference in days between today and the target week day, eg `now.AddDays(thatDay - now.DayOfWeek)`. That of course assumes that weeks start on a Sunday and are calculated in a specific way – Panagiotis Kanavos Jan 15 '19 at 11:25
  • How do *you* define current week? Different people disagree on which day transition marks the boundary between weeks (E.g. Saturday -> Sunday or Sunday -> Monday being two conventions I'm seen) – Damien_The_Unbeliever Jan 15 '19 at 11:25
  • @Damien_The_Unbeliever our week starts at sunday and end on Saturday – user786 Jan 15 '19 at 11:27

1 Answers1

2

base on

Mohamed Elrashid
  • 8,125
  • 6
  • 31
  • 46