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;
}
}
" 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
@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.
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.
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);
oops my bad. Leave out the parens https://msdn.microsoft.com/en-us/library/system.datetime.date(v=vs.110).aspx
@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;
}
}
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);
@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?
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.
Your answer
Follow this Question
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