1

Hi I'm having an array of date example [2019-02-21T04:06:32.000Z] and I want to convert the date into desired format [02/21/2019 4:06:32 AM]. So I'm using javascript map

dateArray.map( x => {
   return moment.tz(x, 'Etc/UTC').format('MM/DD/YYYY h:mm:ss A').toString()
});

after this I console date array but it is still showing [2019-02-21T04:06:32.000Z] but inside map() it is showing in desired format. What I'm doing wrong here? Can any one help me to solve this? Thank you.

2
  • Map method returns new array. Commented Feb 21, 2019 at 8:50
  • 1
    map doesn't modify the array in-place, it will return a new array instead (use: dateArray = dateArray.map(...)) Commented Feb 21, 2019 at 8:50

2 Answers 2

7

.map is not mutating the existing array. You should do :

dataArray = dateArray.map( x => {
  return moment.tz(x, 'Etc/UTC').format('MM/DD/YYYY h:mm:ss A').toString()
});
Sign up to request clarification or add additional context in comments.

Comments

0

If you must change the existing array then use a for loop.

dateArray = ['2019-02-21T04:06:32.000Z'];

for(let idx = 0; idx < dateArray.length; idx++){
  dateArray[idx] = moment.tz(dateArray[idx], 'Etc/UTC')
    .format('MM/DD/YYYY h:mm:ss A').toString();
}

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.