Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Active Server Pages (ASP)
Expertise: Beginner
Jun 13, 2000

Displaying Time in Different Time Zones

Question:

I have seen an easy way to increase any given portion of NOW(), but it was months ago and I can't find it again. What I want to accomplish is to display the current time for California, New York and France ( as 9:27 AM or 4:15 PM [no 0's]). I could disassemble, increase, and reassemble, but the code seems a bit overkill. Is there a function to simply increase the hour by 3 and 9?

Answer:

You can use the DateAdd function to add the number of hours you need to any time to get a future or past time. In addition, you can also use the FormatDateTime function to format your date appropriately.

The DateAdd function adds a specified time interval to a date/time value.

DateAdd(interval, number, date)
To add 3 hours to a date in a variable strDateTime, use this code:
  
 DateAdd("h", 3, strDateTime)
The "h" signifies hours, and the 3 indicates the number of hours to add. To subtract, use a negative value.

The FormatDateTime function returns a date/time value appropriately formatted.

   FormatDateTime(Date[, NamedFormat])
For our purpose, we will be using the NamedFormats vbLongTime (3) or vbShortTime (4).

If you use the code:

  
 FormatDateTime(Now(),3)
you will obtain something like this:
   2:59:16 PM
On the other hand, if you use the following code:
   FormatDateTime(Now(),4)
you will obtain:
   14:59
You can use all this knowledge to solve your particular problem.

Your question actually consists of two parts:

  1. How do I show time for CA, NY (+3) and FR (+9)
  2. How do I show time as 9:27 AM (without seconds)

1. To show time for CA, NY and FR (assuming your web server is in CA), use the following code:

Dim strDateTime
' -- Get Current CA time, Format = Long Time
strDateTime = FormatDateTime(Now(),3)
' -- output it
Response.write "CA Time = " & strDateTime & "
" ' -- Add 3 hours to get NY time, and Format it strDateTime = FormatDateTime(DateAdd("h",3,strDateTime),3) ' -- output NY Time Response.write "NY Time = " & strDateTime & "
" ' -- Add 6 hours to get FR time, (remember we ' -- already added 3 hours before), and format it strDateTime = FormatDateTime(DateAdd("h",6,strDateTime),3) ' -- output FR time Response.write "FR Time = " & strDateTime & "
"
The above code would result in three lines as follows:
CA Time = 3:09:21 PM
NY Time = 6:09:21 PM
FR Time = 12:09:21 AM

2. To show time without seconds (whether using for this purpose or for any other purpose), simply write a small routine that would properly format the time. Create this function and place it appropriately within your ASP file (I prefer to keep it in a common library of routines ASP file, and include the file when needed).

Function FormatTheTime(byval s)
	dim h, m
	h = left(s, instr(s,":")-1)
	m = mid(s, instr(s,":")+1)
	If Int(h) > 12 Then
		s = Int(h) - 12 & ":" & m & " PM"
	Else
		s = s & " AM"
	End if
	FormatTheTime = s
End Function
As you can see, this function takes a 24-hour format value and returns a value back with either AM or PM appended to it.

Then, modify our previous code to use a 24-hour format and call this function before outputting the value:

Dim strDateTime
' -- Get Current CA time, Format = Short Time
strDateTime = FormatDateTime(Now(),4)
' -- output it, calling our special function
Response.write "CA Time = " & FormatTheTime(strDateTime) & "
" ' -- Add 3 hours to get NY time, and Format it strDateTime = FormatDateTime(DateAdd("h",3,strDateTime),4) ' -- output NY Time, calling our special function Response.write "NY Time = " & FormatTheTime(strDateTime) & "
" ' -- Add 6 hours to get FR time, (remember we ' -- already added 3 hours before), and format it strDateTime = FormatDateTime(DateAdd("h",6,strDateTime),4) ' -- output FR time, calling our special function Response.write "FR Time = " & FormatTheTime(strDateTime) & "
"
And this time, you should see:
CA Time = 3:09 PM
NY Time = 6:09 PM
FR Time = 00:09 AM

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date