Showing posts with label Code Examples. Show all posts
Showing posts with label Code Examples. Show all posts

Tuesday, July 22, 2008

Time Ago in Words for .NET C#

I saw the need for a C# version of the time_ago_in_words function in Ruby. I translated the code below into C# from my existing post I published for Ruby.





static string TimeAgo(DateTime time)
{
DateTime startDate = DateTime.Now;
TimeSpan deltaMinutes = startDate.Subtract(time);
string distance = string.Empty;
if (deltaMinutes.TotalMinutes <= (8724 * 60))
{
distance = DistanceOfTimeInWords(deltaMinutes.TotalMinutes);
if (deltaMinutes.Minutes < 0)
{
return distance + " from now";
}
else
{
return distance + " ago";
}
}
else
{
return "on " + time.ToString();
}
}


static string DistanceOfTimeInWords(double minutes)
{
if (minutes < 1)
{
return "less than a minute";
}
else if (minutes < 50)
{
return minutes.ToString() + " minutes";
}
else if (minutes < 90)
{
return "about one hour";
}
else if (minutes < 1080)
{
return Math.Round(new decimal((minutes / 60))).ToString() + " hours";
}
else if (minutes < 1440)
{
return "one day";
}
else if (minutes < 2880)
{
return "about one day";
}
else
{
return Math.Round(new decimal((minutes / 1440))).ToString() + " days";
}
}




You can download the complete source here: http://www.box.net/shared/3fmhllxq8g

Friday, December 07, 2007

Enterprise Library Logging Timestamp format

Just for awareness the Microsoft Enterprise Library Logging Application Block for flat file trace lister allows to specified the time stamp format in the template formatter as
{timestamp(local)} for local time zone time stamp or optionally you can specified your DateTime format such as MM/dd/yyyy HH:mm:ss.

See .NET Standard DateTime String Formats at http://msdn2.microsoft.com/en-us/library/az4se3k1(vs.71).aspx

Thursday, November 15, 2007

String.Format() JavaScript equivalent to .NET

Here is a JavaScript function to format strings on the Client-Side as if you were using in the the .NET String.Format() method in your code-behind.




String.Format = function()
{


// Check for arguments
if( arguments.length == 0 ) return null;
// Get the string which is the first argument
var stringToFormat = arguments[0];
for(var i=1; i < arguments.length;i++)
{
var replaceExpression = new RegExp('\\{' + (i-1) + '\\}','gm');
// Replace argument
stringToFormat = stringToFormat.replace(replaceExpression, arguments[i]);
}

// Return the formated string
return stringToFormat;

}



Alternative you can used the JavaScript prototype framework 'Template' object to do string formatting using variable names instead of argument indexes.

See http://prototypejs.org/api/template for the Template object API.

Tuesday, November 06, 2007

SubSonic - Using the Exist() Method using predicates

Here is a simple example on how to use the Exist() method that hangs of the collection entities generated by SubSonic.




for (int i = 0; i < originalentitycollection.Count; i++)
{
Predicate exist = delegate(Model.Person match)
{
if (match.PersonId == originalentitycollection[i].PersonId)
return true;
else return false;
};


if (!distinctpersoncollection.exists(exist))
{

distinctpersoncollection.add(originalentitycollection[i]);
}
}





You can visit MSDN for more information about the predicate delegates:
http://msdn2.microsoft.com/en-us/library/bfcke1bz.aspx