Having this line of code:
DateTime inFutureDateTime = DateTime.Now.AddSeconds(60);
And writing in the (Unity) console like this
Debug.Log("In future date time " inFutureDateTime.Date)
outputs:
12/15/2021 12:00:00 AM
The Date is correct (today), but the time isn't. It should be CurrentTime 60 seconds.
What am I doing wrong, or did I misunderstand how the DateTime works.
Thanks in advance
CodePudding user response:
instead of .Date
, try calling .ToLongDateString()
as in Debug.Log("In future date time " inFutureDateTime.ToLongDateString())
CodePudding user response:
.Date
is a holder intended for Date only.
Unfortunately when converted to string it displays a time component as well.
The default ToString, or one of it's overrides will display the full date time:
Debug.Log("In future date time " inFutureDateTime)
CodePudding user response:
Date return component date not date format standard.
DateTime inFutureDateTime = DateTime.Now.AddSeconds(60);
Debug.Log("In future date time " inFutureDateTime.Date.ToLongDateString())
CodePudding user response:
The origin of the problem is in the .Date
: it returns Date part of the initial DateTime
value
(note, that time part is 0:0:0.000
). I suggest using string interpolation where you can specify
text, variables, their formats, etc.
// Here we specify the required format - MM/dd/yyyy HH:mm:ss
Debug.Log($"In future date time {inFutureDateTime:MM/dd/yyyy HH:mm:ss}");
If you want to use the default format (from CultureInfo.CurrentCulture
):
Debug.Log($"In future date time {inFutureDateTime}");