How to set date and time with HH: mm: ss = 00:00:00 instead of 12:00:00?

DateTime a1 = new DateTime(Convert.ToDateTime(txtStartDate.Text).Year, Convert.ToDateTime(txtStartDate.Text).Month, Convert.ToDateTime(txtStartDate.Text).Day, 0, 0, 0); 

I tried to change the system time from 12 oโ€™clock to 24 oโ€™clock and restart restarting the website so far is 12:00:00

I want 00:00:00

+7
source share
5 answers

What you see is a formatting issue, not a data issue. It really is 00:00:00, but, nevertheless, you convert it to a string, shows it as 12:00:00, presumably with implicit "s". Remember that DateTime does not actually have a format in it - it's just a date / time. You can format it accordingly, for example.

 Console.WriteLine(a1.ToString("yyyy-MM-dd HH:mm:ss")); 

All this aside, I highly recommend that you not create a DateTime this way. Personally, I prefer to use DateTime.TryParseExact or DateTime.ParseExact anyway, instead of using "any templates that the current culture prefers", but even if you want to Convert.ToDateTime using Convert.ToDateTime , it would be easier to do this once. and then use the Date property to get a DateTime with a time set to 0:

 DateTime a1 = Convert.ToDateTime(txtStartDate.Text).Date; 
+7
source

Well, technically there is no time like 00:00:00. After 11:59:59 he becomes 12:00:00. Maybe I just donโ€™t understand what you are trying to do :)

You may have a special logic in the code that modifies it in such a way that if it is 12:00:00, then display it as 00:00:00 or something else. But out of the box there is no magic or formatted string that will do this.

+1
source

This can be simplified by specifying the format:

 DateTime a1 = DateTime.Parse(string.Format("{0} 00:00:00", "01/27/2011"), CultureInfo.GetCultureInfo("en-US")); 

I donโ€™t know what format your text box is in, but provided that it is 01/27/2011 (US format), you can easily change the above snippet:

 DateTime a1 = DateTime.Parse(string.Format("{0} 00:00:00", txtStartDate.Text), CultureInfo.GetCultureInfo("en-US")); 

Change your CultureInfo.GetCultureInfo (it implements IFormatProvider ) to the one you are using. Ideally, you should parse the date into a UTC date, but this is another discussion.

datetime

+1
source

If you want to add a DateTime object to a string or want to display it in some form of text, the ToString () method is used automatically. ToString () uses "en-US" as the default culture. Of course, you can manually call ToString () and parameterize it with whatever CultureInfo desires, but sometimes this is not possible.

When you access DateTime objects via data binding, for example, you cannot pass any CultureInfo and end up with a date string localized in "en-US". In this case, you need to set CultureInfo for the current stream, for example:

 Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-DE"); 

After this string is called, all dates will be localized in "de-DE" unless you manually call ToString () with another CultureInfo.

Read this link: https://msdn.microsoft.com/en-us/library/k494fzbf(v=vs.110).aspx

0
source

You can simplify this

 if (!string.IsNullOrEmpty(txtStartDate.Text)) { string startDate = Convert.ToDateTime(txtStartDate.Text).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); } 
0
source

All Articles