Solved

UTC (timezone) without using an API

Posted on 2016-09-16
16
27 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 32

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 32

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
 

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 32

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 32

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 32

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 32

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 32

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now