Click here to Skip to main content
15,909,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can show screen that I take date. For example I write17/12/2014 ,screen show me 17 december 2014
Posted

If the DateTime object is valid; no error parsing it to DateTime, then use the .ToString method to convert this date time object to a string representation of your choice. For you this would work.

C#
dateTime.ToString("dd MMMM yyyy"); // date month and year


Use above code to write the dateTime in string format in the interface. For more please read this document[^].
 
Share this answer
 
v4
Comments
BillWoodruff 25-Nov-14 9:06am    
This ignores the OP's desire to transform an input string in a format that cannot be used as an argument to DateTime.Parse to a DateTime.
Use DateTime.TryParse:
C#
public void ShowLongDate(string inputDate)
   {
   DateTime date;
   if (!DateTime.TryParse(inputDate, out date))
       {
       Console.WriteLine("Invalid date: " + inputDate);
       }
   else
       {
       Console.WriteLine(date.ToString("D"));
       }
   }
 
Share this answer
 
v2
Comments
BillWoodruff 25-Nov-14 9:05am    
Ooh, our first one-vote: can our relationship handle this :)

I think the OP may need a heavier cannon than 'TryParse here.
 
Share this answer
 
Thank you for your examples. I understand now.
 
Share this answer
 
Comments
Tomas Takac 25-Nov-14 4:43am    
Don't post your comments as an solution. Rather comment on the relevant solution itself and if it does help you accept the answer.
If you tried to create a new DateTime in code using:
C#
DateTime convertedDate = DateTime.Parse("17/12/2014");
you would get a run-time error, and DateTime.TryParse would also not work:
C#
DateTime convertedDate;

if (DateTime.TryParse("17/12/2014", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out convertedDate))
{
    textBox1.Text = convertedDate.ToString(dateOutputFormat);
}
else
{
    textBox1.Text = "conversion fail";
}
You need to use 'TryParseExact
C#
CultureInfo provider = CultureInfo.InvariantCulture;

string dateInputFormat = "dd/mm/yyyy";
string dateOutputFormat = "dd MMMM yyyy";

private string stringToDate(string inputDate)
{
    DateTime convertedDate;

    if (DateTime.TryParseExact(inputDate, dateInputFormat, provider, DateTimeStyles.AssumeUniversal, out convertedDate))
    {
        return convertedDate.ToString(dateOutputFormat);
    }
    else
    {
       return "Invalid date conversion: " + inputDate;
    }
}

// test using a button-click
private void TestStringToDateConversion_Click(object sender, EventArgs e)
{
    textBox1.Text = stringToDate("17/12/2014");
}
 
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