ascx transformation date subtraction

Jesse V asked on January 11, 2016 16:51

Hi guys,

I'm looking for a way to calculate how many days have passed from ItemCreatedWhen till DayTimeNow. The ItemCreatedWhen is queried from a custom table. If there are less than 31 days passed, new is added to my CSS-class.

<div class="myclass-<% ((<%# FormatDateTime(DateTime.Now, "yyyy/MM/dd") %> - <%# FormatDateTime(Eval("ItemCreatedWhen"), "yyyy/MM/dd") %>).TotalDays) == &lt31, "new" %>">

Can i make calculations within a transformation or should I do it elsewhere?

Thanks in advance!

Correct Answer

Laura Frese answered on January 11, 2016 17:48

Try something similar to:

<span class="<%# If((DateTime.Now - Convert.ToDateTime(Eval("BlogPostDate"))).TotalDays > 15, "foo","bar") %>">WOW</span>

For you it might look like:

<div class="<%# If((DateTime.Now - Convert.ToDateTime(Eval("ItemCreatedWhen"))).TotalDays < 31, "new","old") %>">

1 votesVote for this answer Unmark Correct answer

Recent Answers


Jesse V answered on January 11, 2016 19:50

Thanks Laura, that worked like a charm! Very helpful.

0 votesVote for this answer Mark as a Correct answer

Laura Frese answered on January 11, 2016 20:16

No problem. Just remember that Eval returns an object, so it has to be converted to the type you want. And the Transformation method "If" takes 3 parameters (value, true result, false result) as described in the documentation

Transformation Methods

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.