6

Had the following issue when created universal app in VS 2015 and tried to use .ToShortDateString() method for DateTime in the Shared Project. Visual Studio 2015 intellisense shows this as an error, but the application runs fine. Just wondering, is this a bug in VS or am I missing something?

enter image description here

enter image description here

3
  • 2
    You could always use .toString("d"), which does the same thing. May 17, 2016 at 0:11
  • Thanks mate, error's gone and got the same result. May 17, 2016 at 0:34
  • Hey, idk why, but i added questionmark, like dateToDisplay?.ToShortDateString, and for gods sake, for some reason it worked. Why it doesn't result in alert...? X) Sep 10, 2021 at 14:08

3 Answers 3

12

UWP apps use the new CoreCLR for compilation. The CoreCLR does not support DateTime.ToShortDateString at this time, and it doesn't seem to be getting much traction anyways. Those helper methods are bad at localization.

You can simply replace the call with dateToDisplay.ToString("d").

Update

Looks like it got some traction! This method (along with several others) was added back into the CoreCLR. This move was done to improve parity with the APIs in Xamarin and the .Net Framework. You can read more about this decision here.

1
  • Problem still exists with .forms 3.1 and VS2017. I don't use the UWP project, but also the Android project is not to compile (of cause the errors to the UWP project) - strange wise not by every attempt (seems to be an error in VS). Changed to .ToString("d") and works… (for all platforms) Aug 15, 2018 at 10:50
12

Change the code to:

var test = dateToDisplay.Value.toShortDateString();
0

This is only an error because code analysis is told to treat this as an error. You can change this to a warning or ignore completely, but it is recommended to improve your code.

1
  • 1
    Thanks for the answer mate. Wrote this code as a sample in VS 2015 to test. Although I didn't get the error in VS 2013. May 17, 2016 at 0:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.