I think there is a bit of a confusion here what the string concatenation does to your values.
It is nothing less than simply using the default format for a parameterless ToString()
!
Each of your strings like
Debug.Log("currentTime: " + currentTime);
basically internally implicitly equal
Debug.Log("currentTime: " + currentTime.ToString());
In that case the ToString()
without parameters can be omitted since c# already uses it implicitly.
=> You are using string
in all your examples! How else do you think anything is logged (as a string!) to the console? 😉
As noted in the comments a DataTime
is just a point in time and basically just a kind of wrapper around a long
which are the system ticks passed since the Unix Epoch.
All the other properties, values and formats are calculated based on those ticks on demand (which makes it quite expensive btw).
Is there a way to get “DateTime.Now” formatted as “dd.MM.yyyy HH:mm:ss”
Yes, exactly how you did it.
So what you want to do is just use DateTime
for all your calculations and AddMinutes
etc. and then only when you need to display the (current) value you simply pass in the correct format just like you did in your first line
const string format = "dd.MM.yyyy HH:mm:ss";
and
Debug.Log("currentTime: " + currentTime.ToString(format));
Update
Now finally knowing your actual code and issue
There is no reason to use parse here … what you want is
TimeSpan totalTime = DateTime.Now - lastTime;
and then Debug.Log(totalTime.ToString(YOUR_DESIRED_FORMAT));
2
solved Format DateTime without converting it to a string in c#? [closed]