d:\web\localuser\tarrild.dk\test\macroScripts\634383384311851375_TestScript.cshtml(14): error CS1502: The best overloaded method match for 'System.DateTime.Subtract(System.DateTime)' has some invalid arguments
I have tried casting the member property as date, by using .ToDateTime and Convert.ToDateTime, but nothing worked. How is this done?
To calculate someones age correctly try something like this:
@{
var birthDate = memb.getProperty("BirthDate").Value.ToDateTime;
// cache the current time
DateTime now = DateTime.Today; // today is fine, don't need the timestamp from now
// get the difference in years
int years = now.Year - birthDate.Year;
// subtract another year if we're before the
// birth day in the current year
if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
{
--years;
}
}
<span>Age: @years</span>
Remember that you can make this reusable by creating a function out of this and putting it in a @functions block:
@functions{
public static int GetAgeFromBirthday(string birthDay)
{
var birthDate = birthDay.ToDateTime;
DateTime now = DateTime.Today; // today is fine, don't need the timestamp from now
int years = now.Year - birthDate.Year;
if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
--years;
return years;
}
}
<span>Age: @GetAgeFromBirthday(memb.getProperty("BirthDate").Value)
Thanks for your replies. There is no difference whether I use ToString() or not.
I have tried Sebastiaan solution but got the following error
d:\web\localuser\tarrild.dk\test\macroScripts\634384211987755480_TestScript.cshtml(7): error CS1061: 'string' does not contain a definition for 'ToDateTime' and no extension method 'ToDateTime' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
Working with member date property
Hi
I am running 4.7 and trying to calculate the date of my members using:
@DateTime.Nov.Subtract(memb.getProperty("BirthDate").Value)
But get the following error upon saving:
Error occured
d:\web\localuser\tarrild.dk\test\macroScripts\634383384311851375_TestScript.cshtml(14): error CS1502: The best overloaded method match for 'System.DateTime.Subtract(System.DateTime)' has some invalid arguments
I have tried casting the member property as date, by using .ToDateTime and Convert.ToDateTime, but nothing worked. How is this done?
Can you first .ToString() the output of the .Value property and check if that works?
Cheers,
/Dirk
Subtract takes a TimeSpan and not a DateTime.
To calculate someones age correctly try something like this:
@{ var birthDate = memb.getProperty("BirthDate").Value.ToDateTime; // cache the current time DateTime now = DateTime.Today; // today is fine, don't need the timestamp from now // get the difference in years int years = now.Year - birthDate.Year; // subtract another year if we're before the // birth day in the current year if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day)) { --years; } } <span>Age: @years</span>
Remember that you can make this reusable by creating a function out of this and putting it in a @functions block:
Hi
Thanks for your replies. There is no difference whether I use ToString() or not.
I have tried Sebastiaan solution but got the following error
d:\web\localuser\tarrild.dk\test\macroScripts\634384211987755480_TestScript.cshtml(7): error CS1061: 'string' does not contain a definition for 'ToDateTime' and no extension method 'ToDateTime' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)
Oh I though you'd tested that ToDateTime worked. This is just C#, it will work with:
var birthDate = Convert.ToDateTime(birthDay);
is working on a reply...