Click here to Skip to main content
15,885,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
C#
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();

    }
}




[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
Posted
Updated 16-Sep-10 4:37am
v7
Comments
Sandeep Mewara 15-Sep-10 13:20pm    
Readded the PRE tags. Looks like OP missed it again while update.
William Winner 15-Sep-10 17:57pm    
You do realize that with your new code, you created dateplus3, but then you set tb2.Text to DateTime.Now, right?

Is that really what you wanted?

You can specify the format you want the date and time back in:

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

Might help.
 
Share this answer
 
Well, to be a bit more helpful, all you're looking for is:

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

//or

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


Really, though, this is a pretty simple thing to google and I would suggest starting there in the future.
 
Share this answer
 
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:
C#
using System.Globalization;

and then change your code to:

C#
CultureInfo provider = CultureInfo.InvariantCulture;

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


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.
 
Share this answer
 
v2
Comments
thomas struss 16-Sep-10 8:08am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Dalek Dave 16-Sep-10 11:16am    
Good Answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900