Solved

UTC (timezone) without using an API

Posted on 2016-09-16
16
56 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

627 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