<?xml version="1.0" encoding="UTF-8" ?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
			<title>Tagged with "vb"</title>
			<link>http://www.addedbytes.com/feeds/tag-feed/</link>
			<description></description>
			<language>en</language>
			<copyright>Web Development in Brighton - Added Bytes 2006</copyright>
			<ttl>120</ttl>
			<item>
				<title>VBScript Date Format Functions</title>
				<link>http://www.addedbytes.com/blog/code/vbscript-date-format-functions/</link>
				<description><![CDATA[ Date formatting in VBScript is not quite as powerful as PHP. This function gives you plenty more ways to format dates and times in VBScript with the minimum of effort. <p>VBScript's date formatting doesn't offer a huge number of options compared to PHP's date() function. While it's relatively easy to just write small snippets of code every time you need to format a date a specific way, life would be much easier if there was a function you could call on time and again to do the work for you.</p>

<p>Below, you will find three date format functions, all of which make some use of Unix timestamps, a concept most ASP developers will not be familiar with. Unix timestamps are defined as the number of seconds since the epoch, 00:00:00 01/01/1970, and are a very sensible way to store dates and times in a database, in my opinion. They are very easy to work with, especially when selecting data from a database, and once you are used to them, they can make life much much simpler.</p>

<p><em>NOTE: These functions were updated on the 15th March 2004 to fix a couple of known bugs. In addition, any scripts using these functions may need to be tweaked to take account of your locale. Each locale will use a slightly different date and time format, so please check yours before using these functions, or simply set your locale to UK. To set your locale to UK, you can use the code below:</em></p>

<pre class="php">&lt;%@LANGUAGE=VBSCRIPT" LCID=2057%&gt;</pre>

<p><em>There is also a [url=http://www.microsoft.com/globaldev/reference/lcid-all.mspx]complete list of valid locales[/url] on the Microsoft website, with the values in the right hand column being the ones you will want.</em></p>

<p><em>Finally, the "monthname" function is VBScript is not present in every version, so may need to be defined manually. Please see comments on this article for an example.</em></p>

<p>The first function, UDate(), will convert a normal date into a Unix timestamp. The second function, unUDate(), simply does the reverse, converting any timestamp into a readable date. The last function takes a date and a format string, and returns a formatted date according to what you put into the formatting string.</p>

<h3>UDate()</h3>

<pre class="php">function UDate(oldDate)
    UDate = DateDiff("s", "01/01/1970 00:00:00", oldDate)
end function</pre>

<p>In practice, UDate is very easy to use. For example, if you want to know the current Unix time (the number of seconds since the epoch), you would use something like the following, which would give intCurrent_Unix_Time a value of 1069916571 at the time of writing.</p>

<pre class="php">intCurrent_Unix_Time = UDate(Now())</pre>

<h3>unUDate()</h3>

<pre class="php">function unUDate(intTimeStamp)
    unUDate = DateAdd("s", intTimeStamp, "01/01/1970 00:00:00")
end function</pre>

<p>unUDate is as easy to use as UDate. Simply feed in an integer, and it will return a matching date. It will treat the integer as the number of seconds between the epoch and the desired date, and will return a properly formatted date for normal use in VBScript accordingly.</p>
	
<h3>formatDate()</h3>

<p>Now here's where it starts to get tricky. formatDate is also quite easy to use, but only once you get the hang of how it works. As you can see from the below, formatDate requires two values to be passed to it. The first, format, is a string telling the function how you would like the date formatted, and the second, intTimeStamp, can be either a Unix timestamp (preferred) or a normal VBScript date.</p>

<p>The format of the string is what we want to make use of to lay out our dates exactly as we would like, and perhaps this is best shown with an example. I would like a date displayed on my site in the following format: "7:11am, Thursday 27th November, 2003". In normal VBScript that would be a pain to format - I'd probably just end up giving up and using the normal VBLongTime and VBLongDate to display something similar. I would prefer complete control though...</p>

<p>The format string for formatDate can include a whole selection of characters to represent certain date or time entities. There's a table of the ones included in this function just below here. The format string needs to include these entities where it wants them to be replaced by the correct time or date value. To get the above formatted date and time, then, I could use the following:</p>

<pre class="php">strDateTime = formatDate(<strong>"%g:%i%a, %l %j%O %F, %Y"</strong>, UDate(Now()))</pre>

<p>That looks a lot harder than it actually is. To tell the function we are aiming to replace a specific entity in the string with a time or date element, we use a symbol made up of a percent sign (%) and a letter. It is a case sensitive function, so be careful you form your format strings correctly. Without further ado, here's a list of the entities you can include in the format string (anything in the string that is not an entity from the list below will still be in the string when it has been processed).</p>

<ul><li><span class="listpad">%A</span> - AM or PM</li>
<li><span class="listpad">%a</span> - am or pm</li>
<li><span class="listpad">%m</span> - Month with leading zeroes (01 - 12)</li>
<li><span class="listpad">%n</span> - Month without leading zeroes (1 - 12)</li>
<li><span class="listpad">%F</span> - Month name (January - December)</li>
<li><span class="listpad">%M</span> - Three letter month name (Jan - Dec)</li>
<li><span class="listpad">$d</span> - Day with leading zeroes (01 - 31)</li>
<li><span class="listpad">%j</span> - Day without leading zeroes (1 - 31)</li>
<li><span class="listpad">%H</span> - Hour with leading zeroes (12 hour)</li>
<li><span class="listpad">%h</span> - Hour with leading zeroes (24 hour)</li>
<li><span class="listpad">%G</span> - Hour without leading zeroes (12 hour)</li>
<li><span class="listpad">%g</span> - Hour without leading zeroes (24 hour)</li>
<li><span class="listpad">%i</span> - Minute with leading zeroes (01 to 60)</li>
<li><span class="listpad">%I</span> - Minute without leading zeroes (1 to 60)</li>
<li><span class="listpad">%s</span> - Second with leading zeroes (01 to 60)</li>
<li><span class="listpad">%S</span> - Second without leading zeroes (1 to 60)</li>
<li><span class="listpad">%L</span> - Number of day of week (1 to 7)</li>
<li><span class="listpad">%l</span> - Name of day of week (Sunday to Saturday)</li>
<li><span class="listpad">%D</span> - Three letter name of day of week (Sun to Sat)</li>
<li><span class="listpad">%O</span> - Ordinal suffix (st, nd rd, th)</li>
<li><span class="listpad">%U</span> - UNIX Timestamp</li>
<li><span class="listpad">%Y</span> - Four digit year (2003)</li>
<li><span class="listpad">%y</span> - Two digit year (03)</li></ul>

<p>You can include any of the above in the format string at any point. I would recommend not using a percentage sign if you can help it (except obviously as part of one of the elements above), and in the same vein, using ordinals anywhere except after a number is slightly foolish.</p>

<p>If you would like to see anything else added to the list, above, please email me and I will see what I can do.</p>

<p>The below is the formatDate function itself. To use, simply copy and paste it into a script or include file and call it as above, with whichever format string you require.</p>

<pre class="php">function formatDate(format, intTimeStamp)
    dim unUDate, A

    ' Test to see if intTimeStamp looks valid. If not, they have passed a normal date
    if not (isnumeric(intTimeStamp)) then
        if isdate(intTimeStamp) then
            intTimeStamp = DateDiff("S", "01/01/1970 00:00:00", intTimeStamp)
        else
            response.write "Date Invalid"
            exit function
        end if
    end if
    
    if (intTimeStamp=0) then 
        unUDate = now()
    else
        unUDate = DateAdd("s", intTimeStamp, "01/01/1970 00:00:00")
    end if

    unUDate = trim(unUDate)

    dim startM : startM = InStr(1, unUDate, "/", vbTextCompare) + 1
    dim startY : startY = InStr(startM, unUDate, "/", vbTextCompare) + 1
    dim startHour : startHour = InStr(startY, unUDate, " ", vbTextCompare) + 1
    dim startMin : startMin = InStr(startHour, unUDate, ":", vbTextCompare) + 1

    dim dateDay : dateDay = mid(unUDate, 1, 2)
    dim dateMonth : dateMonth = mid(unUDate, startM, 2)
    dim dateYear : dateYear = mid(unUDate, startY, 4)
    dim dateHour : dateHour = mid(unUDate, startHour, 2)
    dim dateMinute : dateMinute = mid(unUDate, startMin, 2)
    dim dateSecond : dateSecond = mid(unUDate, InStr(startMin, unUDate, ":", vbTextCompare) + 1, 2)

    format = replace(format, "%Y", right(dateYear, 4))
    format = replace(format, "%y", right(dateYear, 2))
    format = replace(format, "%m", dateMonth)
    format = replace(format, "%n", cint(dateMonth))
    format = replace(format, "%F", monthname(cint(dateMonth)))
    format = replace(format, "%M", left(monthname(cint(dateMonth)), 3))
    format = replace(format, "%d", dateDay)
    format = replace(format, "%j", cint(dateDay))
    format = replace(format, "%h", mid(unUDate, startHour, 2))
    format = replace(format, "%g", cint(mid(unUDate, startHour, 2)))

    if (cint(dateHour) &gt; 12) then
        A = "PM"
    else
        A = "AM"
    end if
    format = replace(format, "%A", A)
    format = replace(format, "%a", lcase(A))

    if (A = "PM") then format = replace(format, "%H", left("0" &amp; dateHour - 12, 2))
    format = replace(format, "%H", dateHour)
    if (A = "PM") then format = replace(format, "%G", left("0" &amp; cint(dateHour) - 12, 2))
    format = replace(format, "%G", cint(dateHour))

    format = replace(format, "%i", dateMinute)
    format = replace(format, "%I", cint(dateMinute))
    format = replace(format, "%s", dateSecond)
    format = replace(format, "%S", cint(dateSecond))
    format = replace(format, "%L", WeekDay(unUDate))
    format = replace(format, "%D", left(WeekDayName(WeekDay(unUDate)), 3))
    format = replace(format, "%l", WeekDayName(WeekDay(unUDate)))
    format = replace(format, "%U", intTimeStamp)
    format = replace(format, "11%O", "11th")
    format = replace(format, "1%O", "1st")
    format = replace(format, "12%O", "12th")
    format = replace(format, "2%O", "2nd")
    format = replace(format, "13%O", "13th")
    format = replace(format, "3%O", "3rd")
    format = replace(format, "%O", "th")

    formatDate = format

end function</pre> <br><br>]]></description>
				<pubDate>Mon, 15 Mar 2004 14:07:00 +0000</pubDate>
				<guid isPermaLink="false">http://www.addedbytes.com/blog/code/vbscript-date-format-functions/</guid>
				<dc:creator>Dave Child</dc:creator>
				<a href="/feeds/tag-feed/?tags=asp&amp;start=0" class="ditto_tag" rel="tag">asp</a>,<a href="/feeds/tag-feed/?tags=date&amp;start=0" class="ditto_tag" rel="tag">date</a>,<a href="/feeds/tag-feed/?tags=development&amp;start=0" class="ditto_tag" rel="tag">development</a>,<a href="/feeds/tag-feed/?tags=javascript&amp;start=0" class="ditto_tag" rel="tag">javascript</a>,<a href="/feeds/tag-feed/?tags=programming&amp;start=0" class="ditto_tag" rel="tag">programming</a>,<a href="/feeds/tag-feed/?tags=reference&amp;start=0" class="ditto_tag" rel="tag">reference</a>,<a href="/feeds/tag-feed/?tags=scripts&amp;start=0" class="ditto_tag" rel="tag">scripts</a>,<a href="/feeds/tag-feed/?tags=time&amp;start=0" class="ditto_tag" rel="tag">time</a>,<a href="/feeds/tag-feed/?tags=unix&amp;start=0" class="ditto_tag" rel="tag">unix</a>,<a href="/feeds/tag-feed/?tags=vb&amp;start=0" class="ditto_tag" rel="tag">vb</a>,<a href="/feeds/tag-feed/?tags=vbscript&amp;start=0" class="ditto_tag" rel="tag">vbscript</a>,<a href="/feeds/tag-feed/?tags=webdesign&amp;start=0" class="ditto_tag" rel="tag">webdesign</a>
			</item>

			<item>
				<title>VBScript Regular Expressions</title>
				<link>http://www.addedbytes.com/blog/code/vbscript-regular-expressions/</link>
				<description><![CDATA[ Regular expression reference and examples for VBScript. <p>Regular expressions in VBScript are two words that can bring many to their knees, weeping, but they are not as scary as some would have you believe. With their roots in Perl, regular expressions in VBScript use similar syntax, and the chances are that you may already be familiar with the concepts here if you have played with regular expression matching before.</p>

<p>Below, you will find three sections. The first section, <a href="http://www.addedbytes.com/asp/vbscript-regular-expressions/#reference">Reference</a>, is a simple reference listing the most-used of the various symbols and characters used in regular expressions. The second section, <a href="http://www.addedbytes.com/asp/vbscript-regular-expressions/#functions">Functions</a>, has two functions in it that may make life easier for you. The third section, <a href="http://www.addedbytes.com/asp/vbscript-regular-expressions/#examples">Examples</a>, is where the fun begins - examples of regular expressions in action.</p>

<h3 id="reference">Reference</h3>

<p><strong>Character Sets and Grouping</strong></p>

<ul><li class="reference"><span class="listpad">.</span> - Any single character (except new line character, "\n")</li><li class="reference"><span class="listpad">[]</span> - Encloses any set of characters</li><li class="reference"><span class="listpad">^</span> - Matches any characters not within following set</li><li class="reference"><span class="listpad">[A-Z]</span> - Any upper case letter between A and Z</li><li class="reference"><span class="listpad">[a-z]</span> - Any lower case letter between a and z</li><li class="reference"><span class="listpad">[0-9]</span> - Any digit from 0 to 9</li><li class="reference"><span class="listpad">()</span> - Group section. Also can then be back-referenced with $1 to $n, where n is the number of groups</li><li class="reference"><span class="listpad">|</span> - Or. (ab)|(bc) will match "ab" or "bc"</li></ul>

<p><strong>Repetition</strong></p>

<ul><li class="reference"><span class="listpad">+</span> - One or more</li><li class="reference"><span class="listpad">*</span> - Zero or more</li><li class="reference"><span class="listpad">?</span> - Zero or one</li><li class="reference"><span class="listpad">{5}</span> - Five</li><li class="reference"><span class="listpad">{1,3}</span> - One to three</li><li class="reference"><span class="listpad">{2,}</span> - Two or more</li></ul>

<p><strong>Positioning</strong></p>

<ul><li class="reference"><span class="listpad">^</span> - Start of string</li><li class="reference"><span class="listpad">$</span> - End of string</li><li class="reference"><span class="listpad">\b</span> - End of word</li><li class="reference"><span class="listpad">\n</span> - New line</li><li class="reference"><span class="listpad">\r</span> - Carriage return</li></ul>

<p><strong>Miscellaneous</strong></p>

<ul><li class="reference"><span class="listpad">\</span> - Escape character</li><li class="reference"><span class="listpad">\t</span> - Tab</li><li class="reference"><span class="listpad">\s</span> - White space</li><li class="reference"><span class="listpad">\w</span> - Matches word (equivalent of [A-Za-z0-9_])</li></ul>

<p>Please note that the escape character mentioned above is not usable in normal VBScript. Regular expression syntax is based upon Perl regular expression syntax. To escape a character in VBScript, you usually double it. For example, the following will print out 'This is a "quoted" piece of text'.</p>

<code>response.write("This is a ""quoted"" piece of text.")</code>

<h3 id="functions">Functions</h3>

<p>The first of the functions below, ereg (named after the PHP function to keep me from going quite quite mad), is the one you will probably use most. Simply put, if you feed in a string, pattern, and choose whether or not you would like to ignore the case of letters in either, the function will return TRUE if the string contains the pattern, or FALSE if not.</p>

<code>function ereg(strOriginalString, strPattern, varIgnoreCase)
    ' Function matches pattern, returns true or false
    ' varIgnoreCase must be TRUE (match is case insensitive) or FALSE (match is case sensitive)
    dim objRegExp : set objRegExp = new RegExp
    with objRegExp
        .Pattern = strPattern
        .IgnoreCase = varIgnoreCase
        .Global = True
    end with
    ereg = objRegExp.test(strOriginalString)
    set objRegExp = nothing
end function</code>

<p>Next up we have ereg_replace. Like it's shorter cousin, you need to feed it a string, a pattern and choose your case sensitivity. This time, you must also add a replacement. This function will replace all instances of the pattern with the replacement in the string (if you change ".Global = True" to ".Global = False" then the function will only replace the first instance of the pattern with the replacement).</p>

<code>function ereg_replace(strOriginalString, strPattern, strReplacement, varIgnoreCase)
    ' Function replaces pattern with replacement
    ' varIgnoreCase must be TRUE (match is case insensitive) or FALSE (match is case sensitive)
    dim objRegExp : set objRegExp = new RegExp
    with objRegExp
        .Pattern = strPattern
        .IgnoreCase = varIgnoreCase
        .Global = True
    end with
    ereg_replace = objRegExp.replace(strOriginalString, strReplacement)
    set objRegExp = nothing
end function</code>

<h3 id="examples">Examples</h3>

<p><strong>Example 1: Checking hexadecimal string</strong></p>

<p>A hexadecimal number can be made up of any digit, and any letter, upper or lower case, between a and f, inclusive. So to check if a string is actually hexadecimal, the following will do quite nicely (strOriginalString is the original string to be tested):</p>

<code>&lt;%
if ereg(strOriginalString, "[^a-f0-9\s]", True) = True then
    response.write "String is not hexadecimal."
else
    response.write "String is hexadecimal."
end if
%&gt;</code>

<p>The pattern, "[^a-f0-9\s]" matches anything that is <strong>not</strong> in the set of characters specified (so if there is anything in the string that is not in that set, the function will return True). The characters specified are all letters between a and f inclusive, and we've specified a case insensitive match, so upper case letters will be treated the same way. We are also allowing whitespace (new lines, spaces, carriage returns and tabs), which is what the "\s" represents in regular expressions.</p>

<p>Example string that returns False (and is therefore hexadecimal):</p>

<code>AAcc99</code>

<p><strong>Example 2: Masking the last section of an IP address</strong></p>

<p>An IP address is made up of four sets of numbers seperated by periods. It's common practice, if you are going to display visitor (or any) IP address on your site, to mask the last (fourth) set of numbers. Here's a way to use ereg_replace to do just this:</p>

<code>&lt;%
strOriginalString = ereg_replace(strOriginalString, "([^0-9])([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.[0-9]{1,3}([^0-9])", "$1$2.$3.$4.***$5", True)
%&gt;</code>

<p>This is a little more tricky, as you'd hopefully expect from a second example. It looks harder than it is though, so one step at a time. There are actually only a few entities in the pattern - they are just repeated. The most important is this: "([0-9]{1,3})". It matches a section of an IP adress, and is enclosed in brackets so that this section can be used in the replacement of the pattern as well (otherwise we would not be able to keep the first three parts of the IP address to display). You can see these sections in use, referenced with "$2", "$3" and "$4" in the replacement. The pattern within the brackets simply says "between one and three digits between 0 and 9".</p>

<p>The second repeated section is "\.". We use a backslash before the period to indicate that this period (the character following the backslash) is to be treated as a normal period. We call this an <em>escaped character</em>, and this is a fairly common practice. The period, unescaped (without the backslash), is used as a symbol representing "any character except the new line character".</p>

<p>Example input text:</p>

<code>My IP address is 123.456.78.9 but 4444.1.1.1 is just a bunch of random numbers, and so is 12.34.56, and 1.1.1.1 is another valid IP.</code>

<p>Example output text:</p>

<code>My IP address is 123.456.78.*** but 4444.1.1.1 is just a bunch of random numbers, and so is 12.34.56, and 1.1.1.*** is another valid IP.</code>

<p><strong>Example 3: Making the second word of every sentence in a string bold, as long as the word before only contains upper case letters and the second word does not contain an even digit</strong></p>

<p>Getting more interesting now, this example is not in the least bit useful in practice, but should prove to be a useful demonstration of the power of regular expressions. It sounds tough - but with regular expressions, it's a walk in the park.</p>

<code>&lt;%
strOriginalString = ereg_replace(". " &amp; strOriginalString, "(\.|!|\?)\s([A-Z]+)\s([^02468\s]+)\s", "$1 $2 &lt;strong&gt;$3&lt;/strong&gt; ", False)
strOriginalString = mid(strOriginalString, 2)
%&gt;</code>

<p>We start by adding an artificial period and space to the beginning of the string, just to make sure we catch the first sentence, and add a line to strip our extra characters out afterwards. We only want those sentences split with punctuation <em>and</em> a space, or we'll end up with bold decimals and it will be very messy indeed. So, we check for puncuation, followed by a space, followed by a word made entirely of capitals, followed by another space, followed by a second word that doesn't contain even numbers, or whitespace, followed by a space. If we find that, we replace it with the same items we picked up in brackets, only with a &lt;strong&gt;&lt;/strong&gt; tag pair around the second word.</p>

<p>Example input text:</p>

<code>THE quick brown fox jumped over the lazy dog? Many red balloons blew up! EVEN num2ber sentence. ODD num3ber sentence.</code>

<p>Example output text:</p>

<code>THE <strong>quick</strong> brown fox jumped over the lazy dog? Many red balloons blew up! EVEN num2ber sentence. ODD <strong>num3ber</strong> sentence.</code> <br><br>]]></description>
				<pubDate>Fri, 07 Nov 2003 09:29:40 +0000</pubDate>
				<guid isPermaLink="false">http://www.addedbytes.com/blog/code/vbscript-regular-expressions/</guid>
				<dc:creator>Dave Child</dc:creator>
				<a href="/feeds/tag-feed/?tags=asp&amp;start=0" class="ditto_tag" rel="tag">asp</a>,<a href="/feeds/tag-feed/?tags=code&amp;start=0" class="ditto_tag" rel="tag">code</a>,<a href="/feeds/tag-feed/?tags=expressions&amp;start=0" class="ditto_tag" rel="tag">expressions</a>,<a href="/feeds/tag-feed/?tags=programming&amp;start=0" class="ditto_tag" rel="tag">programming</a>,<a href="/feeds/tag-feed/?tags=reference&amp;start=0" class="ditto_tag" rel="tag">reference</a>,<a href="/feeds/tag-feed/?tags=regex&amp;start=0" class="ditto_tag" rel="tag">regex</a>,<a href="/feeds/tag-feed/?tags=regexp&amp;start=0" class="ditto_tag" rel="tag">regexp</a>,<a href="/feeds/tag-feed/?tags=regular&amp;start=0" class="ditto_tag" rel="tag">regular</a>,<a href="/feeds/tag-feed/?tags=regular-expressions&amp;start=0" class="ditto_tag" rel="tag">regular-expressions</a>,<a href="/feeds/tag-feed/?tags=scripting&amp;start=0" class="ditto_tag" rel="tag">scripting</a>,<a href="/feeds/tag-feed/?tags=vb&amp;start=0" class="ditto_tag" rel="tag">vb</a>,<a href="/feeds/tag-feed/?tags=vbscript&amp;start=0" class="ditto_tag" rel="tag">vbscript</a>
			</item>
	</channel>
</rss>