moment. js time and date processing details
- 2021-11-14 05:05:39
- OfStack
Week 1 to Sunday time formatting conversion
(Y--Year M--Month D--Day)
var timeNow = new Date( ) // Current time
var weekOfday = moment(timeNow).format('E'); // Calculate the day of the week today
var nowDay = moment(timeNow).format('YYYY-MM-DD') // Current date
var Monday = moment(timeNow).subtract(weekOfday - 1, 'days').format('YYYY-MM-DD'); // Week 1 Date
var Tuesday = moment(timeNow).subtract(weekOfday - 2, 'days').format('YYYY-MM-DD'); // Week 2 Date
var Wednesday = moment(timeNow).subtract(weekOfday - 3, 'days').format('YYYY-MM-DD'); // Week 3 Date
var Thursday = moment(timeNow).subtract(weekOfday - 4, 'days').format('YYYY-MM-DD'); // Week 4 Date
var Friday = moment(timeNow).subtract(weekOfday - 5, 'days').format('YYYY-MM-DD'); // Week 5 Date
var Saturday = moment(timeNow).subtract(weekOfday - 6, 'days').format('YYYY-MM-DD'); // Week 6 Date
var Sunday = moment(timeNow).add(7 - weekOfday, 'days').format('YYYY-MM-DD'); // Sunday date
// Get the total number of days in the current month
moment().daysInMonth()
Excerpts from official website:
Date formatting
moment().format('MMMM Do YYYY, h:mm:ss a'); // 5 Month 27 Day 2020, 4:47:31 Afternoon
moment().format('dddd'); // Week 3
moment().format("MMM Do YY"); // 5 Month 27 Day 20
moment().format('YYYY [escaped] YYYY'); // 2020 escaped 2020
moment().format(); // 2020-05-27T16:47:31+08:00
Relative time
moment("20111031", "YYYYMMDD").fromNow(); // 9 Years ago
moment("20120620", "YYYYMMDD").fromNow(); // 8 Years ago
moment().startOf('day').fromNow(); // 17 Hours ago
moment().endOf('day').fromNow(); // 7 Within hours
moment().startOf('hour').fromNow(); // 1 Hours ago
Calendar time
moment().subtract(10, 'days').calendar(); // 2020/05/17
moment().subtract(6, 'days').calendar(); // Last week 416:47
moment().subtract(3, 'days').calendar(); // Last Sunday 16:47
moment().subtract(1, 'days').calendar(); // Yesterday 16:47
moment().calendar(); // Today 16:47
moment().add(1, 'days').calendar(); // Tomorrow 16:47
moment().add(3, 'days').calendar(); // Next week 616:47
moment().add(10, 'days').calendar(); // 2020/06/06
Multilingual support
moment.locale(); // zh-cn
moment().format('LT'); // 16:47
moment().format('LTS'); // 16:47:31
moment().format('L'); // 2020/05/27
moment().format('l'); // 2020/5/27
moment().format('LL'); // 2020 Year 5 Month 27 Day
moment().format('ll'); // 2020 Year 5 Month 27 Day
moment().format('LLL'); // 2020 Year 5 Month 27 Sunday afternoon 4 Point 47 Points
moment().format('lll'); // 2020 Year 5 Month 27 Day 16:47
moment().format('LLLL'); // 2020 Year 5 Month 27 Day and week 3 Afternoon 4 Point 47 Points
moment().format('llll'); // 2020 Year 5 Month 27 Day and week 3 16:47
Sorted out a small part of commonly used, want to know more about the use. Here are two commonly used link addresses for reference only
1. moment.js Address: https://github.com/moment/moment
2. The front-end open source project CDN acceleration service, basically all open source js can be found above: https://www.bootcdn.cn/