Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MomentJS not returning correctly formatted date #196

Open
stevemacn opened this issue Mar 29, 2017 · 4 comments
Open

MomentJS not returning correctly formatted date #196

stevemacn opened this issue Mar 29, 2017 · 4 comments
Labels
Milestone

Comments

@stevemacn
Copy link

When modifying the awesome theme. The following line should return May 2015:

h.formatDate("2015-05","MMM YYYY")

For every possible month (even full dates "2015-05-10") it always returns Jan XXX where XXX is the correct year passed to Moment. Is there something I'm missing? Looking at the coffeescript I couldn't find anywhere obvious that a mistake was made.

@hacksalot
Copy link
Owner

Thanks for the report. Investigating...

@hacksalot hacksalot added this to the v1.9.0 milestone Jan 29, 2018
@hacksalot
Copy link
Owner

hacksalot commented Feb 14, 2018

I believe this is resolved in latest (v1.9.0-beta). If not, let me know-- the version with that bug is a couple years old.

@hacksalot hacksalot added the bug label Feb 14, 2018
@kapgit
Copy link

kapgit commented Mar 18, 2018

I have the same problem with

  • HackMyResume v1.9.0-beta
  • node v9.8.0
  • Windows 10 Pro

Context:

I'm using hackmyresume to generate a Word resume.

hackmyresume build myfreshresume.json to wordresume.doc -t mytemplate-doc.xml

Extract of myfreshresume.json (FRESH schema):

...
  "education": {
    "level": "",
    "history": [
      {
        "institution": "Some institution",
        "end": "2017-02-12",
        "curriculum": [
          "Some curriculum"
        ],
        "summary": "",
        "area": "Some area",
        "studyType": "Certification"
      }
    ]
  },
...

In the Word 2003 XML template file mytemplate-doc.xml (using handlebars templating notation):

...
{{#each r.education.history}}{{formatDate end "MMM YYYY"}}
...

Problem

When running hackmyresume, I get the actual output 'Jan 2017' instead of the expected 'Feb 2017'.

Analysis

I had a quick look in the HackMyResume code, more specifically in .\npm\node_modules\hackmyresume\dist\helpers\generic-helpers.js.

  1. The generic helper function 'formatDate' expects a parameter 'dtFormat':
@param {string} [dtFormat='YYYY-MM'] The desired datetime format. Must be a Moment.js-compatible datetime format.

I understand from this description that 'dtFormat' is meant to be the output format defining how a given date should be formatted in the generated resume.

  1. However, 'dtFormat' passed to the 'moment' constructor (line 70) is the format used to parse the given date:
momentDate = moment(datetime, dtFormat);

as documented in https://momentjs.com/docs/#/parsing/string-format/:

moment(String, String);

If you know the format of an input string, you can use that to parse a moment.

moment("12-25-1995", "MM-DD-YYYY");

My conclusion and workaround

As the date format used in the resume JSON file ("YYYY-MM-DD") may be different than the desired format in the output resume (here: "MMM YYYY"), I believe the 'dtFormat' parameter should not be passed to the 'moment' constructor.

To test this, I replaced 'dtFormat' with a hard-coded "YYYY-MM-DD" (format used in the JSON resume) on line 70. This workaround seems to work as expected. Now the date output is 'Feb 2017'.

...
      if (String.is(datetime)) {
        // If a string was passed in, convert to Moment using the 2-paramter
        // constructor with an explicit format string.
        momentDate = moment(datetime, "YYYY-MM-DD" /*dtFormat*/);
        if (momentDate.isValid()) {
          return momentDate.format(dtFormat);
        }
...

BTW, I noted a similar issue in the 'date' generic helper function, defined at line 97, and 'moment' constructor at line 116. Worked around it with

dateValueMoment = moment(dateValue,"YYYY-MM-DD" /* dateFormat*/);

@hacksalot
Copy link
Owner

hacksalot commented Mar 18, 2018

Thanks for the awesome writeup. Date handling code needs to be rewritten for v2.0 until then I think your solution is workable. We're not "agnostic" enough in the handling where we need to be and we're not specific enough when we can be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants