How do you get the current time of people based on their time zone? | XM Community
Solved

How do you get the current time of people based on their time zone?

  • 31 January 2019
  • 7 replies
  • 414 views

I would like to get the current time of my participants based on their time zone so that I can use "branches" in survey flow to direct them to different pages/blocks. I need some support in developing codes to do this. Thanks!
icon

Best answer by TomG 3 February 2019, 16:32

View original

7 replies

Userlevel 5
Badge +6
Hi ,
Qualtrics knows current date and time from the correct time-zone. I suppose it is based on the user's coordinates since it is still correct when I change the time on my computer. (However, note that users can "cheat" their location.) Therefore you can use piped time. You can get possible formats clicking on piped text (see the image) and then you can copy the text and use it as you like.
!

I don't how exactly should the condition look like, but you could, for example, extract hours from it in JavaScript (put it to some question which will be shown before you apply your condition) and send them to an embedded data field. Do not forget to define it in the survey flow. Then you can give users different blocks if the hour is greater or lower than some given threshold. Look at this if you don't know how to do it.

Qualtrics.SurveyEngine.addOnload(function()
{
var time = "${date://CurrentTime/MT}";
var h = time.substring(0, time.indexOf(':'));
Qualtrics.SurveyEngine.setEmbeddedData( 'h', h );
});
Thank you for your response, fleb. However, as I came to discover, the current time piped text is based on the time setting of the survey creater’s qualtrics account rather than survey takers’ loca time zone. How would you resolve this using JavaScript assuming that my account is set to be using the Eastern Time? Thanks!
Userlevel 7
Badge +27
@echongsk,

You can use JS to get the local time, save the hour to an embedded variable, and use it for branching.
```
Qualtrics.SurveyEngine.addOnload(function() {
var d = new Date();
Qualtrics.SurveyEngine.setEmbeddedData("hour", d.getHours());
});
```
@TomG,

Thank you so much for your response! It works! May I also know if there's away to get the _date_ based on the local time of my survey taker as well?

Also, is the javascript getting the local time based on the IP address of the survey taker? I'm trying to see if there's a way to still get the local time of the person even if they use VPN. Thanks.
Userlevel 7
Badge +27
> @echongsk said:
> @TomG,
>
>May I also know if there's away to get the _date_ based on the local time of my survey taker as well?
It depends on the format you want, the easiest way to get detailed date/time info in a string is this:
```
Qualtrics.SurveyEngine.addOnload(function() {
var d = new Date();
Qualtrics.SurveyEngine.setEmbeddedData("date", d.toString());
});
```
> Also, is the javascript getting the local time based on the IP address of the survey taker? I'm trying to see if there's a way to still get the local time of the person even if they use VPN.
JavaScript gets the date/time from the client device where the browser is running. So, it should be correct if they are using a VPN (but not a a proxy).
@TomG,

This is very helpful. Thank you. I would like to get the date as a "date" format (e.g., yyyy-mm-dd) rather than a "string" so that I can do comparison in the logic flow. How can I achieve this? Thanks again!
Userlevel 7
Badge +27
> @echongsk said:
> @TomG,
>
> This is very helpful. Thank you. I would like to get the date as a "date" format (e.g., yyyy-mm-dd) rather than a "string" so that I can do comparison in the logic flow. How can I achieve this? Thanks again!
Once you get to needing specific date/time formats, I recommend using moment.js instead of native JavaScript. It makes it a lot easier.

Leave a Reply