Dear All,

The problem I'm facing now is when I set my DateTimePicker format yyyy-MM-dd, when I put a date in that column, they will auto generate the time now.

For example, i entered 2011-03-30 but they will save in database 2011-03-30 10:56:54 AM.

Question here is:

How can I do where i want it to be "2011-03-30 00:00:00" ?

Kindly advice

Appreciate if you guys can help me on this...

Thank Q

Dani AI

Generated

A quick clarification: changing the DateTimePicker Format or CustomFormat (what you did in Properties) only affects how the control displays the value. The control still stores a full DateTime internally, so when you read Value you get a date plus a time. To store "2011-03-30 00:00:00" you must explicitly use a DateTime that has its time component cleared (midnight), or use a MySQL DATE column if you don't need the time at all.

Prefer passing a DateTime parameter to the database instead of concatenating strings. Example pattern in VB.NET (use whichever mysql parameter API you already have):

Dim d As DateTime = DateTimePicker1.Value.Date  ' time = 00:00:00
Dim cmd As New MySql.Data.MySqlClient.MySqlCommand("INSERT INTO mytable (dtcol) VALUES (@dt)", conn)
cmd.Parameters.Add("@dt", MySql.Data.MySqlClient.MySqlDbType.DateTime).Value = d
cmd.ExecuteNonQuery()

Notes and gotchas:

  • DateTimePicker1.Value.Date returns the same day with time 00:00:00 — simplest way to force midnight. Building New DateTime(...) with hours/minutes = 0 is equivalent.
  • If the column type is DATE, MySQL stores only the date. If it is DATETIME and you insert the bare date string, MySQL will implicitly set the time to 00:00:00, but passing a DateTime parameter is safer and avoids localization issues.
  • Avoid ToShortDateString() for DB input: it produces culture-dependent strings and is not safe for SQL building. Use parameters or explicit formatting with InvariantCulture if you must convert to string.
  • The MonthCalendar control only selects dates (no time). For date+time selection use DateTimePicker with CustomFormat including time (for example yyyy-MM-dd HH:mm:ss) and ShowUpDown = True to let users adjust hours/minutes, or combine a date control and separate time control if you need more control.

Recommended Answers

All 3 Replies

I have done it.... Heeee.....=)

Congrats,

May be it was by using "DateTime.Now().ToShortDateString()".

Finally, Why don't you mark this as solved .

thanks

I just modified in Properties... Is it ok?

And one more question here can we have datetime selection in monthcalendar?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.