Data Directive. Trying to set a date format. Internet says I can do this with a format in quotes, inside the ToString().
What am I missing?
Data Directive. Trying to set a date format. Internet says I can do this with a format in quotes, inside the ToString().
What am I missing?
Best guess is the Date01 is nullable. This will get thrown if that’s the case.
try
ttUD08Row.Date01?.ToString("yyyy-MM-dd")
I’ve overcome limitations by the nullable DateTime by adding a .Value after the Date01. You can pair it up with a check to make sure it’s not null before doing this to avoid the null exception errors. Does the Date01? work also? I’ve never tried it like that.
If you do the Date01?.ToString()
… it will skip the part after the “.” and just return null if there isn’t a value assigned to Date01.
Nice! I love it. Thanks for the tip. I’ll try that out next time.
Cast it to a DateTime
((DateTime)ttUD08Row.Date01).ToString("yyyy-MM-dd");
Above is the reason why you have to force cast it.
Perfect, thank you. This works well.
Sorry I posted and ran yesterday.
If your date is null you will get an Nullable object must have a value.
error. If you use the ?. then it will just return null.
OK, I’ll definitely try to remember that.
In this specific case, though, it’s populating Key1 based on a mandatory Date01. So I should always have a value that is being parsed.
Now, I am thinking, what if they change Date01 after making the new record? Ugh. I guess I now need to block them from changing Date01.
Anyhoo, thank you all for the tips.
Another option is to use string interpolation:
$"{ttUD08Row.Date01:yyyy-MM-dd}"
I prefer formatting strings this way, because it’s usually much more compact.