Sugar icon indicating copy to clipboard operation
Sugar copied to clipboard

Adding formatter mixed with tokens

Open PottertheJavaOtter opened this issue 6 years ago • 3 comments

I am trying to parse when the date reads 26m ago or 22h ago.

I assumed Date.getLocale('en').addFormat('(\\d+)m {sign}', ['minutes']); would work. However, I am without luck. Is there a way to use the regular tokens and custom arguements as well>

PottertheJavaOtter avatar Apr 25 '18 20:04 PottertheJavaOtter

Basically when passing an array it ignores the normal tokens in favor of custom ones. However this is a good feature request. Assuming you only need English then I've confirmed that this should work:

Date.getLocale('en').addFormat('(\\d+)m (ago|from now)', ['minutes', 'sign'])

andrewplummer avatar Apr 26 '18 00:04 andrewplummer

Sounds great, if I can understand the parsing works I will try to help out as well. Regex is definately not one of my strong points.

@andrewplummer I have tried that and various other combinations. I think its just defaulting to minutes is whatever you put in there and not the 'ago' part.

I tried this on the Sugar site in console and I get this result:

`Date.getLocale('en').addFormat('(\d+)m (ago|from now)', ['minutes', 'sign'])

undefined Date.create('26m ago');

Thu Apr 26 2018 09:26:00 GMT-0400 (EDT)` I think its just defaulting to 26 mintues because the time I put it in was at 9:46 so I think the ago part is not working atm

PottertheJavaOtter avatar Apr 26 '18 13:04 PottertheJavaOtter

Alternatively I tried this as well, and I am still getting issues. Date.getLocale('en').addFormat('{mm}m {sign}'); But same it defaults to minutes but not the ago part.

PottertheJavaOtter avatar Apr 26 '18 13:04 PottertheJavaOtter