devxlogo

Displaying Time in Different Time Zones

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 TimestrDateTime = FormatDateTime(Now(),3)' -- output itResponse.write "CA Time = " & strDateTime & "
"' -- Add 3 hours to get NY time, and Format itstrDateTime = FormatDateTime(DateAdd("h",3,strDateTime),3)' -- output NY TimeResponse.write "NY Time = " & strDateTime & "
"' -- Add 6 hours to get FR time, (remember we ' -- already added 3 hours before), and format itstrDateTime = FormatDateTime(DateAdd("h",6,strDateTime),3)' -- output FR timeResponse.write "FR Time = " & strDateTime & "
"

The above code would result in three lines as follows:

CA Time = 3:09:21 PMNY Time = 6:09:21 PMFR 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 = sEnd 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 TimestrDateTime = FormatDateTime(Now(),4)' -- output it, calling our special functionResponse.write "CA Time = " & FormatTheTime(strDateTime) & "
"' -- Add 3 hours to get NY time, and Format itstrDateTime = FormatDateTime(DateAdd("h",3,strDateTime),4)' -- output NY Time, calling our special functionResponse.write "NY Time = " & FormatTheTime(strDateTime) & "
"' -- Add 6 hours to get FR time, (remember we ' -- already added 3 hours before), and format itstrDateTime = FormatDateTime(DateAdd("h",6,strDateTime),4)' -- output FR time, calling our special functionResponse.write "FR Time = " & FormatTheTime(strDateTime) & "
"

And this time, you should see:

CA Time = 3:09 PMNY Time = 6:09 PMFR Time = 00:09 AM

See also  How to Avoid Money Transfer Scams  
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist