c# - Returning a date from a class -
what want have user enter date , have class returns last day of month. so, i've put in class module:
public static class stringextensions { public static datetime lastdayofmonth(datetime mydate) { datetime today = mydate; datetime eom = new datetime(today.year,today.month, datetime.daysinmonth(today.year, today.month)); return eom; } }
in code-behind, have this:
datetime ldom = stringextensions.lastdayofmonth(txtcit.text);
i've tried hard-coding date like:
datetime ldom = stringextensions.lastdayofmonth('1/12/2016');
i'm getting these errors:
error 14 best overloaded method match 'clientdpl.stringextensions.lastdayofmonth(system.datetime)' has invalid arguments
and
error 15 argument 1: cannot convert 'string' 'system.datetime'
can see i'm doing wrong?
as @mason pointed out in comment, better way use datetime extension method similar to:
public static class datetimeextensions { public static datetime lastdayofmonth(this datetime date) { datetime eom = new datetime( date.year,date.month, datetime.daysinmonth( date.year, date.month ) ); return eom; } }
can used like:
console.writeline(datetime.now.lastdayofmonth());
see in action at:
Comments
Post a Comment