.Net Date Format Strings

I keep forgetting these and not being able to find them in the MSDN library without going through pages of locailzation jargon, so I’ve decided to reprint this list that I found on vb-helper.com.

  • d – Short date
  • %d – Day number
  • M?d – Month and day number
  • dd – Day number, two digits
  • ddd – Abbreviated day name
  • dddd – Full day name
  • f – Full (long date, short time)
  • %f – Fractions of second, one digit
  • s^f – Seconds and fractions of second, one digit
  • ff – Fractions of second, two digits
  • fff – Fractions of second, three digits
  • ffff – Fractions of second, four digits
  • fffff – Fractions of second, five digits
  • ffffff – Fractions of second, six digits
  • fffffff – Fractions of second, seven digits
  • g – General
  • %g – Era (eg. A.D.)
  • y-g – Year and era (eg. 5-A.D.)
  • gg – Era (eg. A.D.)
  • h – Hour (1-12) (Doesn’t seem to work)
  • %h – Hour (1-12)
  • h-m – Hour and minute
  • hh – Hour (01-12)
  • H – Hour (0-23) (Doesn’t seem to work)
  • HH – Hour (00-23)
  • m – Month name and date
  • %m – Minute (0-59)
  • hh_m – Hour and minute (0-59)
  • mm – Minute (00-59)
  • M – Month name and date
  • %M – Month number (1-12)
  • M+d – Month number and day number
  • MM – Month number (01-12)
  • MMM – Month abbreviation
  • MMMM – Month name
  • s – Standard sortable date/time
  • %s – Seconds (0-59)
  • s^ff – Seconds (0-59) and fraction of seconds
  • ss – Seconds (00-59)
  • t – Long time
  • %t – First letter of AM/PM designator
  • hh+t – Hour and first letter of AM/PM designator
  • tt – AM/PM designator
  • y – Short date
  • %y – Year (0-99)
  • m-y – Month and year
  • yy – Year (00-99)
  • yyyy – Year (0000-9999)
  • z – Doesn’t work
  • %z – Whole hour time zone (-12 to +13)
  • Zone:z – Zone – and whole hour time zone (-12 to +13)
  • zz – Whole hour time zone (-12 to +13) with two digits
  • zzz – Time zone in hours and minutes})

Reverse DNS Lookup from ASP.Net

My client needs to be able to restrict page content based on the client machine name on their intranet site. I knew that you could get the IP address from the REMOTE_HOST HTTP header, and some exploration of the System.Net namespace revealed that you can use the System.Net.Dns.GetHostByAddress method to perform a reverse DNS look up to get the machine name. Here’s a VB.Net example:

Dim strIp As String
Dim he As System.Net.IPHostEntry
Dim strHostName As String
strIp = Me.Request.ServerVariables("REMOTE_HOST")
he = System.Net.Dns.GetHostByAddress(strIp)
strHostName = he.HostName

Sharing Session Variables between ASP and ASP.Net Pages

Two methods for sharing session variables between ASP and ASP.Net pages.

I’m working on a major ASP to ASP.Net conversion project that can only really be done one page at a time. One of the obstacles to doing it this way was sharing session variables between ASP and ASP.Net pages.
Most solutions to this common problem involve intermediary pages that write the session variables of one type into form variables and then from form variables back into session variables of the other type (See Peter A. Bromberg’s example on eggheadcafe.com).

Microsoft’s Billy Yuen’s solution uses SQL Server as a repository for session data, and provides custom objects for both ASP and ASP.Net to access the session data.

Ultimately, we abandoned this since the site’s use of session variables was minimal and there were other ways of solving the problems we were using session state for.

CanVS Macros Save You from Carpal Tunnel?

I am currently writing my ASP.Net pages in VB.Net since my client comes from the ASP/VBScript tradition and the VB syntax is more familiar to them. It hasn’t taken long for me to miss the terse syntax of C#, especially since I have mild tendonitus and typing all those End X is killing me. Properties are the worst. Even though VS.Net will complete properties that you start in VB, you still have to write one line for each to get them started. So I looked to the web for a solution to this problem, and low and behold, I found this great article by Scott Mitchelle on using VS.Net macros to write properties.

I took this concept and expanded it by writing a macro that will write the property shells for all the private member reocords you have selected. Click here to download the macro.

"Tech" PodCasts

I’m surprised how hard it is to find a good tech PodCast that talks about coding and best practices. I stumbled upon Craig Shoemaker’s Polymorphic PodCast first, and haven’t found anything that comes close to the quality of its content since. I think his show should make it somewhere higher thank #27 on Podcast Alley’s list of top tech podcasts. Of course, I’m going to be biased towards shows that cover the technologies I develop in (.Net, Oracle, etc.) so perlcast and anything with mac in the title are out for me. But half the shows on that list are mostly music (i.e. daily source code).

Too bad the .Net show won’t podcast just because the word "pod" is in the title.

Trimming down that ViewState

I’m developing an ASP.Net site for a client that is very concerned about bandwidth. My first objective is to reduce the size of the hidden veiwstate data returned in the form.

The worst culprits seem to be the data display controls, especially the DataGrid. I found that for displaying small amounts of data in a table (20 rows by 5 columns) the page with a DataGrid returned over twice as much view state data (5,716 bytes vs 2,512 bytes) as one using a Repeater to render the table.

Worse yet, once I started returning larger amounts of data, even the Repeater was generating a heafty amount of veiw state data. Since these pages are essentially run once search result pages, I’ve added the following to the Page directive


ASP.Net still returns view state data for the page, but only a small amount. To completely remove it from the page, you have to remove all runat=”server” tags.

The following article also shows how to trim the ViewState data on a control by control basis: