Sign In Register

How can we help you today?

Start a new topic

Unity users, what's your method for saving and retrieving dates?

I'm having trouble getting a consistent method for the saving and retrieving of DateTimes in Unity and can't find many examples and the few I did find don't consistently work.

I was wondering if anyone has had any good tips to share on this? Do you use UTC time? Do you convert it to local time in unity? How do you replace it and keep the format consistent? etc

I've been using this method for a while and its worked but for some reason, tracking new times doesn't want to work this way anymore.

Any sharing is helpful.

1 person has this question


It depends on the form of the date that you want, and how are you going to use it. 

If you want to use the time to assign timestamps to messages sent between players and/or server, then you probably need the time to be in milliseconds format, for me, I'm retrieving the milliseconds this way(C#):

(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds

 which return milliseconds in double type, you can convert it to long.

Do you use the "GetDate" function the GameSparks unity API provides?

You mean on the Real-time script? I use: 

new Date().getTime()

 to get the time on the server as milliseconds.

I don't know about the GetDate function,

On my current game, I save times as UTC in gamesparks using these functions

function getDateNow() {
    var now = new Date();
    return new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()).getTime();

function dateAdd(date, interval, units) {
    var ret = new Date(date); //don't change original date
    switch (interval.toLowerCase()) {
        case 'years':
            ret.setFullYear(ret.getFullYear() + units);
        case 'quarters':
            ret.setMonth(ret.getMonth() + 3 * units);
        case 'months':
            ret.setMonth(ret.getMonth() + units);
        case 'weeks':
            ret.setDate(ret.getDate() + 7 * units);
        case 'days':
            ret.setDate(ret.getDate() + units);
        case 'hours':
            ret.setTime(ret.getTime() + units * 3600000);
        case 'minutes':
            ret.setTime(ret.getTime() + units * 60000);
        case 'seconds':
            ret.setTime(ret.getTime() + units * 1000);
            ret = undefined;
    return ret.getTime();

the times in the server end up as doubles.

public static DateTime getDateTimeFromGameSparks (GSData result, string dataKey) {
    double milliseconds = result.GetDouble(dataKey).value;
    DateTime valueToReturn = new DateTime();
    valueToReturn = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(milliseconds);
    return valueToReturn;

 On my unity client, I use this to retrieve the DateTime converted to local time

But it honestly doest work consistently for me. Sometimes it ends up as the beginning of time.

Haven't used the Real-time script yet though

No GetDate is a function GameSparks provides on the client side in unity for retrieving values as a DateTime class. Seemed handy to me but I never had any luck with it.

Can you tell me in which class/namespace this function exist in?

Because I haven't found it in GameSparks unity API

ya its in the GSData class


Anyone have luck with GetDate() method? I am trying to retrieve date from GameSparks in Unity with no luck. Here is what I have in database: 

"LastLoginDate": {"$date": { "$numberLong": "1497036956781" }

the JSON returned to Unity looks like this  :


Strangely the milliseconds are converted to some time format when returned to unity, which I am ok with but now I cannot get the date with any method in GSData.

GetData() does not work

GetLong() does not work

GetDouble() does not work

I simply cannot retrieve the date no matte what. Any suggestions?

A related question:

In my c# script, I have:

GSData scriptData = obj.ScriptData;

DateTime time = scriptData.GetDate("myParam").Value;

But I get the error:

InvalidOperationException: Nullable object must have a value.

System.Nullable`1[System.DateTime].get_Value () (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Nullable.cs:108)

(relating to the second line)

When I debug, the scriptData has a string with the listed param as a key with a value.

What am I doing wrong?

The alternative which works, seems a hacky way to go about it...

GSData scriptData = obj.ScriptData;

double date = (double)scriptData.BaseData["myParam"];

myDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(date);

Login to post a comment