Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by KnightRiderGuy · Jan 07, 2017 at 05:45 PM · c#timedatetimedaycycledate

Looking For A Better Way To Check If Day Has Passed

Currently I'm using this method to check to see if a day has passed since the user has asked a certain question. My method is working fine for the shortTimePassed, BUT is not accurate enough for the dayHasPassed, Since it is making a comparison based on the last time the question was asked and then the current time, so if you asked the question later in the day it would not be until later that same time the next day that it would consider a day has passed which is not what I want. Is there another way to handle the dayHasPassed, ??

This is my code snippet:

 DateTime currentDate;
     DateTime oldDate;
 
     public static bool dayHasPassed = false;        // "Are You A Spaceship?" One Day Passed
     public static bool shortTimePassed = false;        // "Are You A Spaceship?" Short Time Passed
 
     public static bool dayHasPassedQ02 = false;        // "Are We Friends?" One Day Passed
     public static bool shortTimePassedQ02 = false;    // "Are We Friends?" Short Time Passed
 
     public static bool dayHasPassedQ03 = false;        // "?" One Day Passed
     public static bool shortTimePassedQ03 = false;    // "?" Short Time Passed
 
 
 
     void Start()
     {
         Q01 ();        // "Are You A Spaceship?"
         Q02 ();        // "Are We Friends?"
     }
 
 
     // Q01 "Are You A Spaceship?"
     public void Q01(){
         //Store the current time when it starts
         currentDate = System.DateTime.Now;
 
         //Grab the old time from the player prefs as a long
         long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
 
         //Convert the old time from binary to a DataTime variable
         DateTime oldDate = DateTime.FromBinary(temp);
         print("oldDate: " + oldDate);
 
         //Use the Subtract method and store the result as a timespan variable
         TimeSpan difference = currentDate.Subtract(oldDate);
         print("Difference: " + difference);
 
         if (difference.Days == 1) {
             Debug.Log ("One Day Has Passed From old Date That You Asked If I Was A Spaceship");
             dayHasPassed = true;
         }
         // Either 0 Days Or More Than 2 Days
         if (difference.Days <= 0 || difference.Days >= 2) {
             dayHasPassed = false;
             Debug.Log ("At Least One Day Has NOT YET Passed From old Date That You Aksed If I Was A Spaceship");
         } 
         if (difference.Minutes <= 20){
             Debug.Log ("It Hasn't Even Been 20 Minutes Since You Aksed If I Was A Spaceship");
             shortTimePassed = true;
         }
         if (difference.Minutes >= 21){
             Debug.Log ("More Than 20 Has Passed Since you Aksed If I Was A Spaceship");
             shortTimePassed = false;
         }
     }


Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Jan 07, 2017 at 06:03 PM 0
Share

" if you asked the question later in the day it would not be until later that same time the next day that it would consider a day has passed "

The sounds like the correct definition of a "day" to me, but is not what you want? Please elaborate on what your loooking for. Perhaps you want to see if the time-span crosses midnight? if so, just use the date property https://msdn.microsoft.com/en-us/library/system.datetime.date(v=vs.110).aspx to el$$anonymous$$itate the time elements. Then just compare the resultant midnight-dates. https://msdn.microsoft.com/en-us/library/system.datetime.op_lessthan(v=vs.110).aspx

avatar image KnightRiderGuy Glurth · Jan 07, 2017 at 07:16 PM 0
Share

@Glurth, Thanks, well I'll try to elaborate, but I thought I was clear ;) Think about it from a conversation aspect, If I ask you some thing and then the next day ask you the same thing you might well naturally respond with: "That's what you said yesterday"

But you would not likely say that if it was not the next day.

avatar image Glurth KnightRiderGuy · Jan 07, 2017 at 07:27 PM 0
Share

Ah, so you want to see if the "Date" has changed. That's certainly different from the passage of a day (24hrs). Yeah, just remove the time components, before you compute the difference, as I suggested above; that should do it.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KnightRiderGuy · Jan 07, 2017 at 09:43 PM

Thanks @Glurth, I'll experiment a bit with that tomorrow and let you know how it goes, got company over now so I'll have to play about with it tomorrow :)

