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.
  2. Dismiss Notice

Question if Date - 100% working but I hate it!

Discussion in 'Scripting' started by Calumniator, Jul 20, 2023.

  1. Calumniator

    Calumniator

    Joined:
    Jul 5, 2023
    Posts:
    24
    I want to to have something that only appears on Halloween. So my "solution" is:

    Code (CSharp):
    1. public Text datetext;
    2. public int day;
    3. public int month;
    4.  
    5. DateTime date = DateTime.Now;
    6. datetext.text = date.ToShortDateString();
    7. day = Int32.Parse(datetext.text.Substring(0, 2));
    8. month = Int32.Parse(datetext.text.Substring(3, 2));
    9.  
    10. if ((day == 31) && (month == 10))
    11. {
    12.     // Do things on Halloween!
    13. }
    It works but I hate it. There has to be a better way, a way without the detour via text but I'm to stupid to figure out a way to extract day and month directly. How can it be done?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Perhaps by looking in the documentation for
    System.DateTime
    ?

    I mean you weren't expecting US to look for you, where you? :)
     
    orionsyndrome, lordofduct and Bunny83 like this.
  3. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    729
  4. Calumniator

    Calumniator

    Joined:
    Jul 5, 2023
    Posts:
    24
    Why trolling? I clearly have already read the documentation and my "problem" had nothing to to with this. It was nothing but a severe brain lag. I even posted my code. A simple: "Why not simply use a string instead of the text?" for example would have been already good enough as an answer but this READ THE F***ING MANUAL attitude is just rude and not helpful at all.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    You don't need more than this:
    Code (csharp):
    1. DateTime date = DateTime.Now;
    2. if (date.Day == 31 && date.Month == 10)
    3. {
    4.     // Do things on Halloween!
    5. }
     
    lordofduct, Ryiah and Calumniator like this.
  6. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    That dude is the most liked person on this forum (officially, there is literally a roster) with nearly 35K posts. If you're dyslexic or can't use Google in 2023, you can at least show some humility. You're trolling yourself, if you're acting like we're your enemies.

    The point of Kurt's answer is that if you can't be bothered with reading the most basic of the basic manuals, don't expect other people to read it for you. Why would you expect this? Go do something else with your life.
     
    Bunny83 likes this.
  7. Calumniator

    Calumniator

    Joined:
    Jul 5, 2023
    Posts:
    24
    @orionsyndrome:
    "acting like we're your enemies" The lost art of exaggeration is strong in this one. ^^ And as I said before I read the documentation before but had a severe brainlag that's why I said: "but I'm to stupid to figure out a way to extract day and month directly".

    @CodeSmile:
    That's way shorter than what I ended up with. Thanks a lot!
     
  8. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    Alternatively:
    Code (csharp):
    1. var halloweenDate = new DateTime(DateTime.Now.Year, 10, 31);
    2. if (DateTime.Now.Date == halloweenDate.Date)
    3. {
    4.     // Stuff
    5. }
    6.  
    You can make it a field of a class too:
    Code (csharp):
    1. private DateTime HalloweenDate = new DateTime(DateTime.Now.Year, 10, 31);
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    The trolling is entirely in your own mind my friend.

    I am telling you "I know there's a way but I don't know what it is because I don't remember silly stuff like that, but I know where to look. You should go look there too and you'll find it if you've ever programmed and read a documentation page."

    I don't remember stuff like the fact that the fields even exist on the DateTime object instance itself. I've worked with so many date/time APIs through the years, if I bothered to remember details of how they all work my brain would have exploded by now.
     
  10. Calumniator

    Calumniator

    Joined:
    Jul 5, 2023
    Posts:
    24
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Actually, hippocoder is the most liked. :D
     
  12. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    While @Ryiah has a good example post. So did @CodeSmile.

    I think it was best to direct OP towards the documentation as was done early on. This is a simple coding issue that can be resolved with very little effort.

    A teach a man to fish moment, rather than a give a man a fish.

    Truth has been spoken.
     
    DragonCoder, orionsyndrome and Yoreki like this.
  13. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    Okay, I have to confess, I asked ChatGPT. I was provided with an example, I looked up the relevant parts in the docs, verified it ran with Try .NET (https://try.dot.net/), made the date a field and verified that, and posted it.
     
    Last edited: Jul 20, 2023
    MushyAvocado and lordofduct like this.
  14. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    LOLLLLLLLL

    See @Calumniator, you could have just asked AI to go fishing for you.
     
    orionsyndrome likes this.
  15. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Sure, right, Kurt has the most messages :) and hippocoder is the most liked. Well, who doesn't like coding hippos!
     
  16. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Dude, you need to learn what trolling means. It's not something you casually say to your friends. Unless you're just being sarcastic. But it doesn't come across that way, so no, I'm not exaggerating, you're hardcore antagonistic. I/we don't care if you consider yourself stupid -- and that's btw a very detrimental position one could have -- you need to read what is written EXACTLY and discern the intention behind it.

    You had one job. And when you failed that, asking for help is another job that you've failed to do properly. How much worse do you want it to get?
     
    Bunny83 and Kurt-Dekker like this.