I am trying to format a datetime to a string of the form "c", by using the ToString method:
string end = evt.End.DateTime.ToString("c", DataController.culture);
DataController.culture is a reference to a valid culture object and evt is an Event object from the Google Calendar v3 API. I am sure DateTime is of the DatetTime class because the debugger says so in the type of the object.
However I keep getting the issue mentioned in the title, why? Even the MSDN docs show how to use that method but somehow I can't:
Console.WriteLine(date1.ToString("d MMMM",
CultureInfo.CreateSpecificCulture("es-MX")));
// Displays 29 agosto
The API documentation indicates that the property you're attempting to ToString()
is actually a System.Nullable<DateTime>
, which has different usability considerations from a normal DateTime
. In order to invoke the ToString
method you wish, you need to retrieve a concrete DateTime
struct from the nullable one provided to you - in your case, by doing the following:
string end = evt.End.DateTime.Value.ToString("c", DataController.culture);
Mind you, you normally will want to first check that your nullable object (eg. evt.End.DateTime
) actually has a value by checking the HasValue
property before invoking ToString()
or any other method or property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With