DateTime To Be NULL when the value is DateTime.MinValue or it is Null

In my console app I am attempting to format to HHmmss -> I am sure it is due to my data types but how can I have this be NULL when NULL and not display 1/1/0001 12:00:00 AM?

This is my syntax

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"));

If altered to public static DateTime? fmtLastScanTime; I get an error of

'No overload for method 'ToString' takes 1 arguments

How can I have this display NULL instead of 1/1/0001 12:00:00 AM? Trying to account for 1/1/0001 12:00:00 AM being displayed

Upvotes: 7

Views: 4610

Answers (4)

Maharshi
Maharshi

Reputation: 1198

May be but try this

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

Upvotes: 0

Mohit S
Mohit S

Reputation: 14064

Nullable DateTime. A nullable DateTime can be null. The DateTime struct itself does not provide a null option. But the "DateTime?" nullable type allows you to assign the null literal to the DateTime type. It provides another level of indirection.

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

A nullable DateTime is most easily specified using the question mark syntax

Edit:

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

Another one could be

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

Upvotes: 3

sujith karivelil
sujith karivelil

Reputation: 29026

The value 1/1/0001 12:00:00 AM is the minimum/default value of the DateTime object, if you want to assign null to DateTime object means you have to make them as Nullable(as like others suggested). So the declaration of fmtLST should be :

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

In this case you have to care about printing the output to the console. it should be something like:

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

Upvotes: 0

Lucifer
Lucifer

Reputation: 1594

I have Found This refrence here when seraching for same Issue

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());
            }
        }
    }
}

you can go in link find more details Thanks

Upvotes: 0

Related Questions