GORM PostgreSQL getting date column without timezone

Issue

I’m learning golang atm, and currently I’m using gorm trying to select query getting date column, but it keep returning ‘2020-01-10T00:00:00Z’, how do I get rid of the timezone?

I’ve tried changing date to time.Time or string, nothing works, here is the code

type Price struct {
    DateStay time.Time `json:"date"`
    Price    int       `json:"price"`
}

Update:
This is the code that I am using

        var price []models.Price
        err = models.DB.Raw(`
            SELECT P.date_stay, P.price
            FROM prices p
            WHERE P.room_type_id = ?
        `, roomTypeId).Scan(&price).Error

I tried to P.date_stay::date, date(P.date_stay) on the query but nothing works

I expect it to return ‘2020-01-10’

Solution

Using time.Time as a type for the date is probably best.

You can format the result of the date by setting the format to the desired date format you want.
(dd-mm-yyyy or whatever order you please).

Then you can format the value using time.Parse(format, date)

format := "2000-01-13"
formattedDate, err := time.Parse(format, price.DateStay)

assuming price is a result from your select query.

If you time is a time.Time you can try using price.DateStay.Format(format)

Answered By – WebstraDev

Answer Checked By – Katrina (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.