7

I am aware of the standard procedure for displaying a DateTime in a custom format, like so:

MessageBox.Show(dateSent.ToString("dd/MM/yyyy hh:mm:ss"));

However, when I change the variable from a DateTime to a DateTime? to accept null values, I lose the definition for the ToString(string) overload. I need to use DateTime? as I am reading from a database which potentially has null values - if the field in the database has a null value, then I need to assign the variable a null value too.

So I have two questions:

1) Out of curiosity, does anyone know if there is a reason why DateTime? does not contain an overload for ToString(string)?

2) Could anyone suggest an alternative method for what I am trying to achieve?

Mike Baxter
  • 6,868
  • 17
  • 67
  • 115
  • 4
    Because it could possibly be null. What is `null.ToString()`? – Joel Etherton Apr 04 '13 at 10:52
  • 1
    `Because it could possibly be null.` is not a rhetorical question. The actual question is not rhetorical either. If you can tell me what null.ToString() I think it would very helpful. – Joel Etherton Apr 04 '13 at 11:14
  • possible duplicate of [How can I format a nullable DateTime with ToString()?](http://stackoverflow.com/questions/1833054/how-can-i-format-a-nullable-datetime-with-tostring) – Adi Inbar May 29 '15 at 00:25
  • @JoelEtherton - null.ToString() yields a compilation error because you need to call .ToString() on something where the type is inferrable, and null is not that. I think what you meant is what is x.ToString() when x is an unassigned DateTime?, in which case it calls the struct's ToString() method which returns empty string since the value is null. – user420667 Feb 22 '17 at 23:51
  • @user420667: I know the difference. The question was delivered to get OP to consider the concept more thoroughly. – Joel Etherton Feb 23 '17 at 17:14

3 Answers3

11

DateTime? is syntactic sugar for Nullable<DateTime> and that's why it don't have ToString(format) overload.

However, you can access underlying DateTime struct using Value property. But before that use HasValue to check, if the value exists.

MessageBox.Show(dateSent.HasValue ? dateSent.Value.ToString("dd/MM/yyyy hh:mm:ss") : string.Empty)
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
7

Instead of having to manually perform a null check every time, you can write an extension method.

 public static string ToStringFormat(this DateTime? dt, string format)
 {
      if(dt.HasValue) 
         return dt.Value.ToString(format);
      else
         return "";
 }

And use it like this (with whatever string format you want)

 Console.WriteLine(myNullableDateTime.ToStringFormat("dd/MM/yyyy hh:mm:ss"));
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • This is the first time I have encountered an extension method. It is perhaps a little overkill for my needs (as this nullable datetime situation is a rare encounter in my application) but your answer is elegant and has been a learning experience for me. Thanks! +1 – Mike Baxter Apr 04 '13 at 11:10
  • @Teifi - You're welcome :) Extension methods must be in a static class (I've created a new static class that contains numerous ext methods and import them into my projects). To make an ext method, it has to be static and has to have the word `this`. The first parameter is the type that will be affected (here it's `DateTime?`) So now, when you have a `DateTime?` object, you can press `.` and it will list the usual methods as well as ext ones. More info here http://msdn.microsoft.com/en-gb/library/vstudio/bb383977.aspx Useful for repeated actions on types. – keyboardP Apr 04 '13 at 11:14
1

You can still use

variableName.Value.ToString(customFormat);
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
GLlompart
  • 261
  • 5
  • 18
  • 6
    But only if you are very confident that your variable doesn't contain null at that point. – KingCronus Apr 04 '13 at 10:53
  • Yeah well, I assumed that people understands what is a nullable type – GLlompart Apr 04 '13 at 10:56
  • Yeah I can just do a `if (variableName != null)` and this method works just as well. Thank you for the workaround! +1 – Mike Baxter Apr 04 '13 at 11:11
  • I would suggest you to use `variableName.HasValue` as it performs this operation :P – GLlompart Apr 04 '13 at 11:15
  • There's no real difference, it just looks cleaner to me to use built in operations. You can read a little bit more about it here: [link](http://stackoverflow.com/questions/5233882/is-there-any-difference-between-mynullablelong-hasvalue-and-mynullablelong-nu) – GLlompart Apr 04 '13 at 11:25