文本框的DateTime格式无法编译 [英] DateTime format to textbox won't compile

查看:119
本文介绍了文本框的DateTime格式无法编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正在研究的程序(请参见下文),但我还没有弄清楚如何自定义时间格式,然后将其发送到文本框.我把String.Format弄乱了,但是无法编译.我希望当前时间采用以下格式:mm/dd/yyyy,然后打印到textbox1(tb1).提前谢谢.

[从答案1开始更新]
我输入了字符串值,现在程序启动时得到了正确的值,但是当我按下(Bct)按钮时,程序挂断了(DateTime到达= DateTime.Parse(tb1.Text);).我想我需要通过TimeDate而不是字符串来完成格式化(mm/dd/yyyy).这可能吗?

I have this program I''ve been working on (see below) and I have not figured out how to customize the time format and then send it to a textbox. I messed around with String.Format but it won''t compile. I want the current time to be in the following format: mm/dd/yyyy, and then print to textbox1 (tb1). Thanks in advance.

[updated since answer 1]
I put in string values and now I get the correct values when the program starts but when I press the (Bct) button the program gets hung up on (DateTime arrival = DateTime.Parse(tb1.Text);). I think I need to get the formatting (mm/dd/yyyy) done via TimeDate, not a string. Is this possible?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        DateTime date = DateTime.Now;

        String a = DateTime.Now.ToString("d/M/yyyy");
        tb1.Text = (a).ToString();

        DateTime dateplus3 = date.AddDays(3);
        String d = DateTime.Now.ToString("d/M/yyyy");
        tb2.Text = (d).ToString();

        string nightlyprice = String.Format("{0:c}", 115.00);
        tb4.Text = (nightlyprice).ToString();
    }

    private void Bct_Click(object sender, EventArgs e)
    {
        DateTime arrival = DateTime.Parse(tb1.Text);
        DateTime departure = DateTime.Parse(tb2.Text);
        TimeSpan nights = departure.Subtract(arrival);

        int nightsshort = nights.Days;
        int nightlyprice = 115;

        tb5.Text = string.Format("{0:c}", nightsshort * nightlyprice);
        tb3.Text = (nightsshort).ToString();

    }
}





[从答案3开始更新]感谢您的帮助!完成的工作代码:
公共Form1()





[updated since answer 3] thanks for the help! Finished and working code:
public Form1()

 <pre>{
            InitializeComponent();

            DateTime date = DateTime.Now;
            string nightlyprice = String.Format("{0:c}", 115.00);
            DateTime nowplus3 = date.AddDays(3);

            tb1.Text = DateTime.Now.ToString("M/d/yyyy");
            tb2.Text = String.Format("{0:M/d/yyyy}", nowplus3);            
            tb4.Text = (nightlyprice).ToString();
        }
   
        private void Bct_Click(object sender, EventArgs e)
        {
            try
            {
                CultureInfo provider = CultureInfo.InvariantCulture;

                DateTime arrival = DateTime.ParseExact(tb1.Text, "M/d/yyyy", provider);
                DateTime departure = DateTime.ParseExact(tb2.Text, "M/d/yyyy", provider);
                DateTime date = DateTime.Now;
                TimeSpan nights = departure.Subtract(arrival);
                TimeSpan irrational = arrival.Subtract(date);
                TimeSpan years = departure.Subtract(date);

                int nightlypricei = 115;
                int yearsi = years.Days;
                int irrationali = irrational.Days;
                int nightsi = nights.Days;
                
               
            

                if (nightsi >= 1 && irrationali >= 0 && yearsi <= 1825)
                {
                    
                    tb5.Text = string.Format("{0:c}", nightlypricei * nightsi);
                    tb3.Text = (nightsi).ToString();
                }
            
Rest of code omitted

推荐答案

您可以在以下日期中指定所需的日期和时间格式:

字符串s = DateTime.Now.ToString("d-M-yyyy hhmmss")

可能会有帮助.
You can specify the format you want the date and time back in:

String s = DateTime.Now.ToString("d-M-yyyy hhmmss")

Might help.


好吧,想要帮助的是,您正在寻找的是:

Well, to be a bit more helpful, all you''re looking for is:

date.ToString("MM/dd/yyyy")

//or

dateplus3.ToString("MM/dd/yyyy")



不过,实际上,对于Google来说,这是一件非常简单的事情,我建议将来从这里开始.



Really, though, this is a pretty simple thing to google and I would suggest starting there in the future.


针对您的新问题,您的问题将与事实有关您已更改日期和月份.

您的区域性设置可能将DateTime指定为MM/dd/yyyy,这意味着当它看到15/9/2010时,它表示没有月份,其数字为15.

添加:
In response to your new question, your problem is going to be related to the fact that you''ve switched the date and the month.

Your culture settings probably have the DateTime specified as MM/dd/yyyy, which means that when it sees 15/9/2010, it says there is not month with the number of 15.

Add:
using System.Globalization;


然后将您的代码更改为:


and then change your code to:

CultureInfo provider = CultureInfo.InvariantCulture;

DateTime arrival = DateTime.ParseExact(tb1.Text, "d/M/yyyy", provider);
DateTime departure = DateTime.ParseExact(tb2.Text, "d/M/yyyy",provider);



我也很好奇为什么您要说您要使用MM/dd/yyyy格式,然后再使用d/M/yyyy.那是怎么回事?如果您最初将其存储为MM/dd/yyyy,那么Parse就不会出现问题.



I''m also curious why you say that you want your format to be in MM/dd/yyyy format and then you use d/M/yyyy. What''s up with that? If you had originally stored it as MM/dd/yyyy, you wouldn''t have gotten the issue with Parse.


这篇关于文本框的DateTime格式无法编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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