0

I am building a self-depreciating list of dates and am using Date.parse() to convert rich text into a timestamp. Right now, we have a system where the user enters the date themselves, and I use a token on the backend to pull it through to that specific page.

I am trying to 'protect the user' from entering a wrong date format.

Currently, if they enter January 12th, 2016 it won't be able to parse and create a timestamp due to th. However, if they enter January 12, 2016 it can create a timestamp.

Is there a way to replace these ordinal numbers on a dynamic string? I've tried a simple jquery find/replace, but have had no luck with getting it to remove the ordinal date texts.

What I've tried:

$('.my-button').each(function() {
    console.log($(this).text());
    var text = $(this).text().replace('st, ', '');
    $(this).text(text);
});

Any ideas on a solution, or a way to make Date.parse(): accept these extra characters?

knocked loose
  • 3,142
  • 2
  • 25
  • 46
  • How are free form dates getting into the system? Is it through a web page or outside of your control? – traktor Apr 12 '16 at 23:42
  • Possible duplicate of [Parsing a date’s ordinal indicator ( st, nd, rd, th ) in a date-time string](http://stackoverflow.com/questions/28514346/parsing-a-date-s-ordinal-indicator-st-nd-rd-th-in-a-date-time-string) – traktor Apr 12 '16 at 23:53
  • **Do not** use Date.parse at all, it's largely implementation dependent and inconsistent. Parsing strings with the Date constructor is equivalent. The only reliable way to parse date strings is to use a parser, either one you've written or a library (moment.js is good but there are others less bulky). And when using a parser, you **must** specify the format, otherwise you leave correct parsing to chance. I don't understand the use of "self-depreciating" in this context. – RobG Apr 13 '16 at 00:31
  • @Traktor53—that answer is for Java, not javascript (which does not have a decent parser built–in). – RobG Apr 13 '16 at 00:33

1 Answers1

0

For the simple case of removing an ordinal from a date string like "January 12th, 2016", you can use a regular expression like:

var re = /(\d{1,2})[a-z]{2}\b/i;

['January 12th, 2016','January 1st, 2016','January 2nd, 2016','20th February, 2015'].forEach(
  function(s) {
    document.write('<br>' + s.replace(re,'$1'));
  }
);

However, parsing strings with Date.parse (and the Date constructor, they are equivalent for parsing) is strongly recommended against. Use a parser and define the format, do not leave it to chance.

There are libraries like moment.js, however it may be too big for your requirements. There are other dedicated parsers (check on GitHub), or if you only need to support one format, write one yourself. It only requires three or four of lines of code.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • This would work except the user enters the text on their side, I don't see it. So I have to be able to pull the text from the element they are writing in. – knocked loose Apr 13 '16 at 12:46