Alexander
Alexander

Reputation: 20234

DateTimeOffset?.ToString(string format)

I have a DateTimeOffset? object which I want to format according to some datetime format.

I found that type DateTimeOffset has ToString(String,IFormatProvider) method:

http://msdn.microsoft.com/de-de/library/bb351892(v=vs.110).aspx

But neither does this exist for an object of type DateTimeOffset?, nor can I find specifics on this type, or how to convert it.

What is DateTimeoffset?, and how do I convert it to a string using custom format?

Upvotes: 2

Views: 2858

Answers (2)

D Stanley
D Stanley

Reputation: 152594

DateTimeOffset? is the same as Nullable<DateTimeOffset> which has a .Value property to get the underlying DateTimeOffset value:

DateTimeOffset? dto;
...
string s = dto.Value.ToString(String,IFormatProvider);

Note that you should check to make sure the nullable DateTimeOffset has a value before calling .Value, otherwise you'll get a NullReferenceException:

DateTimeOffset? dto;
...
if(dto.HasValue)
    string s = dto.Value.ToString(String,IFormatProvider);

Upvotes: 6

kat0r
kat0r

Reputation: 949

DateTimeoffset? is a nullable version of DateTimeoffset, nothing more. You can access its value using .Value.
http://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx has more detailed information.

Upvotes: 1

Related Questions