Date-fns: bug(format()): gives incorrect Month

Created on 10 Jan 2020  ·  3Comments  ·  Source: date-fns/date-fns

Hi there guys!

I am maintainer of https://github.com/rxdi/xmigrate which is a Migration library written in Typescript for mongodb. Inside the library there is a method which creates Migration template for different versions of javascript(ES2015, ES6, Typescript).

When we create migration as a file we give it a name which is Now.

I am using that logic to handle conversion of Date to specific format

https://github.com/rxdi/xmigrate/blob/master/src/helpers/date.ts

import { format } from 'date-fns';

export const now = (dateString = Date.now()) => {
  const date = new Date(dateString);
  return new Date(
    date.getUTCFullYear(),
    date.getUTCMonth(),
    date.getUTCDate(),
    date.getUTCHours(),
    date.getUTCMinutes(),
    date.getUTCSeconds(),
    date.getUTCMilliseconds()
  );
};

export const nowAsString = () => format(now(), 'yyyymmddhhmmss');

Since yesterday till i got an email for Issue related with xmigrate i was not having problems

https://github.com/rxdi/xmigrate/issues/8

I have created a test for this scenario and i found out that is obvious it gives the wrong result

import { nowAsString } from "./date";

describe("Date helper", () => {
  it("Should return correct date", async () => {
    console.log(nowAsString());
  });
});

As you can see we have 20205710095702 but i think it is not possible to have 57 months inside one year right ? :D

 PASS  src/helpers/date.spec.ts
  Date helper
    ✓ Should return correct date (12ms)

  console.log src/helpers/date.spec.ts:5
    20205710095702

I have tested this also with version 2.9.0 and the result is the same

Any ideas ?

@kossnocorp

Friend of mine gives me some article related with Y2K problem https://www.newscientist.com/article/2229238-a-lazy-fix-20-years-ago-means-the-y2k-bug-is-taking-down-computers-now/

is there any chance that we are facing this now ?

🤹‍♂️ Support

Most helpful comment

Hi, you used token for minutes mm instead of months MM.

All 3 comments

Hi, you used token for minutes mm instead of months MM.

@kossnocorp Thanks ! Have a great day! :)))

Awesome fast response :+1:

Thanks for having you in the community :)

I appreciate the kind words!

Was this page helpful?
0 / 5 - 0 ratings