简体   繁体   中英

Get date and year from iso date in db

Suppose I have multiple array of isodate object as:

const apple = [{"readDetail":2021-04-17 14:23:26.978Z},{"readDetail":2021-02-18 14:23:26.978Z}, 
               {"readDetail":2019-04-18 14:23:26.978Z}]


Expected O/P = [  "April 2021","April 2020", "Feb 2021",]

Is it possible to convert to array of object to the expected O/P?

I tried looking for answers I could not find the answer. Please anyone has any question please let me know.

You can use Array#map with Date#toLocaleString .

 const apple = [{"readDetail":'2021-04-17 14:23:26.978Z'},{"readDetail":'2021-02-18 14:23:26.978Z'}, {"readDetail":'2019-04-18 14:23:26.978Z'}] const res = apple.map(({readDetail})=> new Date(readDetail).toLocaleString('en', {month: 'long', year: 'numeric'})); console.log(res);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM