Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

UTC (timezone) without using an API

Posted on 2016-09-16
16
Medium Priority
?
71 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 34

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 34

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 34

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 34

Accepted Solution

by:
Big Monty earned 2000 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 34

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 34

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 34

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

916 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