简体   繁体   中英

using oracle data reader i can't format datetime into date only using ToString("dd/MM/yyyy")

using oracle data reader I am trying to display a loaded datetime into a textbox but it doesnt format correctly

Remember, the date to string formatter does NOT work with a string.

So, you can do this:

        string stringDate = "2022-01-15 18:40:30";
        DateTime MyDate = DateTime.ParseExact(stringDate,"yyyy-MM-dd HH:mm:ss",CultureInfo.InvariantCulture);

        // now we can output convert to anything we want.
        Debug.Print("Day of week = " + MyDate.ToString("dddd"));
        Debug.Print("Month = " + MyDate.ToString("MM"));
        Debug.Print("Month (as text) = " + MyDate.ToString("MMMM"));

        // desired format
        Debug.Print(MyDate.ToString("dd-MM-yyyy HH:mm:ss"));

Output:

在此处输入图像描述

but, you can not use the Tostring("some date format") on a string!!!

So, we can't from above do this:

string stringDate = "2022-01-15 18:40:30";
stringDate.ToString("mm/dd/yyyy");

We cannot do that UNLESS the date is datetime

So, you can cast to datetime, and then use Tostring() on the real date time type.

eg this:

MyTextbox.Text = ((DateTime)MyReader["CREATED_DATE"]).ToString("mm/dd/yyyy");

So note how we cast to DateTime and with () around the expression, then we can use the ToString() of the DateTime class.

ToString("some date format") does NOT work on a string!!!

Also, I'll let this slide, but PLEASE PLEASE in the future post your code - you forced me to re-type the above - I could have cut + pasted from your code - and my time is far more valiable then to open up some picture and screen shot, and THEN have to re-type that line of code.

We are all VERY busy people here - we don't have time to come to your house, cook your dinner, and then clean up after your mess, and throw out the garbage.

If you don't make answering easy here - then those who are smart, tallented and spending a few minuts a day here to help and share back to this community that helped us? Then we just skip your type of question in the future, and you be left here with all the others who have time to waste. And those others who have time to waste?

They also tend to be people that don't give very good answers, since if one in life, or even here on SO demostrates the ability to waste time? Then they tend to not be very good at much of anything - including that of writing and leaning code!

So in the future - post the code as text - it easier to read, but MORE important enables those here to try/test/read/see the code with less effort, and if we have a nice fix to your code, we save world poverty and starving children by not having to waste our time having to re-type YOUR code. We have better things to do with our valuable time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM