Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Cloud saving DateTime error?

Discussion in 'Cloud Save' started by VagelisGardikis, Mar 18, 2022.

  1. VagelisGardikis

    VagelisGardikis

    Joined:
    Apr 24, 2018
    Posts:
    20
    Hello. I want to store a date time on the cloud save and then retrieve it from another script.
    I have tried many different combinations etc but i cant seems to make it work.
    My best try so far is this:

    Code (CSharp):
    1. await CloudSaveManager.instance.SaveSomeData("LastStartPettingDate", lastPetDate.ToString(@"yyyy-M-d\THH:mm:ss.fff\Z"));
    This saves the datetime as : "2022-3-18T13:42:28.635Z" and it is correct.

    the problem is when i try to retrieve it

    Code (CSharp):
    1. DateTime lastPetDate;
    2.             DateTime.TryParseExact(savedValue, @"yyyy-M-d\THH:mm:ss.fff\Z", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out lastPetDate);
    The datetime i get is always : 1/1/0001 12:00:00 am

    What am i missing? I know the problem is in format probably.
    Other approaches return an error saying string format was not recognized as a valid Datetime
    I have tried Convert.ToDatetime, parse, parseExact.
     
  2. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    You are not showing the code where you retrieve it, only where you parse it. Try directly outputting the value before parsing, as a test:

    Debug.Log("Saved value = " + savedValue.ToString());
     
  3. VagelisGardikis

    VagelisGardikis

    Joined:
    Apr 24, 2018
    Posts:
    20
    I tried this debug log and the output value is the correct..

    Code (CSharp):
    1. Dictionary<string, string> savedDataPetDate = await SaveData.LoadAsync(new HashSet<string> { keyName });
    2.             string savedValue = savedDataPetDate["LastStartPettingDate"];
    3.  
    4.             Debug.Log("saved value: " + savedValue);
    5.  
    6.             DateTime lastPetDate;
    7.             DateTime.TryParseExact(savedValue, @"yyyy-M-d\THH:mm:ss.fff\Z", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out lastPetDate);
     
  4. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    So your parsing looks to be incorrect, the data is saved and retrieved correctly.
     
  5. VagelisGardikis

    VagelisGardikis

    Joined:
    Apr 24, 2018
    Posts:
    20
    The error im getting is string format was not recognized as a valid Datetime .
    I tried many different formats to parse it but i cant get it to work.

    When i did the same thing in instance data of an economy item, it worked perfectly. Now im trying to do the same on cloud save..
     
  6. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    So it's not related to cloud save. Your original message stated: "The datetime i get is always : 1/1/0001 12:00:00 am" but now you claim to have an error, what changed? At any rate, you might have better luck in the Scripting forum. Mention you have a string like 2022-3-18T13:42:28.635Z and you want to format it like you need.
     
  7. VagelisGardikis

    VagelisGardikis

    Joined:
    Apr 24, 2018
    Posts:
    20
    When i do TryParseExact i get the 1/1/0001.
    When i do all the rest ways, i get the error.
    Since it worked on item instance data, i thought the problem was in cloud save.

    Could the problem be that item instance data uses Dictionary<string,object> (where my code worked!)
    and teh cloud save uses Dictionary<string,string>?

    If you think that its not a cloud save problem, i will create a thread in the Scripting forum.
    Thank you for your time in advance.
     
  8. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    If you hard coded the string and attempted to parse it, you would see the same behavior, so not related to cloud save. You would want share the hard-coded example in the Scripting forum We are saving and retrieving as expected with Cloud Save.
     
  9. VagelisGardikis

    VagelisGardikis

    Joined:
    Apr 24, 2018
    Posts:
    20
    Actually i hardcoded the exact same string i retrieved from cloud save and it worked.
    After, when i passed the same value straight out of cloud save, it didnt work.
    this doesnt make sense to me.
    Thats why i cant figure this out.
     
  10. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    Sorry I don't follow. You said earlier that Debug.Log shows the same value retrieved as what you saved. Is that not the case? Please do the following:

    1) debug log your hardcoded string
    2) debug log the parsed output of this string
    3) debug log your retrieved string (which should be the same as (1) according to your earlier mention)
    4) debug log the parsed output of this retrieved string

    Also share the code you are using for this test.
     
  11. VagelisGardikis

    VagelisGardikis

    Joined:
    Apr 24, 2018
    Posts:
    20
    WOWOWOWOW
    You are great. You helped with an issue i have been trying to solve for over 7 hours.
    when i printed the value and the hardcoded value the result was:
    hardcoded value: stringValue
    saved value : "stringValue"


    so i used savedValue = savedValue.Replace("\"", "");
    and it works now correctly.
     
    JeffDUnity3D likes this.