Reputation: 5
I am very new to ASP.NET and I have a few functions that are starting to appear in multiple times across my project. I decided to grip them up in a single class file in my App_code folder.
In the code below I can call the TxtStrRight and the TxtStrLeft but when I use the GetDatDiff I am receiving the error "Does not exists in the current context".
Can someone please explain what I am doing wrong.
I am calling this method with
<%# GetDiffDate(Convert.ToDateTime(Eval("DateTimeLogged"))) %>
My GlobalUtilities class file in my App-code folder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Generic utilities that can be accessed from any page
/// </summary>
public static class GlobalUtilities
{
//Takes x characters from the right hand side. Use Looks like MyString.TxtStrRight(8)
public static string TxtStrRight(this string value, int length)
{
if (String.IsNullOrEmpty(value)) return string.Empty;
return value.Length <= length ? value : value.Substring(value.Length - length);
}
//Takes x characters from the left hand side. Use Looks like MyString.TxtStrLeft(40)
public static string TxtStrLeft(this string value, int length)
{
if (String.IsNullOrEmpty(value)) return string.Empty;
return value.Length <= length ? value : value.Substring(0, length) + "...";
}
//Get the difference between time and date of NOW and the database value.
public static string GetDiffDate(DateTime dt)
{
TimeSpan ts = dt - DateTime.Now;
if (Math.Abs(ts.TotalHours) < 24 && Math.Abs(ts.TotalHours) >= 1)
{
return string.Format("{0:0} hrs ago", Math.Abs(ts.TotalHours));
}
else if (Math.Abs(ts.TotalHours) < 1)
{
return string.Format("{0:0} mins ago", Math.Abs(ts.TotalMinutes));
}
else
{
return dt.ToString("dd MMM yyyy");
}
}
}
Upvotes: 0
Views: 340
Reputation: 20620
Try this (add the class name):
<%# GlobalUtilities.GetDiffDate(Convert.ToDateTime(Eval("DateTimeLogged"))) %>
Upvotes: 1