Skip to content

Instantly share code, notes, and snippets.

@CHAOWEICHIU
Created April 4, 2020 22:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CHAOWEICHIU/649867fc5418a523016592d7fdd1421a to your computer and use it in GitHub Desktop.
Save CHAOWEICHIU/649867fc5418a523016592d7fdd1421a to your computer and use it in GitHub Desktop.
const data = [
'2020/03/03 下午 12:55:10',
'2020/03/03 下午 03:43:56',
'2020/03/04 下午 12:56:14',
'2020/03/04 下午 03:42:10',
'2020/03/05 下午 12:54:55',
'2020/03/05 下午 03:40:22',
'2020/03/06 下午 12:56:30',
'2020/03/06 下午 03:41:13',
'2020/03/10 下午 12:53:36',
'2020/03/10 下午 03:43:36',
'2020/03/11 下午 12:56:47',
'2020/03/11 下午 03:40:24',
'2020/03/12 下午 12:56:42',
'2020/03/12 下午 03:41:21',
'2020/03/17 下午 12:52:06',
'2020/03/17 下午 03:40:47',
'2020/03/18 下午 12:57:54',
'2020/03/18 下午 03:41:20',
'2020/03/19 下午 12:52:06',
'2020/03/19 下午 03:46:39',
'2020/03/24 下午 12:51:41',
'2020/03/24 下午 03:42:21',
'2020/03/26 下午 12:49:51',
'2020/03/26 下午 03:47:42',
'2020/03/27 下午 12:53:14',
'2020/03/27 下午 03:45:55',
]
/* Implementation */
const moment = require('moment')
moment.suppressDeprecationWarnings = true
moment.locale('zh')
const customInputFormat = 'YYYY/MM/DD A hh:mm:ss'
if (data.length % 2 !== 0) {
throw new Error(`data set is in odd, length: ${data.length}`)
}
const momentFormatDataSet = data
.map(d => d.replace('下午', 'PM'))
.map(d => d.replace('上午', 'AM'))
.map(d => moment(d, customInputFormat))
const dataSetResult = momentFormatDataSet.reduce(
(container, currentValue, currentIndex, all) => {
if (currentIndex % 2 !== 0) {
const time = all[currentIndex].diff(
all[currentIndex - 1],
'minutes',
)
return [...container, time]
}
return container
},
[],
)
console.log(dataSetResult)
@CHAOWEICHIU
Copy link
Author

try it out here CodePen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment