-2

I have an array of number

var year = [1999,2000,2001,2002,2003];

and I need to convert to date-time formar

var yearConvert =["1999-00-00","2000-00-00","2001-00-00","20002-00-00","2003-00-00"];

I only have year which is an integer

2

2 Answers 2

0

You can use map and concat -00-00 on each element

var year = [1999,2000,2001,2002,2003];

let op = year.map(e => e+'-00-00')

console.log(op)

Sign up to request clarification or add additional context in comments.

Comments

0

You can use Array#map to do it. But the returned array will have the values as string not as Date object.

const year = [1999,2000,2001,2002,2003];
const dateString = year.map((ele) => `${ele}-00-00`);
console.log(dateString);

//Convert this array to Date Objects using Date constructor Date(year, monthIndex, day) month starts from '0'

const date = dateString.map((str) => { 
  const data = str.split('-');
  return new Date(data[0], data[1], data[2]+1);
});

console.log(date)

//To verify parse to date strings

const dateParsed = date.map((date) => date.toDateString());
console.log(dateParsed);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.