Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Zulu time(GMT)

Status
Not open for further replies.

Kamas

MIS
Mar 27, 2003
1
US
I need to display date in Zulu. How do I implement this in Vbscript or how can I get GMT ?
 
Wow.

I saw this question and figured "piece of cake!"

[ponder] Then I started looking. And looking. And looking .

My first thought was that there might VBScript language primitives - you know, a function or something - to handle this. If you find one, let me know!

Then I thought "well sure, there is some readily available object with a GMT-time property or at least a conversion method." If you find one, let me know!

Next I figured "hey, there must be a way to crank out a simple VB ActiveX control or DLL to do this!" If you find one, let me know!

Finally I thought "well there ought to be a system DLL I can call from within a VB ActiveX component to handle this..." Aha! Yes there are several possible approaches. And boy are they ugly, with moles and warts with hairs springing from them along with scales and dripping pus! What a circus of type conversions you'd need. Ugh!

Desperation

So then I got desperate. I mean desperate!

I descended to the depths of ECMAScript - also known as Javascript, or in our case JScript.

Today the politically correct term for Zulu Time or GMT Time is Universal Coordinated Time, which goes by a French acronym UTC. Don't misspell it UCT like my fingers keep wanting to do, or you'll have nothing but grief!

You have to understand a few things here:
[ul][li]It isn't actually very painful to intermix VBScript and ECMAScript (JScript) within a single desktop script file or ASP page or DHTML page.
[li]ECMAScript doesn't come from the world of Windows, so it has its own ideas about date formats.
[li]JScript has a few "extensions" to help make it Windows compatible - so some date interchanges aren't too bad.[/ul]
Some code

You can't do this in a .VBS file.

Here is a sample Windows Scripthost File (UTC.wsf) that demonstrates getting the current (local) system date, converting it to a UTC date with the help of JScript (neither ECMAScript nor Netscape's Javascript will handle this as written), and then displaying the results via MsgBox:
Code:
<job id=UTCDemo>
<script language=jscript>
function strUTCDate(aDate)
{
  var d, s;

  d = new Date(aDate);
  s = (d.getUTCMonth() + 1) + &quot;/&quot;;
  s += d.getUTCDate() + &quot;/&quot;;
  return(s + d.getUTCFullYear());
}
</script>
<script language = vbscript>
Dim dtDate, dtUTC

dtDate = Date
dtUTC = CDate(strUTCDate(dtDate))

MsgBox &quot;Local date: &quot; & CStr(dtDate) & _
       &quot;  UTC Date: &quot; & CStr(dtUTC)
</script>
</job>
The first thing that may strike you is that there is some weird markup around the script. Yeah, but that's the way a .WSF works, and it allows intermixing of scripting languages in a single script file. It also allows includes and stuff. This is REALLY a handy thing, ugly as it may appear. Check out the current Windows Script Host docs for information.

Some other things...

You can set the date of a JScript &quot;Date object&quot; like our d here using a VBScript date-format variant. Microsoft is now calling this a VT_DATE value. This is what ECMAScript and Javascript can't do - you'd have to pass a date string in ECMAScript format to the function if you weren't using the JScript engine.

There is a way to get a VT_DATE back out of a &quot;Date object&quot; but it is only available for the current date. In other words the UTC transformations aren't available that way. This is why I have written the function to return a string. Then I use CDate( ) to convert it to a VBScript-style date variant (a VT_DATE) after I get it back.

Note also the crap where I add 1 to the month after fetching it via the &quot;Date object's&quot; getUTCMonth( ) method. For whatever odd reason ECMA chose to have this method return 0 to 11 for month values! They don't return days and years that way, so why the month? If you can figure this out, let me know!


If you need this in a DHTML web page, you simply use separate <script></script> tags for each language. If you need this in ASP just declare the JScript function inside:
Code:
<SCRIPT LANGUAGE=JScript RUNAT=Server>
function...
...
</SCRIPT>
Just use the regular <% and %> brackets for things written in the ASP page-default language as usual (usually this would be VBScript).

You might also consider looking at &quot;scriptlet&quot; technology (do a search at to encapsulate this as a script-based component if you need it a lot.


Here is what is sad...

By the time you're done you might almost be better off doing the whole dang thing in JScript anyway. But the true nastiness ahead is the coming Death of VBScript. Remember the &quot;Death of Superman&quot; issue? Ended an era, didn't it?

As of the release of .Net, VBScript is being merged into VB7 (a.k.a. VB.Net). This isn't happening all at once, but in ASP.Net (.ASPX pages) you don't have VBScript to kick around anymore. Instead you have to write VB7.

This has some EXTREMELY good things about it - for one, strong typing. For another, being able to call system DLLs without writing ActiveX wrappers. I'm sure other people here could point out lots more goodies.

Why it sucks.

The C++ lovin' johnnies at Microsoft got hold of the hearts of the VB team and squeezed hard. VB7/VB.Net is now full of Javascript-like crud in place of the things we all have come to know and love (well at least know ) about VB/VBScript.

Things like CDate( ) functions are gone, replaced by the really crappy idea that everything is an object with methods hanging off it. Where you once had:

Code:
dtBirth = CDate(strBirth)

You get something like:

Code:
dtBirth = strBirth.CDate()

Disclaimer - actual method name may be different. My guts are still bunched up from the last time I tried looking at VB.Net over 6 months ago!

kamas:

I hope this at least helped to answer your question. As you can see it really got me going.

Good luck! [thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top