Similar questions has been asked many times but I couldn't find a more concrete solution. The things I'm doing are:
- I have a
<input type="date">inside my HTML which when clicked opens a calender with date in dd/mm/yyyy format. - I change the html5 date to timestamp to send to my db by
Date.parse(html5Date)and in the server I modify the date and send it back to my Angular app. - I now convert the timestamp back to Date object by
new Date(timestamp).To print the date in a human-friendly format inside a table I do[date.getDate(), date.getMonth() + 1, date.getFullYear()].join('/'). - On edit (PUT request), I again capture the date from HTML, convert it to timestamp, send it to server and process the returning date back to html date.
- Other than these, I also do a ton of functionalities like date comparison, adding hours to the dates, show time of the day etc inside the HTML:
- Just these simple operations are over 120 lines of code which I think is ridiculous and error prone. I've looked into Angular Datepicker but it's a bit confusing. Also sometimes the HTML date is of type Object and sometimes it's String so Date.parse() gives error.
- Are there any developer friendly methods that does : copy HTML5 date (from datepicker) --> change to timestamp (for angular&server) --> format timestamp back to string/object (for html)? Thank You :)
Note: Angular throws a lot of annoying error in console saying dateformat is wrong (being html date type) but doesn't stop code from running