DateTime a1 = new DateTime(Convert.ToDateTime(txtStartDate.Text).Year, Convert.ToDateTime(txtStartDate.Text).Month, Convert.ToDateTime(txtStartDate.Text).Da开发者_运维技巧y, 0, 0, 0);
I have tried to change system time from 12 hrs hh to 24 hours HH and restart web site insert still is 12:00:00
I want 00:00:00
What you're seeing is a formatting issue rather than a data issue. It really is 00:00:00, but however you're converting it to a string is showing it as 12:00:00, presumably with an implicit "am". Don't forget that a DateTime
doesn't actually have a format in it - it's just the date/time. You can format it appropriately yourself, e.g.
Console.WriteLine(a1.ToString("yyyy-MM-dd HH:mm:ss"));
All this aside, I would strongly recommend that you don't create the DateTime
this way. Personally I prefer to use DateTime.TryParseExact
or DateTime.ParseExact
anyway, rather than using "whatever patterns the current culture happens to prefer", but even if you do want to parse with Convert.ToDateTime
, it would be clearer to do it once, and then use the Date
property to get a DateTime
with the time set to 0:
DateTime a1 = Convert.ToDateTime(txtStartDate.Text).Date;
Well, technically there is no time as 00:00:00. After 11:59:59 it becomes 12:00:00. Maybe I'm just not understanding what you're trying to do :)
You could have special logic in your code that modifies it such that if it is 12:00:00, then display it as 00:00:00 or something. But out of the box there is no magic or format string that will do this.
You could simplify that a bit by specifying a 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 textbox is, but assuming it is 01/27/2011 (US format) you can change the above snippet to work easily:
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're using. Ideally you should parse the date into a UTC date but that's a different discussion.
When 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. The ToString() uses "en-US" as default culture. Ofcourse you can manually call ToString() and parametrise it with any CultureInfo you wish but sometimes that is not possible.
When you access DateTime-objects via Databinding for example, you can't pass any CultureInfo and end up with a date string localised in "en-US". In this case you need to set the CultureInfo for the current thread like:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-DE");
After this line is called all dates will be localised in "de-DE" as long as you don't manually call ToString() with a different CultureInfo.
Have a read on this link: https://msdn.microsoft.com/en-us/library/k494fzbf(v=vs.110).aspx
You could simplify that
if (!string.IsNullOrEmpty(txtStartDate.Text))
{
string startDate = Convert.ToDateTime(txtStartDate.Text).ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
}
精彩评论