2

My backend is built using java and the user settings API returns datetime formats to use within the view using Java standards (datetimes are later returned in ISO8601 and formatted on the fly). Example in Java 'yyyy-MM-dd' should be converted to 'YYYY-MM-DD' to be used with javascript's library momentjs.

Is there a direct way to convert symbols from java to javascript libraries (momentjs or date-fns)?

Vincent Peres
  • 1,003
  • 1
  • 13
  • 28
  • If server and client may be in different timezones and that datetime should be displayed in a timezone local to the user, then I'd suggest not to send it as a String at all, but instead to send UTC timestamp, that client then can properly display. – Mirek Pluta Nov 18 '21 at 11:14
  • 1
    That's already handled by sending ISO8601 datetimes with UTC. Thanks – Vincent Peres Nov 18 '21 at 11:24
  • Do I understand correctly that you want to somehow automatically convert the format/pattern strings? – Hulk Nov 18 '21 at 12:14
  • And what exactly do you mean with "javascript datetime tokens"? `Date` objects? – Thomas Nov 18 '21 at 13:13
  • @Hulk yes. Translate https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html "Patterns for Formatting and Parsing" to their Javascript equivalent. – Vincent Peres Nov 18 '21 at 13:31
  • I have edited the question to be more specific as there is no default javascript date object method that would output a given format using symbols but the equivalent would be to use momentjs. – Vincent Peres Nov 18 '21 at 13:41
  • You might instead use a formatter that uses Java tokens, e.g. [this answer](https://stackoverflow.com/a/57302039/257182) using PHP tokens. – RobG Nov 19 '21 at 22:00

2 Answers2

2

momentjs has its own specific format symbols which was confusing, whereas other libraries like date-fns are following the LDML standard. I ended up using date-fns as Java is having the same standard.

https://date-fns.org/v2.26.0/docs/Unicode-Tokens

https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table

Vincent Peres
  • 1,003
  • 1
  • 13
  • 28
  • 1
    You could also use [JS-Joda](https://js-joda.github.io/js-joda), which is a port of the Java 8 Date and Time API (JSR 310). I bet they are using the same pattern letters. – MC Emperor Nov 23 '21 at 11:27
0

This will be a pretty hand-held operation. Also you may not be able to cover all cases, but certainly the most common ones. The following should get you started.

function replacer(match, offset, string) {
  switch (match) {
    case 'M': return 'M';
    case 'MM': return 'MM';
    case 'MMM': return 'MMM';
    case 'MMMM': return 'MMMM';
    case 'd': return 'D';
    case 'dd': return 'DD';
    case 'D': return 'DDD';
    case 'DDD': return 'DDDD';
    case 'yy': return 'YY';
    case 'yyyy': return 'YYYY';
    default: return '(not supported)';
  }
}

var javaPattern = 'yyyy-MM-dd';
var regEx = /([a-z]+)/g;
var javaScriptPattern = javaPattern.replace(regEx, replacer);
console.log(javaScriptPattern);

Please go through the Java pattern letters and the JavaScript pattern letters in the two links below and expand the translations in my code as needed.

Links

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    The tokeniser has to be a little smarter than `/([a-z]+)/` since delimiters between tokens aren't mandatory, e.g. yyyyMMdd. You need to collect characters one at a time until the pattern no longer matches a token, e.g. the parser in [*this answer*](https://stackoverflow.com/a/57302039/257182). :-) – RobG Nov 18 '21 at 20:25
  • Thanks for your answer, I wanted to avoid doing this. It seems like some other libraries like date-fns are following the LDML format which is what Java is using. https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table – Vincent Peres Nov 23 '21 at 11:15