Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to store date that I perform an event?

Discussion in 'Scripting' started by jcfrizz, May 4, 2018.

  1. jcfrizz

    jcfrizz

    Joined:
    Jul 22, 2017
    Posts:
    11
    Hi everyone, so in game during runtime, I add items to a list. I want the date that the item was added to the list to be displayed and stored, so that it never changes. I know how to get the current date, using DateTime.now, but will that stay at that date or will it reset when the time or day changes? My goal is for the date that the item was added to the list to NOT ever change. Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    There are formatting commands to get the time out in any format you like, then store that as a string.

    If you ever want to use the date again (i.e, do meaningful computations with it), take a look at other ways to store it in the System.DateTime docs for C#.
     
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    DateTime values don't change. Internally they represent how much time has elapsed since 12:00:00 midnight, January 1, 0001. (https://msdn.microsoft.com/en-us/library/system.datetime.ticks(v=vs.110).aspx) If you're storing these values in an in-memory collection, you don't have to worry about them changing.

    Are you planning on storing this date in a file or in a database of some kind? Databases generally have ways to represent dates unambiguous. To store a date in a file you'll need to convert it into a string.
     
  4. jcfrizz

    jcfrizz

    Joined:
    Jul 22, 2017
    Posts:
    11
    Ah okay thanks, I didn't realize the values don't change. Thanks for clearing that up! And I am displaying the date on my UI page. It works now!