Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

custom c# calender or DateTime / Date?

Discussion in 'Getting Started' started by Abelabumba, Feb 24, 2015.

  1. Abelabumba

    Abelabumba

    Joined:
    Jan 14, 2015
    Posts:
    45
    So I made a calendar script for my project:

    Code (CSharp):
    1. public static string date;
    2.     int year = 1984;
    3.     string[] month = new string[12] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
    4.     int monthNumber = 0;
    5.     int day = 1;
    6.  
    7.     void Update () {
    8.         date = (day + "." + month[monthNumber] +" "+ year);
    9.     }
    10.  
    11.     public void AddOneDay()
    12.     {
    13.         day++;
    14.  
    15.         if (monthNumber == 1 && day == 29)
    16.         {
    17.             day = 1;
    18.             monthNumber++;
    19.         }
    20.         else if (day == 31 && (monthNumber == 3 || monthNumber ==  5 || monthNumber == 8 || monthNumber == 10))
    21.         {
    22.             day = 1;
    23.             monthNumber++;
    24.         }
    25.         else if (day == 32 && monthNumber == 11)
    26.         {
    27.             year++;
    28.             day = 1;
    29.             monthNumber = 0;
    30.         }
    31.         else if (day == 32)
    32.         {
    33.             day = 1;
    34.             monthNumber++;
    35.         }
    36.     }
    37.  
    38. }
    Which does what I want, except there are no leap years but they're not that important to me, I doubt anyone will notice / care in the context of my game. I have two questions, actually:

    - How could I make the script better / shorter / more elegant?
    - An advantage in using the DateTime thing? And if so, how would I do that? Google mostly shows stuff about system time, which I don't need. I've played with it a bit but didn't get very far before I began to question if there was any sense to it...
     
  2. Deleted User

    Deleted User

    Guest

    Code (csharp):
    1. using UnityEngine;
    2. using System;
    3.  
    4. public class Calendar : MonoBehaviour
    5. {
    6.  
    7.     private DateTime _date = new DateTime( 1984, 1, 1 );
    8.  
    9.     public void AddDay()
    10.     {
    11.         _date = _date.AddDays( 1 );
    12.     }
    13.  
    14.     public override string ToString()
    15.     {
    16.         // for formating see https://msdn.microsoft.com/de-de/library/zdtaw1bw(v=vs.110).aspx
    17.         return _date.ToString("dd.MM.yyyy");
    18.     }
    19. }
    Thats how I would have done it
     
  3. Abelabumba

    Abelabumba

    Joined:
    Jan 14, 2015
    Posts:
    45
    Thanks; that's much cleaner indeed. I suppose there is no easy way to have it show 15th February, 3rd March etc, as it's not an option on the page you linked? Could probably do something with ifs again, if day = 3 or 23 put rd at the end, if it's 2 or 22 nd and so on.
     
  4. Deleted User

    Deleted User

    Guest