0

Similar questions has been asked many times but I couldn't find a more concrete solution. The things I'm doing are:

  1. I have a <input type="date"> inside my HTML which when clicked opens a calender with date in dd/mm/yyyy format.
  2. 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.
  3. 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('/').
  4. 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.
  5. 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:
  6. 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.
  7. 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

2 Answers 2

1

Sounds like you are doing waaay to many conversions. I would argue that there should only be one way dates are represented: as Date objects in the programming language. There are only a few conversions that need to happen:

Date <=> Integer milliseconds since the epoch to pass to server
Date <=> String human-readable format to display to user

Any thing beyond this is asking for trouble. Comparisons can be made by casting to int date.getTime(), comparing, and casting back to Date. Ditto for additions. Note that Date.parse is implementation dependent in what it will accept, although all of them will accept ISO 8601 formatted date strings anything else is guesswork. Which means you will have to deal with converting strings by hand, something like the following:

var toDate = str => {
   var splitter = str.indexOf("/") === -1 ? "-" : "/";
   var [mon, day, year] = str.split(splitter);
   return new Date(year, mon - 1, day);
};

var toDateString = date => {
  return "" + date.getFullYear() + (date.getMonth() + 1) +...
};

Note that there's no validation, that's left as an exercise to the reader.

A WORD ABOUT MOMENT.JS

moment.js is awesome. Its also huge, its a kitchen-sink API with a heft to match. You're already loading angular, so think carefully before bulking the size of your payload with another huge library.

Sign up to request clarification or add additional context in comments.

3 Comments

Good points, it might just be a matter of re-coding things. But I wouldn't say moment is huge. It's 3000-4000 lines (that's uncompressed, it would be a lot smaller if it were compressed). Angular uncompressed is like 32000 lines. But it is important to keep script size to a minimum.
splitter can be /\D/ so any non-digit will do.
Thanks for the answer. I finally created a factory with multiple date conversion methods inside it so I can inject and use it in other controllers. Helped me a ton.
0

Moment.js is a powerful date formatting and manipulation library. A lot of things you can do in Moment.js are a single line of code, which makes life a lot easier. I agree, without using a library like this date formatting and handling can be a pain.

http://momentjs.com/

EDIT: fyi, I use this with my Angular app and find it extremely useful!

3 Comments

This should be a comment, it's not an answer.
I intended it as an answer to his problem. :)
@sam if this is an answer, then the question should be closed as off-topic for asking for a tool/library. Someone (not me) has already voted to close it. I think that's a little harsh, so I answered it as written.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.