-2

I need to convert a string in this format MM/yyyy to a Date() object in Angular. I'm getting an Invalid Date error while doing: new Date('01/2020')

How do I instantiate a Date object using this specific format?

Reactgular
  • 43,331
  • 14
  • 114
  • 176
Daniel André
  • 1,004
  • 1
  • 12
  • 26
  • Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – Jacopo Sciampi Dec 12 '19 at 15:19
  • @JacopoSciampi Not really. Not only is the conversion from Date to string, but it is using javascript and not the superset language – Daniel André Dec 12 '19 at 15:24
  • 1
    Dates need a day representation, cause it has to know which day of the month it's going to represent. if you just want the month and year from that date object, you just have to hardcode it to a specific day of the month. Remember, month starts from cero to 11. – Jose Anibal Rodriguez Dec 12 '19 at 15:25
  • 1
    Does this answer your question? [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – d4nyll Dec 12 '19 at 15:31
  • 1
    @DanielAndré what you asked is to manage a date in typescript. The way you manage dates objects in TS is using Javascript. for the Date to string part is another thing. Indeed Typescript is a superset of Javascript, that's why certain things such as Typo manage (Date, string, int and so on) are handled directly in js. – Jacopo Sciampi Dec 12 '19 at 15:46
  • `let [m, y] = '01/2020'.split('/'); new Date(y, m-1)`. – RobG Dec 12 '19 at 23:06

2 Answers2

1

You are passing a month and year without a day. The Date object needs the day also for constructing the date. A valid date is consists of day, month and year.

Pass the day also:

new Date('01/01/2020')

If you do not want to pass the day:

const str = '01/2020';

const year = str.substr(3,6);
const month = str.substr(0,2);

new Date(year, month - 1)

You need substr method to select year and month from your string. The month starts from zero. If 01 is January for you then you need to substruct 1 from the month.

Maihan Nijat
  • 7,755
  • 6
  • 40
  • 87
  • I do not want to pass the day, hence the question. – Daniel André Dec 12 '19 at 15:23
  • 1
    There is no need to convert to number, `new Date(year, month - 1)` is fine (note adjustment of calendar month number). If there is more than one argument, they are converted to number by default. The first part of this answer should be modified, parsing non–standard strings with the built–in parser is a bad idea. – RobG Dec 12 '19 at 23:01
  • @RobG Thanks for the suggestion. I revised the answer. – Maihan Nijat Dec 13 '19 at 13:14
1

You can just parse the string and use this Date constructor:

const year = 2020;
const month = 2; // March

const date = new Date(year, month);
// Constructors:

new Date();
new Date(value);
new Date(dateString);
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
Roberto Zvjerković
  • 7,065
  • 2
  • 19
  • 35