0

I'm having a compiler error on line 63:

<div class="dateAdded">Article submitted @article.DateAdded.ToRelativeDateStringUtc()</div>

Compiler Error Message: CS1061: 'System.DateTime' does not contain a definition for 'ToRelativeDateStringUtc' and no extension method 'ToRelativeDateStringUtc' accepting a first argument of type 'System.DateTime' could be found (are you missing a using directive or an assembly reference?)

Here is where my error is occurring. I have a class that contains ToRelativeDateStringUtc() included in my program. Is this a MS class that I have to import? I do not see it listed anywhere in the .net reference list. I'm sure its something simple, does anyone have any ideas?

tereško
  • 58,060
  • 25
  • 98
  • 150
Killdashnine
  • 5
  • 1
  • 4

2 Answers2

0

You should add the required namespaces to your web.config. Depending on whether you are using razor or not, there are different sections.

For razor

<system.web.webPages.razor>
  <pages pageBaseType="System.Web.Mvc.WebViewPage">
    <namespaces>
      <add namespace="System.Web.Mvc" />
      <add namespace="System.Web.Mvc.Ajax" />
      .
       .
      <!-- etc -->
    </namespaces>
  </pages>
</system.web.webPages.razor>

For older versions of ASP.NET

<pages>
  <namespaces>
    <add namespace="System.Web.Mvc"/>
    <add namespace="System.Web.Mvc.Ajax"/>
    <add namespace="System.Web.Mvc.Html"/>
    <add namespace="System.Web.Routing"/>
    <add namespace="System.Web.WebPages"/>
    <add namespace="System.Web.Helpers"/>
    <add namespace="MyCustomHelpers"/>
  </namespaces>
 </pages>

Finally, you could add and @using add the top of your .cshtml.

Pieter
  • 3,339
  • 5
  • 30
  • 63
  • I think you're right here.. I added the namespace to my config. Pointing at my helper class. Now I am looking at this: Line 68: Compiler Error Message: CS1001: Identifier expected – Killdashnine May 08 '14 at 09:52
  • Well now that error is magically gone. My same previous error remains – Killdashnine May 08 '14 at 10:01
  • @Deadhat: you should add something like ``. You added a reference to a file, not a namespace. – Pieter May 08 '14 at 10:45
  • I have added project.HelperClasses. Which points to the file my program needs containing the method it says is missing, however the error remains. – Killdashnine May 08 '14 at 20:41
  • Could you add your web.config? This might help in solving your problem. Also, what kind of page are you creating? – Pieter May 09 '14 at 07:44
0

You did't pass parameter to ToRelativeDateStringUtc method.

this.should look like :

public static string ToRelativeDateStringUtc(this DateTime date)
{
    return GetRelativeDateValue(date, DateTime.UtcNow);
}
private static string GetRelativeDateValue(DateTime date, DateTime comparedTo)
{
    TimeSpan diff = comparedTo.Subtract(date);
    if (diff.TotalDays >= 365)
        return string.Concat("on ", date.ToString("MMMM d, yyyy"));
    if (diff.TotalDays >= 7)
        return string.Concat("on ", date.ToString("MMMM d"));
    else if (diff.TotalDays > 1)
        return string.Format("{0:N0} days ago", diff.TotalDays);
    else if (diff.TotalDays == 1)
        return "yesterday";
    else if (diff.TotalHours >= 2)
        return string.Format("{0:N0} hours ago", diff.TotalHours);
    else if (diff.TotalMinutes >= 60)
        return "more than an hour ago";
    else if (diff.TotalMinutes >= 5)
        return string.Format("{0:N0} minutes ago", diff.TotalMinutes);
    if (diff.TotalMinutes >= 1)
        return "a few minutes ago";
    else
        return "less than a minute ago";
}

More details

http://dotnetslackers.com/articles/aspnet/5-Helpful-DateTime-Extension-Methods.aspx

http://forums.asp.net/t/1880820.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview

  • -1 That's not how extension methods usually work. This code should be called as `myDateTimeInstance.ToRelativeDateStringUtc()`, the compiler will resolve the parameters. – sisve May 09 '14 at 08:35
  • @SimonSvensson I think your very brilliant, can you solve my problem? http://stackoverflow.com/questions/23386532/web-security-in-ie-vs-chrome-firefox-bug –  May 09 '14 at 08:53