Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a date string to datetime compatible to postgresql

I have a date string in the format,

"dd/mm/yyyy"
e.g="23/2/2017"

How can I convert this to a valid format, so that I can save this value in Datetime field of postgresql.

I tried using datetime package, but not getting.

like image 566
Bhimasen Avatar asked Jul 23 '26 12:07

Bhimasen


2 Answers

in Postgres you can "adjust" your datestyle, eg:

t=# set datestyle TO DMY;
SET
Time: 0.215 ms
t=# select '23/2/2017'::timestamptz;
      timestamptz
------------------------
 2017-02-23 00:00:00+00
(1 row)

or just "parse" with right mask (ignoring not suitable datestyle), eg:

t=# select to_timestamp('23/2/2017','DD/MM/YYYY');
      to_timestamp
------------------------
 2017-02-23 00:00:00+00
(1 row)
like image 105
Vao Tsun Avatar answered Jul 25 '26 10:07

Vao Tsun


ALTER TABLE <tablename> ALTER COLUMN <columnname> TYPE DATE using to_date(<columnname>, 'DD-MM-YYYY');
like image 31
Klesley Gonçalves Avatar answered Jul 25 '26 09:07

Klesley Gonçalves