Time Ago Template for date fields in .Net MVC

I wanted to display dates for certain models in my views like “15 minutes ago.” I tried at first extending the HTML Helper, but I found it easier to just create a template.

@model DateTime
@{ 
    TimeSpan timeSince = DateTime.Now.Subtract(Model); 
}
@if (timeSince.TotalMilliseconds < 1) 
  { <text>not yet</text> }
else if (timeSince.TotalMinutes < 1) 
  { <text>just now</text> }
else if (timeSince.TotalMinutes < 2) 
  { <text>1 minute ago</text> }
else if (timeSince.TotalMinutes < 60) 
  { <text>@string.Format("{0} minutes ago", timeSince.Minutes)</text> }
else if (timeSince.TotalMinutes < 120) 
  { <text>1 hour ago</text> }
else if (timeSince.TotalHours < 24) 
  { <text>@string.Format("{0} hours ago", timeSince.Hours)</text> }
else if (timeSince.TotalDays < 2) 
  { <text>yesterday</text> }
else if (timeSince.TotalDays < 7) 
  { <text>@string.Format("{0} days ago", timeSince.Days)</text> }
else if (timeSince.TotalDays < 14)
   { <text>"last week</text> }
else if (timeSince.TotalDays < 21)
   { <text>2 weeks ago</text> }
else if (timeSince.TotalDays < 28)
   { <text>3 weeks ago</text> }
else if (timeSince.TotalDays < 60)
   { <text>last month</text> }
else if (timeSince.TotalDays < 365)
   { <text>@string.Format("{0} months ago", Math.Round(timeSince.TotalDays / 30))</text> }
else if (timeSince.TotalDays < 730)
   { <text>last year</text> }
else
   { <text>@string.Format("{0} years ago", Math.Round(timeSince.TotalDays / 365))</text> }

Then I specified the UIHint for the field.

        [UIHint("RelativeDateTime")]
        public DateTime? DateCreated { get; set; }

Then my dates are formated as expected in my views.

  @Html.DisplayFor(modelItem => item.Brand.Name)

Screen shot

Converting BB Press 1.x attachments to BB Press 2.x

If you were using BB Press 1.x with the BB Attachments plugin it can be a bit of a challenge to re-attach them to BB Press 2.0 in WordPress using the GD bbPress Attachments plugin.

Here are the steps I took to accomplish this migration.

1. Run the BB Press migration process making sure that both the original BB Press 1.x tables and WordPress tables are in the same database.
2. Download the original attachments from the bb-attachments folder.
3. Switch WordPress to not use year/month upload folders under Settings – Media.
4. Upload the attachments to the wp-content/uploads folder.
5. Install the GD bbPress Attachments plugin if you haven’t already.
6. Run the following MySQL script

INSERT INTO wp_posts
(post_author, post_date, post_date_gmt, post_content, post_title, post_status, 
comment_status, ping_status, post_name, post_modified, post_modified_gmt, 
post_parent, guid, menu_order, post_type, post_mime_type, comment_count)
SELECT replies.post_author, replies.post_date, replies.post_date_gmt, 
SUBSTRING_INDEX(filename, '.', 1), SUBSTRING_INDEX(filename, '.', 1), 'inherit', 
'open', 'open', CONCAT(bb_attachments.id,'.',SUBSTRING_INDEX(filename, '.', 1)), 
replies.post_modified, replies.post_modified_gmt, replies.ID, 
CONCAT('http://nonamers.org/wp-content/uploads/',bb_attachments.id,'.',filename) as guid, 
0, 'attachment', mime, 0 FROM `bb_attachments` 
JOIN bb_posts ON bb_posts.post_id = bb_attachments.post_id 
JOIN bb_topics ON bb_topics.topic_id = bb_posts.topic_id AND bb_topics.topic_status = 0 
LEFT JOIN wp_posts as topics ON topics.post_status = 'publish' 
AND topics.post_type = 'topic' AND topics.post_title = bb_topics.topic_title
LEFT JOIN wp_posts as replies ON replies.post_parent = topics.ID 
AND replies.post_status = 'publish' AND replies.post_type = 'reply' 
AND replies.post_author = bb_posts.poster_id 
AND replies.post_date_gmt = bb_posts.post_time
WHERE bb_attachments.status = 0 AND replies.ID IS NOT NULL

Tweak any attachments if necessary.

Fix for sorting in Job Manager WordPress Plugin

If you want to sort jobs alphabetically in Job Manager and use sticky sort for highlighted items it will mess up the order of all non-highlighted opportunities. This was my quick workaround. Hopefully it will help someone.

In frontend-jobs.php

<?php
function jobman_sort_highlighted_jobs( $a, $b ) {
	$ahighlighted = get_post_meta( $a->ID, 'highlighted', true );
	$bhighlighted = get_post_meta( $b->ID, 'highlighted', true );
	if( $ahighlighted == $bhighlighted )
		return strnatcmp($a->post_title, $b->post_title);
	if( 1 == $ahighlighted )
		return -1;
	if( 1 == $bhighlighted )
		return 1;
	return strnatcmp($a->post_title, $b->post_title);
}
?>

NextGen Gallery – auto-detect gallery

By default the NextGen gallery for wordpress requires you select the gallery to display when you use the shortcode [nggallery id=1]. However, you may note in the gallery itself you’ve already specified the page where the gallery is to be displayed (“Page Link to:”). So why select the gallery in the shortcode when the database already has this information. This short bit of code will allow you to manage the association in the gallery itself and no longer have to use the id parameter.
Read more

Optimizing Web Application Databases

User feedback regarding a CMS web application was that the certain areas of the site were running slow. There could be many reasons for this like the usual suspects of a slow internet connection, poorly optimized images and stylesheets, and large page sizes. In this case there was a large amount of data coming from a couple tables in the database and were combined together to render the page. This was slowing the page load time down as the MySQL database was combining this data. This is where a simple optimization technique called indexing can help. Read more

Do Good and be That Guy

Self promotion is getting other people to WANT to do your promotion for you. Peter Shankman personifies and lives this notion. He is well known for HARO, which has changed how media, individuals, and companies connect. His presentation at SXSW was powerful and mind changing, enough so that I’m promoting right now. It is about creating win-wins. These wins can also be good for the environment as Val Casey pointed out in her Keynote speech. So how do you do good and be that guy or that company that everyone talks about and make money? Read more

QT Listview Delegates Paint

It took some trial and error to create an appropriate paint method for inactive items in an item delegate for QT’s list view. Here are some pointers.