c# - Work with DateTime -


i have datatable, 1 of columns datastart, typeof(datetime). try add 1 more column, calculate date. that:

datacolumn deadline = table.columns.add("deadline", typeof(datetime)); foreach (datarow row in table.rows) {   row["deadline"] = datatime.now.adddays(10) - (datetime)(row["datestart"]); } 

but take error when run application:

unable cast object of type 'system.timespan' type 'system.iconvertible'.couldn't store <13.10:32:02.3571743> in deadline column. expected type datetime.

how must fix it?

if add or subtract 2 datetimes result timespan span between both. want subtract 10 days datestart-time, don't you? use datetime.timeofday:

row["deadline"] = datetime.today.adddays(10) + row.field<datetime>("datestart").timeofday; 

the result of datetime + timespan datetime. note i've used datetime.today.adddays(10) instead of datetime.now.adddays(10) truncate time portion.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -