Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Brail DateTime – StripTime() Extension Method

leave a comment »

Do you agree DateTime is the most annoying thing to deal with? .Net doesn’t have Date type where I can just store date value only. Oh bother why not define your own type? blame me I am lazy coder.

I have and object that hold date value which can be null, when it time to display the value I pass it from Controller -> View

But when the view render date field it render like this: 09/11/2008 12:00:00 AM What ???!!! oh yeah that is DateTime not Date.

What I can do (since I am using Brail View Engine) <% emp.DateOfEntry.ToString(“dd/MM/yyyy”) %> and I should get 09/11/2008 that fine right ;)

But I got problem with <% emp.DateOfResignation.ToString(“dd/MM/yyyy”) %> the app will blow cuz the employee is still work so the DateOfResignation is null.

Here come my very first Boo Extension method Views\CommonScripts\StripTime.brail:

import System
static def StripTime(val as duck) as string:
    return string.Empty if val is null
    return val.ToString("dd/MM/yyyy") if val.GetType().Name == "DateTime"
  except e as System.MissingMethodException:
    return string.Empty

This code block is inspired from Ayende’s post.

And this is how I use it <% emp.DateOfResignation.StripTime() %> it will return empty string is null detected otherwise it will strip out 12:00:00 AM, isn’t that cool?


Written by Chorn Sokun

June 9, 2008 at 10:22 am

Posted in MonoRail, Tips & Tricks

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: