search

Home  >  Q&A  >  body text

javascript - How to copy moment

const a = moment(); 
console.log('a', a); // 4月24日
const b = a.add(1, 'days');
console.log('a', a);// 4月25日
console.log('b', b);// 4月25日

If you copy a, while b is April 25, a remains unchanged.

淡淡烟草味淡淡烟草味2749 days ago659

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-16 13:44:33

    In fact, moment itself supports the function you mentioned. You only need to put the variable you want to copy into moment and generate another one. The two will not affect each other.

    const a = moment(); 
    console.log('a', a); // 4月24日
    const b = moment(a).add(1, 'days');   //moment一下,完成对a的复制
    console.log('a', a);// 4月24日
    console.log('b', b);// 4月25日

    reply
    0
  • 迷茫

    迷茫2017-05-16 13:44:33

    const a_clone = a.clone();

    reply
    0
  • Cancelreply