2017-01-11 16 views
5

W mojej aplikacji konsoli próbuję sformatować do HHmmss -> Jestem pewien, że jest to spowodowane moimi typami danych, ale jak mogę to zrobić? NULL, gdy NULL i nie wyświetlać 1/1/0001 12:00:00 AM?DateTime To ma wartość NULL, gdy wartością jest DateTime.MinValue lub jej wartość wynosi Null

To moja składnia

public static DateTime fmtLST; 
public static string LST = null;   
if (LST != null) 
{ 
    IFormatProvider format = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat; 
    fmtLST = DateTime.ParseExact(LST, "HHmmss", format); 
} 

Console.WriteLine(fmtLST.ToString("hh:mm:ss tt")); 

Jeśli zmienia się public static DateTime? fmtLastScanTime; otrzymuję błąd

„Nie przeciążenie dla metody 'ToString' trwa 1 argumenty

Jak mogę czy to wyświetlenie NULL zamiast 1/1/0001 12:00:00 AM? Próba rozliczenia za 1/1/0001 12:00:00 AM jest wyświetlana

+0

jeśli 'fmtLST' jest' 'fmtLST.ToString null' następnie()' podniesie wyjątek –

+1

Ale nigdy nie jest null - będzie domyślnie 01.01.01.0000.00:00. Chcę, aby zwrócił wartość NULL, a nie domyślną datę/czas. –

Odpowiedz

0

Może być, ale spróbuj tego

IFormatProvider format = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat; 
       fmtLST = DateTime.ParseExact((LST != null ? LST : null), "HHmmss", format); 
+0

To wystarczy! Dziękuję za to. –

+0

WC:) .......... – Maharshi

3

Nullable DateTime. Nulllable DateTime może mieć wartość NULL. Sama struktura DateTime nie zapewnia opcji zerowej. Ale typ zerowalny umożliwia przypisanie literału o wartości zerowej do typu DateTime. Zapewnia inny poziom pośrednictwa.

public static DateTime? fmtLST; 
//or 
public static Nullable<DateTime> fmtLST; 

pustych DateTime najłatwiej jest określona przy użyciu składni znak zapytania

Edit:

Console.WriteLine(fmtLST != null ? fmtLST.ToString("hh:mm:ss tt") : ""); 

Kolejny może być

if(fmtLST == DateTime.MinValue) 
{ 
    //your date is "01/01/0001 12:00:00 AM" 
} 
+0

Używam zmiennej fmtLST, dzięki czemu mogę formatować DateTime.ParseExact (LST, "HHmmss", format); Jak sformatować w twoim przykładzie? –

+0

@ToastedBreadLeavesYouDead Zajrzyj do edytowanej części rozwiązania –

+0

Czy muszę używać "n/a" jako warunku else? Zmieniłem go na wartość null dla warunku else, ale wrócił on o 12:00:00 AM –

0

Znalazłem to refrence here kiedy seraching dla tego samego problemu

using System; 
using System.Text; 
using System.Windows.Forms; 
namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private void button1_Click(object sender, EventArgs e) 
     { 
      Nullable nullDateTime; 
      //DateTime? nullDateTime = null; 
      nullDateTime = DateTime.Now; 
      if (nullDateTime != null) 
      { 
       MessageBox.Show(nullDateTime.Value.ToString()); 
      } 
     } 
    } 
} 

można przejść w linku Więcej szczegółów Dzięki

0

Wartość 1/1/0001 12:00:00 AM to wartość minimalna/default obiektu DateTime, jeśli chcesz przypisać wartość null do obiektu DateTime oznacza, że ​​trzeba uczynić je jako Nullable (jak sugerowali inni). Więc deklaracja fmtLST powinno być:

public static DateTime? fmtLST = null; // initialization is not necessary 

W tym przypadku trzeba dbać o drukowaniu wyjście do konsoli. powinno być coś takiego:

Console.WriteLine(fmtLST.HasValue ? fmtLST.Value.ToString("hh:mm:ss tt") : "Value is null"); 
+0

innym warunkiem jest "Value IS NULL" - chcę, aby wartość else była zerowa. –

+0

Jak planujesz wydrukować 'null' na ekranie?w 'Console.WriteLine()' –

+0

Piszę do konsoli, aby sprawdzić, czy wartość jest rzeczywiście NULL. Końcowym rezultatem mojej procedury jest aktualizacja tabeli SQL Server, w której potrzebuję wartości NULL, a nie wartości Min.Date –

Powiązane problemy