0

How to set a date format like this (2020-03-10) when submitting a edited form. That form is currently reading a current data from DB (e.g 20-10-1990). I find below solution, the formating is what I want but it is reading a today's date (2020-03-27), not as what as current DB data.

I must send in YYYY-MM-DD format, DB will save as 0000-00-00 00:00:00 if not follow that format.

Current Date as in DB

enter image description here

Console.log when submitting using code below

enter image description here

Code that I am using to get YYYY-MM-DD

var d = new Date();
editProjectPlanStart = [
    d.getFullYear(),
    ('0' + (d.getMonth() + 1)).slice(-2),
    ('0' + d.getDate()).slice(-2)
].join('-');
mastersuse
  • 936
  • 1
  • 15
  • 35
  • you can pass the db data inside the date constructor like so `var d = new Date(db_date_here);` – Karma Blackshaw Mar 27 '20 at 02:17
  • You could use a regex like `("20-10-2020").replace(/([0-9]+)-([0-9]+)-([0-9]+)/, '$3-$2-$1')` – Dominique Fortin Mar 27 '20 at 04:42
  • This answer may help https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date/60881661#60881661 at least in node.js you get this format ;-) Or there is a bunch of other ways in that thread. But some methods may change zone/time to/from UTC/local... – Jan Mar 27 '20 at 07:36

0 Answers0