sign up log in tour help
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.
Take the 2-minute tour
C# Nullable<DateTime> to string
asked 4 years ago
viewed 7571 times
active 2 years ago
19
c# datetime
share | improve this question
3
edited Sep 18 '09 at 17:30
146k 21 280 387
9 Answers
asked Sep 18 '09 at 17:06
JL.
15.8k 53 195 324
active
oldest
share | improve this answer
edited Sep 18 '09 at 20:36
Front End Developer
Six Foot
Houston, TX
html5 css3
iOS Developer
Photojojo
Houston, TX / relocation
objective-c cocoa-touch
votes
string date = myVariable.HasValue ? myVariable.Value.ToString() : string.Empty;
30
Looking for a job?
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 ?
answered Sep 18 '09 at 17:09
Jon Seigel
8,658 3 32 70
Data Operations Engineer (SF)
Audax Health
San Francisco, CA
hadoop mongodb
Software Engineer - Houston, TX
Two Sigma Investments
Houston, TX / relocation
tsql plsql
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
101 People Chatting
JavaScript :: Equality for
fembots
38 mins ago - dystroy
PHP
39 mins ago - Mike M.
Linked
61 Nullable ToString()
2
Datetime? to string
Related
776 How do I calculate
someone's age in C#?
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
share | improve this answer
313 Converting string into
answered Sep 18 '09 at 18:01
Eric Lippert
297k 67 658 1492
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
5 Is Nullable DateTime work
correct?
@baron, those properties aren't Nullable . Sam Sep 10 '13 at 4:37
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
public class Test {
public static void Main() {
System.DateTime? dt = null;
System.Console.WriteLine("<{0}>", dt.ToString());
dt = System.DateTime.Now;
System.Console.WriteLine("<{0}>", dt.ToString());
}
}
datetime objects
0 Datetime nullable issue
2 Nullable DateTime to
String
Hot Network Questions
this yields
Loop without 'looping'
<>
<2009-09-18 19:16:09>
share | improve this answer
8 Convert String to Nullable
Replace interior of matrix with
zeros
answered Sep 18 '09 at 17:17
146k 21 280 387
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
"Quick Fix" Tyre Repair Spray
Cans
Plural of "that's my boy"
When do skills count as class
skills when you have multiple
(prestige) classes?
What is this AC source?
How can I deal with players
who don't consider the
narrative?
You could write an extension method
Random numbers that sum up
to specific value
public static string ToStringSafe(this DateTime? t) {
return t.HasValue ? t.Value.ToString() : String.Empty;
}
How can I discourage
employees from working
voluntary overtime?
...
var str = myVariable.ToStringSafe();
share | improve this answer
Why is a minor 3rd consonant
but an augmented 2nd
dissonant?
answered Sep 18 '09 at 17:10
JaredPar
330k 56 693 1028
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
share | improve this answer
I want to do research but I'm
too old for a PHD
Write a Rectangular Program
that Outputs the Number of
Times it was Rotated
Should I get an antivirus for my
Mac?
Calling .ToString() returns an empty string for a null value.
How to create circles and or
sections of a circle when the
centre is inaccessible
answered Sep 18 '09 at 20:40
DJ.
324 1 4 18
Connecting a potentiometer
How do I generate a random
string?
Is it still necessary to check for
a front camera?
How do you decide when to go
home for the day?
DateTime? d;
// stuff manipulating d;
return d != null ? d.Value.ToString() : String.Empty;
share | improve this answer
answered Sep 18 '09 at 17:09
Cecil Has a Name
3,375 1 15 23
Is there any major difference
when comparing a variable as a
string or as an int
One who can't keep secrets?
What is the longest anime
title?
DateTime d?;
string s = d.HasValue ? d.ToString() : string.Empty;
How do I keep a bike feeling
"new"?
Alternatives to "Like", "+1",
converted by Web2PDFConvert.com
share | improve this answer
Alternatives to "Like", "+1",
"Bookmark", and "Favorite"?
What is a reasonable
maintenance schedule for
Windows PCs?
DateTime? MyNullableDT;
....
if (MyNullableDT.HasValue)
{
return MyNullableDT.Value.ToString();
}
return "";
share | improve this answer
answered Sep 18 '09 at 17:10
Patrick Desjardins
50.9k 46 188 260
edited Oct 9 '11 at 17:56
NullUserException
41.9k 11 96 164
answered Sep 18 '09 at 17:09
Ahmed Khalaf
741 5 22
edited Oct 9 '11 at 17:56
NullUserException
41.9k 11 96 164
answered Sep 18 '09 at 17:09
Mike
2,057 1 20 36
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
Sign up using Stack Exchange
Post Your Answer
By posting your answer, you agree to the privacy policy and terms of service.
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
Unix & Linux
Super User
Ask Different (Apple)
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
English Language &
Usage
Mathematics
Stack Apps
Cross Validated
(stats)
Meta Stack Exchange
Skeptics
Theoretical Computer
Science
Drupal Answers
Science Fiction &
Fantasy
SharePoint
Graphic Design
Mi Yodeya (Judaism)
User Experience
Seasoned Advice
(cooking)
Travel
Mathematica
more (14)
Home Improvement
Christianity
Arqade (gaming)
Personal Finance &
Money
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