- Home /
This is not a Unity question but a pure C# related one, and thus, does not belong to Unity Answers.
error CS0019: Operator `-' cannot be applied to operands of type `System.DateTime' and `string'
What the heck does this mean:
error CS0019: Operator -' cannot be applied to operands of type
System.DateTime' and `string'
I'm trying to use it like this:
public static string GetDaysPassedQ01()
{
LoadInformation.LoadStartDateQ01 ();
Debug.Log(StartDateQ01);
today = System.DateTime.Now;
System.DateTime compareDate = today; //.AddDays(0);
System.TimeSpan difference;
difference = compareDate - today;
difference = compareDate - StartDateQ01;
//difference = today - startDate;
Debug.Log("Days Passed: " + difference.Days);
return difference.Days.ToString();
}
NOTE: The Debug.Log(StartDateQ01); shows this:
01/05/2017 14:04:48 UnityEngine.Debug:Log(Object)
Answer by Creeper_Math · Jan 05, 2017 at 10:13 PM
difference = compareDate - today;
difference = compareDate - StartDateQ01;
That's where the error is going on.. "today" and / or "StartDataQ01" is being stored in a string, so what the computer thinks your doing is taking a number and subtracting some text from it, which isn't possible....
My suggestion is to make sure that they all are being stored in the same variable way, and if you want to subtract them, you will have to turn them into System.DateTime (if you can use operators with those)...
What I would do (which is most likely the least energy efficient way) is to convert both bools into numbers 1 - 366 * year , and then after subtracting what I need, I would just convert them to the regular form... Which itself would take some extensive coding
ell in my save script I'm doing this:
public static void SaveStartDateQ01(){
UserInformation.startDate = System.DateTime.Now ; //save the start date ->
PlayerPrefs.SetString ("DateAsked", UserInformation.StartDateQ01);
Debug.Log("Saving The Date You Asked This $$anonymous$$ichael " + (System.DateTime.Now));
}
And in my load script i'm doing this:
public static void LoadStartDateQ01(){
UserInformation.StartDateQ01 = PlayerPrefs.GetString ("DateAsked");
Debug.Log("You Asked $$anonymous$$e This Question On This Date $$anonymous$$ichael " + PlayerPrefs.GetString ("DateAsked"));
}
All I really need is to uses this information in an if statement to check how many days has passed. something like if (daysPassed <= 1){
Do something
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
How to Call a Number Using WWW and Actually Do Something With The Number (Please Read) 1 Answer
GetHashCode() questions/Turning a text string into an int 1 Answer
Can I create a list with an int/float and a string? C# 2 Answers
Changing a GUI String to read as a INT 2 Answers