I have date ranges called from and to. I want to convert it to weeks in the range of Sunday to Saturday. Eg we have a range of 01-10-2014 - 31-10-2014
Then week data should be :
01-10-2014 - 04-10-2014
05-102014 - 11-10-2014
12-10-2014 - 18-10-2014
19-10-2014 - 25-10-2014
26-10-2014 - 31-10-2014
How can this be achieved using moment.js
?
我认为你的问题在这里有类似的答案,你可以在这里参考:: https : //stackoverflow.com/a/18884615/12772355
Use .diff()
method of moment.
const ranges = [ { from: '01-10-2014', to: '04-10-2014' }, { from: '05-10-2014', to: '11-10-2014' }, { from: '12-10-2014', to: '18-10-2014' }, { from: '19-10-2014', to: '25-10-2014' }, { from: '26-10-2014', to: '31-10-2014' }, { from: '01-10-2014', to: '31-10-2014' }, { from: '09-10-2014', to: '31-10-2014' } ]; for (const range of ranges) { const dateFormat = 'DD-MM-YYYY'; const diff = Math.abs(moment(range.from, dateFormat).diff(moment(range.to, dateFormat), 'weeks', true)); console.log(diff.toFixed(2)); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
If you don't need a floating number like result, remove the true
like thirt parameter of .diff()
and the .toFixed(2)
.
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.