Got the time of day?

I am stuck in a pit, and because of my life I cannot break out of it.

This is a very aesthetic question, but I like to get what I ask from my code. I am trying to parse a string representing the time of a DateTime variable, which I then send to a text box, as well as to my LINQ query.

I would like to present my time in this format: "yyyy-MM-dd hh: mm: ss", and this is what I get from my Select request, but as soon as I try to parse what the user wrote in the DateTime text box he gives me "1/13/2011 12:00:00 AM", even if he was in the format "2011-01-13 00:00:00".

It seems to me that I tried everything to make this work, but there must be a solution, can you guys help me find it? What can I use for IFormatProvider?

This is what I tried:

//textBox1.Text =   "2011-01-13 00:00:00";
DateTime = TimeFrom;    
TimeFrom = DateTime.ParseExact(textBox1.Text, "yyyy-MM-dd hh:mm:ss", null);
TimeFrom = DateTime.ParseExact(textBox1.Text, CultureInfo.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns(), new CultureInfo("sv-SE"), DateTimeStyles.None); 
TimeFrom = DateTime.ParseExact(textBox1.Text, CultureInfo.CurrentCulture.DateTimeFormat.GetAllDateTimePatterns(), new CultureInfo("sv-SE"),System.Globalization.DateTimeStyles.AssumeUniversal);
TimeFrom = DateTime.ParseExact("2008-10-01 16:44:12.000", "yyyy-MM-dd HH:mm:ss.fff", CultureInfo.CreateSpecificCulture("en-US"));
textBox1.Text = TimeFrom.ToString();

But none of this gives me the formatting that I crave so much.



UPDATE:
It seems that to some extent, the current culture has changed from the moment I declared the DateTime variable until the time I wanted to parse the value of textBox1.Text from "sv-SE" to "en-EN", so she decided to change The way my time was formatted. This is not what I do in my code. Any ideas as to why?

Why did he decide to ignore mine (IFormatProvider) new CultureInfo("sv-SE",true), this is something that I did not understand yet, but the broadcast.

If you have any suggestions, please let me know.

+3
5

, DateTime, .

DateTime , ToString .

TimeFrom.ToString("yyyy-MM-dd hh:mm:ss");

: , Thread.CurrentThread.CurrentCulture = new CultureInfo("sv-SE"), . ASP.NET?

+3
textBox1.Text = TimeFrom.ToString("yyyy-MM-dd hh:mm:ss");

- :

textBox1.Text = TimeFrom.ToString(new CultureInfo("sv-SE"));

, .

+4

TimeFrom.ToString("yyyy-MM-dd hh:mm:ss");

DateTime String.

+1

, . , .

, , CurrentCulture en-EN. , , en-EN.

Then it changed the way I view my time. The DateTime variable does not remember its formatting culture. Therefore, any changes to the presentation of DateTime as a string must contain string formatting, for example:dateTimeVariable.toString("yyyy-MM-dd hh:mm:ss")

I hope someone finds this helpful.

0
source

Source: https://habr.com/ru/post/1786145/


All Articles