Trim Prototype

Some legacy browsers such as IE8 do not natively support the trim() function in JavaScript. Though newer browsers do one cannot expect the majority of web site visitors to use them, yet. Because of this I found out how to make a trim function in JavaScript that seems native, in use anyways.

// Set it
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
};

// Use it
var name = "     Cesar      ";
if (name.trim().length == 0) {
    alert('Invalid name.');
} else {
    alert('Valid name.');
}

Function for Getting the Local Time (Time Zones)

Today I needed to convert the current server time to my local time. I made a quick little function that helps take most of the thinking out of it. At the moment my function just works with just two time zones but you can add more from PHP’s supported list.

function getLocalTime($index) {
$zones = array();
$zones[] = new DateTimeZone('America/New_York');
$zones[] = new DateTimeZone('America/Los_Angeles');

$time = new DateTime(date("Y-m-d H:i:s", time()));
$time->setTimezone($zones[$index]);
return $time->format('Y-m-d H:i:s');
}
// Try it
echo getLocalTime(0); //NY

Resource: http://php.net/manual/en/timezones.php

Looping Through a Directory And Its Files

A simple function to sift through all the files in a directory using .net

Private Function getEntries(ByVal dir As String)
Dim strEntry As String
Dim dirInfo As New DirectoryInfo(dir)
Dim strEntries As String() = Directory.GetFileSystemEntries(dirInfo.FullName)
     For Each strEntry In strEntries
          If (Directory.Exists(strEntry)) Then
               getEntries(strEntry)
          Else
               Response.Write(strEntry.ToString())
            End If
     Next
Return 0
End Function