I Had A little time to experiment.... When I changed the code with the new blocks you provided it Reded out the .Date() parts?

 public void Q01(){
         //Store the current time when it starts
         currentDate = System.DateTime.Now;
         //currentDate = System.DateTime.Now.Date();
 
         //Grab the old time from the player prefs as a long
         long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
 
         //Convert the old time from binary to a DataTime variable
         //DateTime oldDate = DateTime.FromBinary(temp).Date();
         DateTime oldDate = DateTime.FromBinary(temp);
         print("oldDate: " + oldDate);
 
         //Use the Subtract method and store the result as a timespan variable
         TimeSpan difference = currentDate.Subtract(oldDate);
         print("Difference: " + difference);
 

Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Glurth · Jan 08, 2017 at 05:03 PM 0
Share

oops my bad. Leave out the parens https://msdn.microsoft.com/en-us/library/system.datetime.date(v=vs.110).aspx

avatar image KnightRiderGuy Glurth · Jan 08, 2017 at 06:39 PM 0
Share

@Glurth, Thanks man, I think that might have fixed the next day issue but I'm not so sure about my tracking for if 20 $$anonymous$$utes has passed for the shortTimePassed bool change

 // Q01 "Are You A Spaceship?"
     public void Q01(){
         //Store the current time when it starts
         //currentDate = System.DateTime.Now;
         currentDate = System.DateTime.Now.Date;
 
         //Grab the old time from the player prefs as a long
         long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
 
         //Convert the old time from binary to a DataTime variable
         DateTime oldDate = DateTime.FromBinary(temp).Date;
         //DateTime oldDate = DateTime.FromBinary(temp);
         print("oldDate: " + oldDate);
 
         //Use the Subtract method and store the result as a timespan variable
         TimeSpan difference = currentDate.Subtract(oldDate);
         print("Difference: " + difference);
 
         if (difference.Days == 1) {
             Debug.Log ("One Day Has Passed From old Date That You Asked If I Was A Spaceship");
             dayHasPassed = true;
         }
         // Either 0 Days Or $$anonymous$$ore Than 2 Days
         if (difference.Days <= 0 || difference.Days >= 2) {
             dayHasPassed = false;
             Debug.Log ("At Least One Day Has NOT YET Passed From old Date That You Aksed If I Was A Spaceship");
         } 
         if (difference.$$anonymous$$inutes <= 20){
             Debug.Log ("It Hasn't Even Been 20 $$anonymous$$inutes Since You Aksed If I Was A Spaceship");
             shortTimePassed = true;
         }
         if (difference.$$anonymous$$inutes >= 21){
             Debug.Log ("$$anonymous$$ore Than 20 Has Passed Since you Aksed If I Was A Spaceship");
             shortTimePassed = false;
         }
     }

avatar image Glurth KnightRiderGuy · Jan 08, 2017 at 07:17 PM 1
Share

That makes sense, chopping of the time will do that. The solution is simply to use TWO "differences": one with the time chopped off, one without. e.g.

 currentDate = System.DateTime.Now;
 DateTime oldDate = DateTime.FromBinary(temp);
 TimeSpan fullDifference = currentDate.Subtract(oldDate);
 TimeSpan dateOnlyDifference = currentDate.Date.Subtract(oldDate.Date);


Show more comments
avatar image KnightRiderGuy Glurth · Jan 11, 2017 at 06:58 PM 0
Share

@Glurth, I just realized something that seems to happen. It seems that because I am calling my questions from another script object that if it is the very first time the question has ever been asked it gives an error, probably because it can't find a time value that has been saved.... is there some sort of fix for that?

avatar image Glurth KnightRiderGuy · Jan 11, 2017 at 07:52 PM 0
Share

Sounds like you need to check if this is your first question, before you call the code above. You don't want to compare times with the previous question, because if it's the first question, there IS no previous question. In this case you still will want to store the question's time-asked, but NOT do a comparison first.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

266 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

DateTime.Today for Different Time Zone 1 Answer

Getting the time from a server 2 Answers

TimeZone Not Found Exception 3 Answers

Wait time after coroutine's wait seconds is complete 0 Answers

how to display stopwatch ss\fff as string? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges