question

Kim Strasser avatar image
Kim Strasser asked

How can I convert Zulu time to UTC?

I get the following Zulu time format result when I use server.BanUsers:

// resultban.BanData[0]:
Created: 2019-12-04T14:53:54.795Z
Expires: 2019-12-05T02:53:54.795Z

CloudScript:

var resultban = server.BanUsers({
        PlayFabId: currentPlayerId,
        PermanentBan : banpermanently,
        Bans : [{ DurationInHours : banhours, PlayFabId : currentPlayerId, Reason : banreason }]
        });

But in Game Manager, the ban looks like this with the UTC format:

Does PlayFab have a built-in function to convert the Zulu time and date format to UTC format?

I want to convert the time and date here in the CloudScript code so that it looks exactly like the UTC format in Game Manager. I think the UTC format is easier to understand for players. I want to display the UTC format in my game.

if (resultban.Error == null)
{
    log.info("Adding ban successful." + "Created: " + resultban.BanData[0].Created + "Expires: " + resultban.BanData[0].Expires);         
}
CloudScript
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

As far as I am aware, the Zulu time zone (Z) is the same as Coordinated Universal Time (UTC) with no offset written as UTC+0:00. Therefore, the date string returned in Cloud Script is actually very similar to the one displayed in Game Manager but with different representations, so the problem you are having now is how to convert the date string returned in Cloud Script to the one displayed in Game Manager. In order to do that, you need to get the month, day, year, hour and minute from the date string. Let’s take this Date (2019-12-04T14:53:54.795Z ) for instance, you can utilize the Date Object offered by JavaScript (Cloud Script runs on JavaScript environment), please read the following code snippet:

 
    var expireDate = "2019-12-04T14:53:54.795Z";
    var date = new Date (expireDate);
    log.info(date.getUTCMonth()); //This will return 11, which represents the month, January gives 0, so December gives 11.
    log.info(date.getUTCDate());  //This will return 4, which represents the day of the month
    log.info(date.getUTCFullYear()); //This will return 2019, which represents the year 
    log.info(date.getUTCHours());   //This will return 14, which represents the hour of the day
    log.info(date.getUTCMinutes()); // This will return 53, which represents the minutes of the day

Also note that you can even get the seconds, milliseconds using the getUTCSeconds() and getUTCMilliseconds() method, and, there are also a set of methods that can convert a date to a string, for instance, toUTCString(), however, this is not the exact format you want.

Now you can get the month, year, etc. from the date string, it would be easy for you to construct any date format you want, please let me know if you have any problems.

10 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kim Strasser avatar image Kim Strasser commented ·

How can I get the player's current profile language in CloudScript? I already set the language in the client code to German, but I don't know how to get the current language in CloudScript.

var request = new SetProfileLanguageRequest { ExpectedVersion = 0, Entity = new PlayFab.ProfilesModels.EntityKey { Id = EntityID, Type = EntityType }, Language = "de" };
var languageTask = PlayFabProfilesAPI.SetProfileLanguageAsync(request);

I want to translate the name of the month if the player's profile language is French or German, if not, then I will use the English name of the month.

0 Likes 0 ·
Citrus Yan avatar image Citrus Yan Kim Strasser commented ·

You can call entity.GetProfile() in CloudScript to get the profile language you just set in the client.

0 Likes 0 ·
Kim Strasser avatar image Kim Strasser Citrus Yan commented ·

What is entity and how can I get it in CloudScript?

0 Likes 0 ·
Show more comments
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.