Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,

I need to assign a String variable into a DataTime variable by changing the format.

C#
string sDate = "21/04/1990"; // format dd/MM/yyyy

         DateTime dt;

         dt = sDate.Set_this_value_to_Dt_with_this_format("yyyy-MM-dd");

         Console.WriteLine(dt);


Finally result must be this ---> dt = 1990-04-21 <---

How can I resolve ?

Help me please

What I have tried:

I tried this code but it doesn't work

C#
string sDate = "21/04/1990"; // format dd/MM/yyyy

DateTime dt = DateTime.ParseExact(sDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture);

Console.WriteLine(dt);
Console.ReadKey();
Posted
Updated 12-Aug-21 9:56am
v7
Comments
PIEBALDconsult 12-Aug-21 16:12pm    
A "DataTime variable" does not have a format.
You may want to alter the settings in your operating system though.

Try this.

C#
string sDate = "21/04/1990";

DateTime dt;

if (DateTime.TryParse(sDate, out dt))
{
    string stringDate = dt.ToString("yyyy-MM-dd");

    Console.WriteLine(stringDate );
}
 
Share this answer
 
Comments
antobaro 12-Aug-21 15:08pm    
Thank you for your solution, but isn't correct.

I don't need stringDate with this format ("yyyy-MM-dd");

I need dt with this format ("yyyy-MM-dd");

I must print this

Console.WriteLine(dt); dt=1990-04-21
BillWoodruff 12-Aug-21 15:43pm    
+5
Tony Hill 13-Aug-21 4:24am    
Thanks
Tony Hill 12-Aug-21 15:53pm    
Having tried your code it falls over with System.FormatException which I presume is what you meant when you said it does not work.

Your problem is you are trying to parse a date 21/04/1990 but you specify that the pattern it must match is "yyyy-MM-dd", to try and parse the date you need to use "dd/MM/yyyy".

DateTime objects do not have a format, a format is used to convert the datetime into a human readable form.
TheRealSteveJudge 13-Aug-21 4:22am    
5*
Quote:
Thank you for your solution, but isn't correct.

I don't need stringDate with this format ("yyyy-MM-dd");

I need dt with this format ("yyyy-MM-dd");

I must print this

Console.WriteLine(dt); dt=1990-04-21

Tony's solution is correct - it answers the question you asked, if not the question you meant!

DateTime values don't have a format - they are stored as a number of "ticks" since a predefined pint in time.
They only acquire a format of any kind when you convert them to a string for presentation to a user - such as when you call ToString on a DateTime variable either explicitly or implicitly. When you do that, the default Date and Time format for the system the code is running on will be used unless you explicitly supply a format string to tell it what string output you want.
See here: Formatting a DateTime for display - format string description[^]
 
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