Created
April 4, 2020 22:48
-
-
Save CHAOWEICHIU/649867fc5418a523016592d7fdd1421a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try it out here CodePen