C# Nullable To String: 9 Answers
C# Nullable To String: 9 Answers
careers 2.0
search
Questions
Tags
Users
Badges
Unanswered
Ask Question
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
registration required.
C# Nullable<DateTime> to string
asked 4 years ago
viewed 7571 times
active 2 years ago
19
c# datetime
9 Answers
active
oldest
votes
30
I have a DateTime? variable, sometimes the value is null , how can I return an empty string ""
when the value is null or the DateTime value when not null ?
Thank you , exactly what I was looking for JL. Sep 18 '09 at 17:10
5 What!? You can just call .ToString() on the Nullable<DateTime> instance to get String.Empty. Even Eric
Lippert (who might have even implemented this behavior) notes this. That should be the accepted answer.
codekaizen Jul 9 '10 at 1:48
1 @codekaizen - I get an exception when I try that. So no, that would not be the accepted answer. Perhaps
this is not a problem in more recent versions of c# or .net? Kimball Robinson Aug 20 '10 at 16:13
@k.robinson - perhaps that is because you are using a boxed reference to the instance. Please realize that
I'm advocating the same as Eric Lippert - one of the creators of the .Net platform itself - is pointing out in his
answer. If you have a problem, you might want to reconsider if "select isn't broken" (pragprog.com/thepragmatic-programmer/extracts/tips). codekaizen Aug 20 '10 at 16:57
@codekaizen - Ok. I still wish I could do this without a typecast: dateTimeField.Text =
dateTimeObj.HasValue ? ((DateTime) dateTimeObj).ToShortDateString() : string.Empty; Kimball Robinson
Aug 25 '10 at 0:12
PHP
39 mins ago - Mike M.
Linked
61 Nullable ToString()
2
Datetime? to string
Related
776 How do I calculate
54
Though many of these answers are correct, all of them are needlessly complex. The result of calling
ToString on a nullable DateTime is already an empty string if the value is logically null. Just call
ToString on your value; it will do exactly what you want.
49 How do I use
DateTime.TryParse with a
Nullable<DateTime>?
datetime
11 Is it better to use
2 Unless you want to use the DateTime properties like .Day .Week because that will give you the entire
DateTime string and lose the power of the DateTime class. e.g. myVariable.Value.Hour.ToString() .
Just an example reason why you might want to do otherwise. baron Aug 4 '11 at 6:01
DateTime.MinValue or a
nullable DateTime?
converted by Web2PDFConvert.com
Just an example reason why you might want to do otherwise. baron Aug 4 '11 at 6:01
correct?
1 DateTime.Ticks for
nullable datetime
Actually, this is the default behaviour for Nullable types, that without a value they return nothing:
14
DateTime
2 Compare nullable
datetime objects
0 Datetime nullable issue
2 Nullable DateTime to
String
this yields
<>
<2009-09-18 19:16:09>
share | improve this answer
2 +1 Did not know this. However, you cannot supply a formatting string this way. Jon Seigel Sep 18 '09 at
17:20
Hm, right. Though this may not be a problem in this case. I didn't know this myself too until yesterday,
though. Stumbled over it when looking at Nullable<T> in Reflector :-) Sep 18 '09 at 17:27
...
var str = myVariable.ToStringSafe();
share | improve this answer
4 Or better yet: make it generic: public static string ToSafeString<T>(this T? obj) where T :
struct :) JulianR Sep 18 '09 at 17:51
Holy smokes, didn't realize .NET had this ability! tster Sep 18 '09 at 18:45
Connecting a potentiometer
How do I generate a random
string?
Is it still necessary to check for
a front camera?
DateTime? d;
// stuff manipulating d;
return d != null ? d.Value.ToString() : String.Empty;
share | improve this answer
DateTime d?;
string s = d.HasValue ? d.ToString() : string.Empty;
DateTime? MyNullableDT;
....
if (MyNullableDT.HasValue)
{
return MyNullableDT.Value.ToString();
}
return "";
share | improve this answer
if (aDate.HasValue)
return aDate;
else
return string.Empty;
share | improve this answer
Your Answer
Sign up or log in
Sign up using Google
Sign up using Facebook
Post as a guest
Name
Email
Not the answer you're looking for? Browse other questions tagged c# datetime or ask
your own question.
question feed
converted by Web2PDFConvert.com
tour help badges blog chat data legal privacy policy work here advertising info mobile contact us feedback
TECHNOLOGY
Stack Overflow
Programmers
Server Fault
Super User
Web Applications
Ask Ubuntu
Webmasters
Game Development
TeX - LaTeX
WordPress
Development
Geographic
Information Systems
Electrical Engineering
Android Enthusiasts
Information Security
Database
Administrators
LIFE / ARTS
CULTURE /
RECREATION
SCIENCE
OTHER
Photography
Mathematics
Stack Apps
Cross Validated
(stats)
Skeptics
Theoretical Computer
Science
Drupal Answers
SharePoint
Graphic Design
Mi Yodeya (Judaism)
User Experience
Seasoned Advice
(cooking)
Travel
Mathematica
more (14)
Home Improvement
Christianity
Arqade (gaming)
Bicycles
Academia
Role-playing Games
more (10)
more (21)
Area 51
Stack Overflow
Careers
Physics
MathOverflow
more (7)
site design / logo 2014 stack exchange inc; user contributions licensed under cc by-sa 3.0 with attribution required
rev 2014.7.11.1700
converted by Web2PDFConvert.com