Error formatting date in C # [closed]

1

I'm trying to format a date:

string s = linha.data.ToString("yyyyMMdd");

But I'm getting an error in ToString()

  

No overload for Method 'ToString' takes 1 argument

Any tips?

EDITED

In my date field, when I hover over it, it looks like this: DateTime? bpiUltrassom.data {get; set;}

I do not know why this question appears after DateTime

    
asked by anonymous 28.10.2017 / 23:59

3 answers

1

The question appears because its DateTime property may be null. And a null DateTime does not have the ToString(String) method, so it has an error.

To resolve your issue, replace your property type from DateTime? to DateTime only, or treat its value before conversion and use .Value . Ex:

string dataFormatada = (data != null? data.Value.ToString("yyyyMMdd") : "");
    
30.10.2017 / 14:35
0

Solution found:

string s = linha.data.Value.ToString("yyyyMMdd");
    
29.10.2017 / 01:30
0

It can be done as follows.

string s = linha.data.ToString().ToString("yyyyMMdd");
    
30.10.2017 / 13:16