Sign In Register

How can we help you today?

Start a new topic

Converting default date format to string without changing format

Hi, I'm trying to save a date to a runtime collection as a string rather than a dateTime.


my code is:

var date = new Date();

date.setDate(date.getDate() + 7);


Spark.runtimeCollection("playerPurchases").insert({

    "_id":{"$oid":Spark.getPlayer().getPlayerId()},

    "userName":userName,

    "subscribed":"Never",

    "freeTrial":date,

    "freeTitleCoupon": "0"

});


This saves the date as a $numberLong which is what I want as I use epoch2dateTime() to convert it to a DateTime in Unity. But its saving it as an embeded date rather than just a string:


  "freeTrial": {

    "$date": {

      "$numberLong": "1554601202001"

    }

  },


How do I make this save as:


"freeTrial": "1554601202001"




I have tried all of the date.toString() functions and they all change the format from the numberLong to a more readable format which I don't want.


When I:

Spark.log.debug(date);


I get the string I'm looking for.


If I'm not able to convert a gamesparks dateTime to string without changing the format, how do I read:


  "freeTrial": {

    "$date": {

      "$numberLong": "1554601202001"

    }

  },


in Unity with C#?


I've tried:


.GetGSData("freeTrial").GetGSData("$date").getString("$numberLong");


but I get a null reference on the GetGSData("$date").


Please help.


Thanks, Ian


You can save your date using date.toISOString() and then when you wanna convert it to a date again just do:


var date = new Date(Insert your date data from your collection here)


1 person likes this

Thanks for the reply @Panayiotis Milios.


I'm retrieving the saved data by calling an event in Unity which pulls in the PlayerPurchases data. This is why I need it in the Epoch format as a string rather than an embedded DateTime.


Saving as date.toISOString() doesn't save it in Epoch format. It saves it in timeStamp format.


Do you know how to save it in Epoch format?


Thanks, Ian.

There is nothing wrong with the way you are doing it. Even if it is registered as "1554601202001" with quotes, it is still interpreted as a number when reading it. So you shouldn't have any problem.

Thanks for the reply @Thorvald ter Meer


.GetGSData("freeTrial").GetGSData("$date").getString("$numberLong");


Gives me a null reference on GetGSData("$date").


So I cannot retrieve the "1554601202001" with the above code in unity.


Thanks, Ian.

I'm confused. On this example:

"freeTrial": {

    "$date": {

      "$numberLong": "1554601202001"

    }

  },

$date is not supposed to be GSData. Wouldn't .GetGSData("freeTrial").getLong() solve your issue?


If you store it as long why do you use getString to retrieve your data and not GetLong()? There is also a getDate function which i haven't used but it might solve your problem.

To handle every possible scenario, we deserialize like so

 

GSData dateData = gsData.GetGSData(typeField.Name);
if (dateData != null)
{
    if (dateData.ContainsKey("$date"))
    {
        if (dateData.ContainsKey("$date"))
        {
            GSData timeData = dateData.GetGSData("$date");
            if(timeData == null)
            {
                typeField.SetValue(obj, DateTime.Parse(dateData.GetString("$date")).ToUniversalTime());
            }
            else if (timeData.ContainsKey("$numberLong"))
            {
                string timeValue = timeData.GetString("$numberLong");
                if (!string.IsNullOrEmpty(timeValue))
                {
                    double epochTime = double.Parse(timeValue);
                    if (epochTime > 0)
                    {
                        typeField.SetValue(obj, new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(epochTime));
                    }
                }
            }
        }
        else
        {
            typeField.SetValue(obj, DateTime.Parse(dateData.GetString("$date")).ToUniversalTime());
        }
    }
}
else
{
    long time = gsData.GetLong(typeField.Name).GetValueOrDefault();
    if(time > 0)
        typeField.SetValue(obj, new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(time));
}

They are probably better ways.


1 person likes this

Hey guys @Panayiotis Milios @Thorvald ter Meer


Thanks for the replies and recommendations. I tried them in Unity and couldn't get it to work however I figured out how to save to GameSparks the way I wanted with the following code:


var date = new Date();

date.setDate(date.getDate() + 7);

var longDate = (date.getTime());

var longDateString = longDate.toString();

playerPurchasesList.update({"_id":{"$oid":playerPurchases._id.$oid}}, {$set:{"freeTrial" : longDateString}});


The trick was to use date.getTime() this returns a long in epoch format which I could then convert to a string and save to the "freeTrial" key.


So the data in explorer is now saved as I wanted;

freeTrial: "1555042848110"


Thanks again for your time and suggestions, I appreciate it!


Ian

Login to post a comment