Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Christian Tarrild 23 posts 43 karma points
    Apr 14, 2011 @ 00:42
    Christian Tarrild
    0

    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?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Apr 14, 2011 @ 02:26
    Dirk De Grave
    0

    Can you first .ToString() the output of the .Value property and check if that works?

     

    Cheers,

    /Dirk

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Apr 14, 2011 @ 07:45
    Sebastiaan Janssen
    3

    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:

    @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)
  • Christian Tarrild 23 posts 43 karma points
    Apr 14, 2011 @ 23:51
    Christian Tarrild
    0

    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?)

  • Sebastiaan Janssen 5058 posts 15520 karma points MVP admin hq
    Apr 16, 2011 @ 13:47
    Sebastiaan Janssen
    0

    Oh I though you'd tested that ToDateTime worked. This is just C#, it will work with:

    var birthDate = Convert.ToDateTime(birthDay);
Please Sign in or register to post replies

Write your reply to:

Draft