c# - How do I determine if a date lies between current week dates? -
in c#,
how check date in week dates?
eg: 6/02/2014
current weeks: 02/02/2014 - 08/02/2014
so dates in above week....
use check (last parameter optional if want 1 week fromdate, don't need use last parameter):
public static bool dateinside(datetime checkdate, datetime fromdate, datetime? lastdate = null) { datetime todate = lastdate != null ? lastdate.value : fromdate.adddays(6d); return checkdate >= fromdate && checkdate <= todate; }
to call use:
bool isdateinside = dateinside(new datetime(2014, 02, 06), new datetime(2014, 02, 02)); // return true
and search first :) answer here: how check whether c# datetime within range
if want check if dates inside same week, can use this:
public static bool dateinsideoneweek(datetime checkdate, datetime referencedate) { // first day of week actual culture info, dayofweek firstweekday = system.globalization.cultureinfo.currentculture.datetimeformat.firstdayofweek; // or can set want: firstweekday = dayofweek.monday; // calculate first day of week reference date datetime startdateofweek = referencedate; while(startdateofweek.dayofweek != firstweekday) { startdateofweek = startdateofweek.adddays(-1d); } // fist day of week find, find last day of reference week datetime enddateofweek = startdateofweek.adddays(6d); // , check if checkdate inside period return checkdate >= startdateofweek && checkdate <= enddateofweek; }
actual week in culture info start monday, february 3th 2014 (so me week between february 3th , february 9th). if check date reference date (second parameter) today (2014-feb-06) results:
for 2014-feb-02 (sunday before week): false 2014-feb-03 (monday inside week): true 2014-feb-06 (today inside week): true 2014-feb-09 (sunday inside week): true 2014-feb-10 (monday next week): false
you can call method check if 1 date inside same week referentional this:
dateinsideoneweek(new datetime(2014, 02, 02), new datetime(2014, 02, 06));
you can find current week start , end dates code:
datetime startdateofweek = datetime.now.date; // start actual date while(startdateofweek.dayofweek != dayofweek.monday) // set first day of week in country { startdateofweek = startdateofweek.adddays(-1d); } // after while loop first day of actual week datetime enddateofweek = startdateofweek.adddays(6d); // find last week day
is wanted?
Comments
Post a Comment