ASP Code Tricks

ASP Code Tricks

I’m a code monkey by trade…

Every once in a while, I’ll get stumped on a problem that doesn’t seem to have an easy answer. I’ll search online for examples, tutorials, or anything that could help me get to the next step, but sometimes it seems as if I’m the only person who’s ever tried to solve a particular code senario. In an effort to contribute to the overal worldly good, this page will be devoted to showing examples of weird little solutions to weird little problems that aren’t easily found online.

Please email me if I’ve made any major (or minor) mistakes in these examples…

1. UTC Timestamp Conversion In ASP/VBScript

I had a hard time finding out how to do this one… it seems simple now, but at the time I was scratching my head. Sure you could use javascript, but this works just as well.


function FixDate(str,format)
if isnull(str) then
FixDate = " "
else
FixDate = FormatDateTime( &_
DateAdd("s",str, "01/01/1970"),format)
end if
end function

str is the UTC timestamp and format controls the, uhhh… formatting. Look it up, slacker. UTC is the number of seconds since 01/01/1970, so this function adds your UTC value to 1/1/70, then uses FormatDateTime to turn it into a nice, pretty, and readable format.

2. Regular Expression Follies

Seriously, this one ruined 24 hours of my life. I had to take a recordset field and parse out the UTC timestamps into a readable format while maintaining the original formatting of the string. I originally (and incorrectly) assumed that regex could (and should) do this by itself, but I had to combine functions to get the results I wanted. Here’s how I did it:


'-------------------
' main code
'-------------------
do while not MyRs.eof
if not isNull(MyRs(0)) then
strNotesField = NoteDates(MyRs(0))
DateArray = Split(strNotesField, "|")
i = -1
for each thing in DateArray
i = i + 1
if IsNumeric(DateArray(i)) then
strNotesField = Replace(strNotesField, &_
DateArray(i), FixDate(DateArray(i), 0))
end if
next
else
strNotesField = "no notes in database."
end if
MyRs.MoveNext
loop
Response.Write replace(strNotesField, "|", vbCrLf) & vbCrLf
'-------------------
' this function convert UTC to a readable format
'-------------------
function FixDate(str,format)
if isnull(str) then
str = " "
else
FixDate = FormatDateTime(DateAdd("s",str, "01/01/1970"),format)
end if
end function
'-------------------
' this function places delimiters
' around the UTC entries
'-------------------
function NoteDates(note)
Dim re
Set re = New RegExp
re.Pattern = "(\d{10})"
re.Global = true
NoteDates = re.Replace(note, "|$1|")
end function

After checking that MyRs(0) isn’t null, the NoteDates function adds delimiters arount the UTC’s via regular expression pattern matching. We then split the delimited field into an array and For Each through the array, checking each array element with IsNumeric. If it passes IsNumeric, we assume it’s a UTC and replace it with a converted datetime string.

Is this the best way? Beats me… all I know is that I spent a day trying to find the most efficient method of getting the job done and this was the best of all my solutions.

(modified Wednesday, Nov 13, 2002 and again on Tuesday, Jan 07, 2003)

6 Replies to “ASP Code Tricks”

  1. This article is just what I was looking for. I had a similar problem with timestamps in ASP. However the article is not displaying the entire article correctly

  2. Hi Bill!

    Need to insert UTC timestamp code into my website that’s not hosted locally and it’s under linux based server, how do I go about? (& my main concern is whether those codes can take me to a certain link/station that will straighforward return a UTC time stamp on my website)

    P’se help!

    Best,

    Emmanuel.

  3. s/wierd/weird/g

    It’s minor, but it’s a correction all the same. Thanks for publishing the FormatDateTime tip, I was having a shoot-out with Oracle trying to get something to format a timestamp stored in a number field into a human-readable date format.. DateAdd was the missing link that provided closure.

Comments are closed.