MadMG / moment-jdateformatparser

Translates the `java.text.SimpleDateFormat` date format to the `moment.js` date format.
MIT License
58 stars 29 forks source link

Quoted strings #7

Closed anttileppa closed 7 years ago

anttileppa commented 9 years ago

Quoted strings are interpret incorrectly.

SimpleDateFormat:

System.out.println(new SimpleDateFormat("d.' of 'MMM").format(new Date()));

prints out _14. of Feb_ when:

console.log(moment().formatWithJDF("d.' of 'MMM")); 

prints _14.' of 'Feb_

anttileppa commented 9 years ago

Workaround for this is to replace SimpleDateFormat quotes with square brackets before passing the formatString into formatWithJDF method.

e.g.

var end = false;
var fixed = formatString.replace(/\'/g, function(match) {
  return (end = !end) ? '[' : ']';    
});
herom commented 9 years ago

hey @anttileppa - thanks a lot for raising this issue and sorry for the long delay on answering your question/issue. if you have already an existing solution to the problem I'd ask you to open a PR so that we can include your fix into our next release - this would be awesome :+1:

gerpres commented 8 years ago

the workaround doesn't work for the following pattern

yyyy-MM-dd[T]HH:mm:ss.SSS[Z]

the 'Z' is translated to ZZ

herom commented 8 years ago

I can't see any quotes in the pattern you posted @gerpres ? The workaround @anttileppa posted was for "escaping" quotes 😃

gerpres commented 8 years ago

sorry for that :-)

the pattern:

yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

doesn't work. neither with '' nor with []

the Z inside the quotation is always replaced by ZZ

herom commented 8 years ago

ah, I see! thank you - I'll take a look at it and see what I can come up with in order to make the string escaping work 👍

gerpres commented 8 years ago

have a look at...

moment.fn.toMomentFormatString = function (formatString) { if (!javaDateFormats[formatString]) {

      var mapped = ""; 

      var regexp = /[^']+|('[^']*')/g;

      while((part = regexp.exec(formatString))) {

          part = part[0];

          if(part.match(/'.?'/)) {
              mapped+="["+part.substring(1,part.length-1)+"]";
          } else {
              mapped+=translateFormat(part, javaFormatMapping)
          }
      }

      javaDateFormats[formatString] = mapped
  }
  return javaDateFormats[formatString];
};
herom commented 7 years ago

closing due to the merge of PR #19 🥇