Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set datetime 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 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

like image 671
timebug Avatar asked Jan 27 '11 09:01

timebug


People also ask

How do I change the format of a DateTime object in Python?

Function usedstrftime() can change the date format in python.

How do I convert DateTime to date format?

To convert a datetime to a date, you can use the CONVERT() , TRY_CONVERT() , or CAST() function.

How do you format date and time?

On the Home tab, click the Dialog Box Launcher next to Number. You can also press CTRL+1 to open the Format Cells dialog box. In the Category box, click Date or Time, and then choose the number format that is closest in style to the one you want to create.


2 Answers

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;
like image 155
Jon Skeet Avatar answered Nov 11 '22 07:11

Jon Skeet


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.

like image 24
Shiv Kumar Avatar answered Nov 11 '22 07:11

Shiv Kumar