我在单个字符串“ 03.09.2020 – 08.11.2020”中具有以下格式的两个日期,我需要将这两个日期分成6个不同的变量:
startDay,startMonth,startYear,endDay,endMonth,endYear
最好的方法是什么?一个for循环?表现明智
使用split
和一些数组解构:
const initial = '03.09.2020 – 08.11.2020'
const [ start, end ] = initial.split('–')
const [ startDay, startMonth, startYear ] = start.split('.')
const [ endDay, endMonth, endYear ] = end.split('.')
console.log(`startDay: ${startDay}`)
console.log(`startMonth: ${startMonth}`)
console.log(`startYear: ${startYear}`)
console.log(`endDay: ${endDay}`)
console.log(`endMonth: ${endMonth}`)
console.log(`endYear: ${endYear}`)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句