How to configure gorm to store date in a specific format in sqlite?

I have this table:

type Table struct {
   Date time.Time `gorm:"column:date;type:datetime;default:current_timestamp;not null"`
}

But it is stored in this format:
2023-10-25 23:36:29.4928236-05:00

How do I get gorm to store it this way?
10-25-2023 23:36:29.4928236-05:00

Sqlite always store dates as “YYYY-MM-DD HH:MM:SS”. So, the problem, i think, is how to show the datetime in a particular format you want so you can use time.Format to apply the pattern you want.

So that means that if I want to store in my preferred format then I must use the text type and not datetime, correct?

Yes, I think so…

I thought gorm would do that for me with a custom datatype.

I know I am late to this question…

How the database is storing the data should be irrelevant, in my humble opinion. Any application that uses the data are responsible for displaying it in the correct format for the specific user, and that includes whatever tool you are using to browse the database (MySQL Workbench, SSMS,…) as well as your program.

So, in short, I think that storing a date in a string is a really bad idea…instead you should focus on how to DISPLAY the date correctly, not how to STORE the date correctly.

1 Like

the Date is not stored as a string, that is just they default format. You should always used ISO8601 FULL on the backend and only DISPLAY the Date or TimeStamp in a different LOCALE on the front end.

Trust me, you DO NOT want to store the Dates as that messed up traditional US format in the backend.

The default format sorts naturally and is UNAMBIGUOUS about what fields are what.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.