Question

I face the following problem :

Sometimes the value of my trans_in(DateTime) in my database is :1900-01-01 00:00:00.000

and it appears in my telerik report like this 12:00 AM i want to show the textbox empty instead so i create the following custom method :

  public static string NotAttend(DateTime trans_in)
        {
            if (trans_in == null || trans_in.ToString().Trim() == "1900-01-01 00:00:00.000")
            {
                return string.Empty;
            }
            else
                return trans_in.ToShortTimeString();
        }

and bind the textbox like this :

= OvertimeReports.CustomExpressions.NotAttend(Fields.trans_in)

but this didn't fix my problem still appear 12:00 AM !!

Was it helpful?

Solution

Your trans_in.ToString() would return you the string representation of your DateTime object based on your current culture, its better if you check your DateTime like:

public static string NotAttend(DateTime trans_in)
{
   if(trans_in == new DateTime(1900, 1, 1))
   {
        return string.Empty;
   }
    else
        return trans_in.ToShortTimeString();
}

OTHER TIPS

Try to compare the trans_in with default(new DateTime())

You shouldn't compare a date with a String without formatting it

reference: http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/d0f4578c-0585-4f38-95be-569a90ebeb21/

edit: i'm seeing here you'd best compare with DateTime.MinValue Determining whether the new date is the default new DateTime() or not

You can provide a format to your DateTime by using overridden function of DateTime.ToString(). HH means time in 24 HRS where hh means 12 HRS format :

 public static string NotAttend(DateTime trans_in)
 {
   if (trans_in == null || 
       trans_in.ToString("yyyy-MM-dd HH:mm:ss.fff") == "1900-01-01 00:00:00.000")
     {
       return string.Empty;
     }
   else
    return trans_in.ToShortTimeString();
  }

The problem what you have here is more of a formatting issue with the .ToString conversion

Try using the specific format you need in the .ToString overload for this

trans_in("yyyy-M-d HH:mm:ss.fff")

So in this HH case you would have it as 00:00 instead of 12:00

A better way would be to compare it as DateTime make a DateTime obj for 1900-1-1 then compare it with trans_in.Date part which would not involve this string formatting issues

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top