日期未正确保存到MySQL

MySQL doesn't save the date properly.I am trying to add createdAt column in database.

I've tried using MySQL NOW() function already, but it doesn't seem to work.

// Go pseudo-code with sql query

stmt, _:= d.db.Prepare("INSERT INTO posts(title, body, EmailHref, SlackHref, DiscordHref, InstHref, Time) VALUES(?, ?, ?, ?, ?, ?, NOW());")
res, err := stmt.Exec(newPost.Title, newPost.Body, newPost.EmailHref, newPost.SlackHref, newPost.DiscordHref, newPost.InstHref)

The type for createdAt column in MYSQL is DATE.

This is what I get when i fetch newly created post:

"time": "2019-04-24T00:00:00Z"

I expect to get actual time with up to minutes.

I think you simple need to change the type from DATE to DATETIME in mySQL

alter table <tableName> modify <columnName> DATETIME;

In case you want to store the time too, you have to use DATETIME instead of DATE.

You can use the following query to change the column type of column createdAt:

ALTER TABLE posts MODIFY createdAt DATETIME

The DATE data type can't store a time part so you have to use DATETIME:

SELECT CAST(NOW() AS DATE);     -- 2019-04-24
SELECT CAST(NOW() AS DATETIME); -- 2019-04-24 09:47:57