Solved

UTC (timezone) without using an API

Posted on 2016-09-16
16
50 Views
Last Modified: 2016-09-16
Is there a way to collect a client side UTC (timezone) without using an API

I have found that many APIs are very slow in the requests going out and coming back, often leading to 2-3 second delays

is there a way I can integrate a client side fed UTC (timezone) without using an API so I can use it in an ASP Classic Query ?

The end result would need to be in the nature of a number (negative-inc symbol or positive-no symbol) eg -5 or 8
0
Comment
Question by:Graeme
  • 9
  • 7
16 Comments
 
LVL 33

Expert Comment

by:Big Monty
ID: 41802102
there's nothing built into vbscript that'll allow you to do this easily, however, you can use server side JScript in ASP. Here's an example:

<script language='Javascript' runat='server'>
  function jsGetUTCTime() {
	
    var d = new Date();
    var n = d.toUTCString();
    return n;
  }
</script>

<%

Function getUTCTime()

  '<!-- Use JScript to get the current GMT time stamp -->
    UTCTime = jsGetUTCTime()
	
  '<!-- strip extraneous info from the date string -->
    UTCTime = Replace(UTCTime, " UTC", "") ' remove UTC
    varStartPoint = inStr(UTCTime, ",") + 1
    varNewLength  = (Len(UTCTime) - varStartPoint)
    UTCTime = Right(UTCTime, varNewLength) 'remove the day name
    UTCTime = Trim(UTCTime)
	
  '<!-- format the date according to ISO 8601 -->
	UTCTimeString = Year(UTCTime) & "-"
	thisMonth = Month(UTCTime)
	If Len(thisMonth) = 1 Then thisMonth = "0" & thisMonth
	UTCTimeString = UTCtimeString & thisMonth & "-"
	thisDay = Day(UTCTime)
	If Len(thisDay) = 1 Then thisDay = "0" & thisDay
	UTCTimeString = UTCtimeString & thisDay & "T"
	thisHour = Hour(UTCTime)
	If Len(thisHour) = 1 Then thisHour = "0" & thisHour
	UTCTimeString = UTCtimeString & thisHour & ":"
	thisMinute = Minute(UTCTime)
	If Len(thisMinute) = 1 Then thisMinute = "0" & thisMinute
	UTCTimeString = UTCtimeString & thisMinute & ":"
	thisSecond = Second(UTCTime)
	If Len(thisSecond) = 1 Then thisSecond = "0" & thisSecond
	UTCTimeString = UTCtimeString & thisSecond & "Z"
	
  getUTCTime = UTCTimeString
End Function

Response.Write "<br>Current Local Time: " & FormatDateTime(Now(), vbLongDate) & " " & FormatDatetime(Now(), vbLongTime)
Response.Write "<br>GMT Time in ISO 8601 Format: " & getUTCTime()

Open in new window


source - http://shurie.com/coder/code_details.asp?codeid=71
0
 

Author Comment

by:Graeme
ID: 41802122
Hi BigMonty and thanks for that

that is awesome, but its not what I would like, I just need the Timezone (eg 8 or -5, etc)
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 41802133
you would then need to do this client side, as your servers time zone would always be the same value.

check out this jsFiddle and see if it's what you want: https://jsfiddle.net/9maczqyy/3/
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Graeme
ID: 41802137
Cheers for that

is there a way to get the value without the '+' symbol ?

I want to put it into a Number field
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 41802141
get rid of the line:

     var prefix = offset < 0 ? "+" : "-";

and change

    return prefix+hours;

to    

    return offset+hours;
0
 

Author Comment

by:Graeme
ID: 41802144
after doing those changes, im getting -472, instead of 8... ?
0
 
LVL 33

Accepted Solution

by:
Big Monty earned 500 total points
ID: 41802148
try this function instead

function timezone() {
    var offset = new Date().getTimezoneOffset();
    var minutes = Math.abs(offset);
    var hours = Math.floor(minutes / 60);
    var prefix = offset < 0 ? "+" : "-";
    var val = prefix+hours;
    return val.replace('+', '').replace('-', '');
}

Open in new window

0
 

Author Comment

by:Graeme
ID: 41802152
excellent! working now!

Next part would be how to i put that into a value for ASP ?

Timezone= '<div id="result1">'  ? (i am sure this is not corect)
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 41802165
simply create a hidden field in your html:

<input type="hidden" name="timeZone" id="timeZone" value="" />

when you go to submit the page, stick the following into a javascript function that will fire when you do the submit:

$('#timeZone').val( timezone() );

this assumes you're using jquery, if not, you can do:

document.getElementById('timeZone').value = timezone();

now you can access the value like you would any other field:

dim myTimeZone
myTimeZone = Request.Form("timeZone")
0
 

Author Comment

by:Graeme
ID: 41802169
Unfortunately its not a submit page, it is going in an insert statement
0
 
LVL 33

Expert Comment

by:Big Monty
ID: 41802177
that's fine, I assume the data for that INSERT statement is coming from a form then? if so, stick it in there.
0
 

Author Comment

by:Graeme
ID: 41802179
No form for this INSERT statement

when the client lands on the page, it records a sessionID and some other particulars (timezone being one of them)

So the insert statement records this as the client browses
0
 

Author Comment

by:Graeme
ID: 41802183
For Example
oConn.Execute("INSERT INTO site_sessions(session_ID,session_IP,session_geoCity,session_geoCountry,session_timezone,session_curr,session_rate,session_date,session_time) VALUES("&Session.SessionID&",'"&Request.ServerVariables("REMOTE_ADDR")&"','"&CityLoc&"','"&CountryLoc&"','"&Timezone&"','"&ClientCurr&"',"&ClientConvert&",'"&Date()&"','"&Time()&"')")

Open in new window

0
 
LVL 33

Expert Comment

by:Big Monty
ID: 41802190
the data has to come from the client, since it is based off of their system.

you're going to need to figure out which page you want to grab that data from, even if you don't use it right away, you can store the value in a session once they submit a page
0
 

Assisted Solution

by:Graeme
Graeme earned 0 total points
ID: 41802195
Client may never submit a page and just browse through links

I have just had a play around and figured out that Response.Write(Timezone()) outputs the value correctly

So im just going to update my INSERT query to replace "&Timezone&" with "&Timezone()&"
0
 

Author Closing Comment

by:Graeme
ID: 41802197
got there in the end! :)

Cheers
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Issues with Insert statement 12 29
return false must be hit after calling certain command 10 33
href return to normal window 5 16
Clear input text 15 9
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question