Click here to Skip to main content
15,912,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
// I have created a small form where the user has to input the date and the form will convert it from the user input fx: 10-02-2020 to 10-Febuary-2020 - and this is the code that i got so far, but im currently stuck

string dag, month, year;
       private void buttonOK_Click(object sender, EventArgs e)
       {


           string date = maskedTextBoxDato.Text;


           string day = dato.Substring(0, 2);
           string month = dato.Substring(3, 2);
           string year = dato.Substring(6, 4);


           switch (maskedTextBoxDato.Text)
           {
               case "01":
                   labelFacit.Text = "Januar";
                   break;
               case "02":
                   labelFacit.Text = "Februar";
                   break;
               case "03":
                   labelFacit.Text = "Marts";
                   break;
               case "04":
                   labelFacit.Text = "April";
                   break;
               case "05":
                   labelFacit.Text = "Maj";
                   break;
               case "06":
                   labelFacit.Text = "Juni";
                   break;
               case "07":
                   labelFacit.Text = "Juli";
                   break;
               case "08":
                   labelFacit.Text = "August";
                   break;
               case "09":
                   labelFacit.Text = "Septemper";
                   break;
               case "10":
                   labelFacit.Text = "Oktober";
                   break;
               case "11":
                   labelFacit.Text = "November";
                   break;
               case "12":
                   labelFacit.Text = "December";
                   break;

               default:
                   labelFacit.Text = "Fejl";
                   break;
           }


What I have tried:

I have tried adding and deleting code mulitiple times and severeal google searches.
Posted
Updated 14-Mar-20 6:16am

You should use a DatePicker Class (System.Windows.Controls) | Microsoft Docs[^] then you will not need any of the above code. Get to know the .NET classes so you can focus on the business parts of your application.
 
Share this answer
 
Comments
OliverAL 14-Mar-20 10:51am    
I have to use the switch method, it´s required for the assignment unfortunately
Richard MacCutchan 14-Mar-20 12:13pm    
OK, so what is the problem?
There are 2 fundamental portions to the task at hand:
1. Understand what the user has input as a DateTime object
2. Display that same DateTime object is a specific format.

For the first part, the best thing is to utilize the DateTime.TryParse method.
C#
string dag, month, year;

private void buttonOK_Click(object sender, EventArgs e)
{
	string date = maskedTextBoxDato.Text;

	DateTime dto;
	if (!DateTime.TryParse(date, out dto))
	{
		// There was a problem reading the user input
		// perhaps regionalization may help out
	} else {
		// to be continued
And the second part of your task is going to be displaying this.
Normally the recommended way would be simply use the ToString method and utilize either the Standard- or Custom Date and Time format strings.
However... you apparently need to use a switch...case block for formatting the date.
As we did get a valid DateTime object in the first part of this, that means we have the various properties; such as Year, Month, and Date- all as properties of type Int. This will make things easier as you don't need to worry about leading zeroes not being there; for instance on 10-2-2020 vs 10-02-2020.
C#
string MonthName;

switch(dto.Month) {
	case 1:
		MonthName = "Januar";
		break;
	// etcetera
}
labelFacit.Text = MonthName;
The rest of your date components will be easy conversions as well, simply using the ToString method on those properties as well.
C#
string day = dto.Day.ToString();
string month = dto.Month.ToString();
string year = dto.Year.ToString();
And if you need the leading zeroes, there are Standard Numeric Format strings available

References:
DateTime.TryParse Method (System) | Microsoft Docs[^]
DateTime.Month Property (System) | Microsoft Docs[^]
Standard date and time format strings | Microsoft Docs[^]
Custom date and time format strings | Microsoft Docs[^]
Standard numeric format strings | Microsoft Docs[^]
 
Share this 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