仅显示Windows窗体应用程序打印输出中的日期 [英] show only date in windows form application printout

查看:84
本文介绍了仅显示Windows窗体应用程序打印输出中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个maskedtextbox用于输入日期和两个按钮用于检索和提交日期,gridview用于显示sql 2008数据库的检索日期,还有一个打印按钮用于打印日期输入

I have a maskedtextbox to enter date and two buttons for retrieve and submit of date and a gridview to show the retrieve dates fron sql 2008 database and also a print button to take the print out of the dates enter

The result in printout is for eexample:-25/09/2013 12:00:00AM which I don't want
I don't want the time 12:00:00AM My result should be only DATE I.E 25/09/2013
why it is taking dafault time with it.





我认为在提交活动期间转换过程中出现了dafault时间值(12:00:00 AM): -



I think the dafault time value(12:00:00AM) came during the conversion process during submit event:-

string str = "insert into date values(@discharge_date,@Discharge_Advice)";
            SqlCommand cmd;
            cmd = new SqlCommand(str, con);
            cmd.Parameters.AddWithValue("@discharge_date", SqlDbType.Date).Value = Convert.ToDateTime(maskedTextBox1.Text);//Here it is converting it in datetime format so how to avoid it so that the conversion would be in DATE only.

           

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            MessageBox.Show("Records inserted");

推荐答案

如果你想只和你约会可以使用 DateTimeFormatInfo 并设置所需的日期模式,然后将其插入数据库。

If you want to have only date you can use DateTimeFormatInfo and set the date pattern you want and then insert it into database.
public DateTime StringToDate(string date)
{
    DateTime dt = new DateTime();
    DateTimeFormatInfo format = new DateTimeFormatInfo();
    format.ShortDatePattern = "dd/MMM/yyyy";
    try
    {
        dt = DateTime.Parse(date, format);
    }
    catch (Exception e) { }
    return dt;
}
//Call this function
cmd.Parameters.AddWithValue("@discharge_date", SqlDbType.Date).Value = StringToDate(maskedTextBox1.Text);


这篇关于仅显示Windows窗体应用程序打印输出中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