Forum Discussion

DR8's avatar
DR8
New Contributor
5 years ago

Getting current Time in Specific Time Zone

I am trying to get current time using vbscript. Instead of the user/system time zone, I would like to get current CST time. How to get current time in some specific Time Zone using vbscript in TestComplete?

  • no, not with aqdatetime.

    if you can find a reliable, free API that gives you current time at a specified timezone, then you could use the aqHttp object and do a get request. 

    then you'd most likely have to save the response body as a variable and then pull the proper key value pairs to output the time

  • for example, using the free api at http://api.timezonedb.com, I created a free account to access the current time in Chicago, the below stub is in Python, but the methods used will be available in vbscript as well at https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqhttp/creategetrequest.html

     

     

     

            
    def get_CST():
      
      address = "http://api.timezonedb.com/v2.1/get-time-zone?key=YOUR_API_KEY&format=json&by=zone&zone=America/Chicago"
    
      # Create an aqHttpRequest object
      aqHttpRequest = aqHttp.CreateGetRequest(address)
    
      # Send the request, get an aqHttpResponse object
      aqHttpResponse = aqHttpRequest.Send()
    
      if aqHttpResponse != None:
        # Read the response data
        Log.Message(aqHttpResponse.Text) # A response body
        
        df = aqHttpResponse.Text #in as a str
        date = df[-21:-2] #get daterange 
        Log.Message(date) #log a message of current CST date
        return date #output date
        
        
        # Save the response body to a file and place it to the project folder
        #aqHttpResponse.SaveToFile(Project.Path + "body.txt")

     

     

     

    and the following logs of running this routine:

     

     

  • there is an aqDateTime object that can get the dates and times, but I'm not too sure if there are inbuilt methods to specify a timezone. Either way i think you could use something like aqDateTime.now to get the times, and then use aqDateTime.AddHours to add/subtract however many hours it would take to get to the desired time zone

    • DR8's avatar
      DR8
      New Contributor

      Thank you.

       

      We have employees in different Time Zones. But the desired result for Time zone is in CST. So subtracting and adding would not help. 

      Is there a direct way to get current CST time?

      • hkim5's avatar
        hkim5
        Staff

        no, not with aqdatetime.

        if you can find a reliable, free API that gives you current time at a specified timezone, then you could use the aqHttp object and do a get request. 

        then you'd most likely have to save the response body as a variable and then pull the proper key value pairs to output the time