C# - Convert Nullable DateTime to String | Internet Privacy | Technology

January 11, 2018 | Author: Anonymous | Category: C#
Share Embed


Short Description

C# - Convert Nullable DateTime to String - Download as PDF File (.pdf), Text File (.txt) or read online. C# Tutorial Con...

Description

sign up

log in

tour

help

careers 2.0 search

Questions

Tags

Users

Badges

Unanswered

Ask Question

Stack Overflow is a question an d answer site for professional and enthusiast programmers. It's It's 100% free, no registration required.

Take the 2-minute tour

×

C# Nullable to string asked 4 years ago viewed 7571 times active 2 years ago

19

c#

Front End Developer  Six Foot Houston, TX

datetime

share | improve this question 3

edited edite d Sep 18 '09 at 17:30 Јοеу 146k 21 280 387

asked Sep 18 '09 at 17:06 JL.. JL 15.8k 53 195 324

active

oldest

css3

iOS Developer  Photojojo Houston, TX / relocation

votes

string date string  date = myVariable.HasValue myVariable. HasValue ?  ? myVariable.Value myVariable. Value. .ToString ToString() () : string string. .Empty Empty; ; share | improve this answer 

html5

objective-c cocoa-touch

9 Answers

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 ?

Data Operations Engineer (SF (SF)) Audax Health San Francisco, CA hadoop

edited Sep 18 '09 at 20:36

answered Sep 18 '09 at 17:09 Jon Seigel 8,658 3 32 70

mongodb

Software Engineer - Houston, TX Software Two Sigma Investments Houston, TX / relocation tsql

plsql

Thank you , exactly what I was looking looking for – JL JL.. Sep 18 '09 at 17:10 5

What!? You can just call .ToString .ToString() () on the the Nullable instance to get String.Empty. Even Eric Lippert (who might have even implemen Lippert implemented ted this this behavior) notes this. That  should  should be the accepted answer. – codekaizen Jul 9 '10 at 1:48

1

@codekaizen - I get an exception when I try that. that . So no, that would not not be the accepted answer. answer. Perhaps this is not a problem in more recent versions of c# or .net? – Kimball Robinson Au Robinson Aug g 20 20 '10 10 at 16:1 16:13 3 @k.robinson - perhap perhaps s that is because you are are using a boxed referen reference ce 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/the(pragprog.com/thepragmatic-programmer/extracts/tips ). – codekaizen codekaizen Au  Aug g 20 20 '10 10 at 16:5 16:57 7 @codekaizen - Ok. I still wish I could do this without a typecast: dateTimeField.Text = dateTimeObj.HasValue ? ((DateTime) dateTimeObj).ToShortDateString() : string.Empty; – Kimball Robinson  Aug  Au g 25 25 '10 10 at 0:12 0:12

101 People Chatting JavaScript :: Equality for fembots 38 mins ago - dystroy

PHP PH P 39 mins ago - Mike M.

Linked 61 Nullable ToString() 2

Datetime? Datetim e? 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. complex. The result of calling ToString on a nullable DateTime DateTime is already an e mpty string if the value is logically null. null. Just  Just call ToString on your value; it will do exactly what you want. share | improve this answer 

answered Sep 18 '09 at 18:01 Eric Lippert 297k 67 658 1492

49 How do I use

DateTime.TryParse with a Nullable? 313 Converting string into

datetime 11 Is it better to use

2

Unless you want to use the DateT DateTime ime properties 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() . '

DateTime.MinValue or a nullable DateTime?

converted by Web2PDFConvert.com

us an examp e reason w y you m g

wan o o o erwse. – aron ug

a

:

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

8 Convert String t o Nullable

DateTime 2 Compare nullable

public class Test { public static void Main() { System.DateTime? dt = null; System.Console.WriteLine("", dt.ToString()); dt = System.DateTime.Now; System.Console.WriteLine("", dt.ToString()); } }

datetime objects 0 Datetime nullable issue 2 Nullable DateTime to

String

Hot Network Questions this yields

Loop without 'looping'

share | improve this answer 

2

Replace interior of matrix with zeros answered Sep 18 '09 at 17:17 Јοеу 146k 21 280 387

"Quick Fix" Tyre Repair Spray Cans Plural of "that's my boy"

+1 Did not know this. However, you cannot supply a formatting string this way. – Jon Seigel Sep 18 '09 at 17:20

When do skills count as class skills when you have multiple (prestige) classes?

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  in Reflector :-) – Јοеу Sep 18 '09 at 17:27

What is this AC source? How can I deal with players who don't consider the narrative?

You could write an extension method

7

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 

4

Why is a minor 3rd consonant but an augmented 2nd dissonant? answered Sep 18 '09 at 17:10 JaredPar  330k 56 693 1028

Or better yet: make it generic: public static string ToSafeString(this T? obj) where T : struct  :) – JulianRSep 18 '09 at 17:51

I want to do research but I'm too old for a PHD

Holy smokes, didn't realize .NET had this abilit y! – tster Sep 18 '09 at 18:45

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.

3

How to create circles and or  sections of a circle when the centre is inaccessible

share | improve this answer 

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?

2

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?

1

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 

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 

1

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  Web Applications  Ask Ubuntu Webmasters Game Development TeX - LaTeX

 Ask Different (Apple) WordPress Development Geographic Information Systems Electrical Engineering  Android Enthusiasts Information Security

Database  Administrators

LIFE / ARTS

CULTURE / RECREATION

SCIENCE

OTHER

Photography

English Language & Usage

Mathematics

StackApps

Cross Validated (stats)

Meta Stack Exchange

Drupal Answers

Science Fiction & Fantasy

SharePoint

Graphic Design

Mi Yodeya (Judaism)

User Experience

Seasoned Advice (cooking)

Travel

Mathematica more (14)

Home Improvement

Skeptics

Christianity  Arqade (gaming)

Personal Finance & Money

Bicycles

 Academia

Role-playing Games

more (10)

more (21)

 Area 51

Theoretical Computer  Science

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

View more...

Comments

Copyright © 2017 DATENPDF Inc.