开发者

What is the proper DateTime format to get "both" date and time in MySQL and C#

开发者 https://www.devze.com 2023-04-09 09:12 出处:网络
In a MySQL table, I need to have two fields \"StartTime\" and \"EndTime\". My requirements are: If I do StartTime - EndTime, I need to get the time elapsed (when I subtract them I need to get time

In a MySQL table, I need to have two fields "StartTime" and "EndTime". My requirements are:

  1. If I do StartTime - EndTime, I need to get the time elapsed (when I subtract them I need to get time in seconds开发者_Go百科 or minutes or whatever).
  2. From "StartTime" I need to get the date (the date when my object was started).

So my question is in what format (or what type) should the fields "StartTime" and "EndTime" be in my MySQL table to meet the above two conditions? Also to which format in C# should I be retrieving the value from MySQL table?


MySQL offers two very similar data types DATETIME and TIMESTAMP. For many applications, either will work, but in some cases, one works better than the other...

http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

http://dev.mysql.com/doc/refman/5.0/en/datetime.html

in general if you can use TIMESTAMP you should, as it is more space-efficient than DATETIME (it takes half the size of DATETIME).

here is a link to all the possible operations on the date type

http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html


Generally in MySql the DateTime will be in 'YYYY-MM-DD HH:MM:SS' format. Refer to this to get the Required.


In c# you should use DateTime. I believe the mySQL equivalent is TimeStamp.

0

精彩评论

暂无评论...
验证码 换一张
取 消