There is a Postgresql database, it has a label with date fields

journal=# show styleshift; ERROR: unrecognized configuration parameter "styleshift" journal=# show datestyle; DateStyle ------------- German, DMY (1 row) journal=# 

Driver to access the database
https://github.com/lib/pq

The date format I need is 02/01/2006, it is this in the database

 journal=# select * from j_nar limit 2; id | id_patient | number_nar | date_open_nar | date_close_nar ----+------------+------------+---------------+---------------- 34 | 33 | 777 | 01.03.2017 | 05.03.2017 35 | 34 | 111 | 18.03.2017 | 18.03.2017 (2 rows) journal=# 

From the html form, the data comes in this form:

2017-04-01

The database is already stored as follows:

04/01/2017

From the server on the frontend output as follows.

 type ListNar struct { Id int64 `json:"Id"` IdPatient int64 `json:"IdPatient"` NumberNar string `json:"NumberNar"` DateOpenNar string `json:"DateOpen"` DateCloseNar string `json:"CloseOpen"` } func ModelListNar(id string)[]*ListNar { query := "SELECT id, id_patient, number_nar, date_open_nar, date_close_nar FROM j_nar WHERE id_patient = " + id + " order by id" rows := db.Select(query) bks := make([]*ListNar, 0) for rows.Next() { bk := new(ListNar) rows.Scan(&bk.Id, &bk.IdPatient ,&bk.NumberNar, &bk.DateOpenNar, &bk.DateCloseNar) bks = append(bks, bk) } return bks } func ListNar(w http.ResponseWriter, rnd render.Render) { bks := ModelListNar(currentId.id) b, _ := json.Marshal(bks) fmt.Fprintf(w, "%s \n", b) } 

On the frontend I get data of this kind:

2017-04-01T00: 00: 00Z

In the structure, I have not specified the format of the time.Time field. Why is this happening and how to fix it.

  • What is the type of date_open_nar and date_close_nar in your database? - Ainar-G
  • date, in german, dmy format - Evgeny Gusev

1 answer 1

There are a lot of problems in the code, let's start with your question. Retrieving the date from the database in the string is wrong. Of course, you do not change the string in JSON during encoding. The DateOpenNar and DateCloseNar must be modifications of the type time.Time . For example:

 type myTime struct { time.Time } func (t myTime) MarshalJSON() ([]byte, error) { s := t.Format("02.01.2006") return []byte(s), nil } func (t myTime) Value() (driver.Value, error) { return pq.FormatTimestamp(t.Time), nil } func (t *myTime) Scan(src interface{}) error { switch v := src.(type) { case string: t.Time, err := pq.ParseTimestamp(time.UTC, v) if err != nil { return err } case []byte: t.Time, err := pq.ParseTimestamp(time.UTC, string(v)) if err != nil { return err } default: return fmt.Errorf("can't read %T into myTime", v) } } 

In addition, there is a terrible SQL injection in your code:

 query := "SELECT id, id_patient, number_nar, date_open_nar, date_close_nar FROM j_nar WHERE id_patient = " + id + " order by id" rows := db.Select(query) 

So now, even in PHP do not. Correctly do:

 query := "SELECT id, id_patient, number_nar, date_open_nar, date_close_nar FROM j_nar WHERE id_patient = $1 order by id" rows, err := db.Select(query, id) 

where id is int64 . In addition, you do not check for errors (which is a mortal sin), and your code does not match the style .

  • About sql injection thanks, I will correct. I will check for errors. As for Time.time, I’m doing this, I wanted to get the time format I needed. - Evgeny Gusev
  • one
    @ Evgeny Gusev Added a driver.Valuer and sql.Scanner interface implementation, so that it is automatically read and written to the database. - Ainar-G