moment is a JavaScript date processing library.
use:
//Installation momentnpm install moment -- save
Quote
//Introduce globally inimport moment from "moment"
Set the moment area to China
//import methodimport 'moment/locale/zh-cn' ('zh-cn');
Mount global variables
.$moment = moment;
Code example:
- Format of the date forward or backward
moment().subtract(13, "days").calendar(); // The calendar time for the current time is 13 days ahead: 2024/07/16moment().subtract(3, "days").calendar(); // The current time is 3 days ahead: 16:46 this Wednesdaymoment().subtract(1, "days").calendar(); // The current time is 1 day ahead: Yesterday was 16:47moment().calendar(); // Today is 16:48moment().add(1, "days").calendar(); // The current time will be pushed back by 1 day: tomorrow at 16:49moment().add(3, "days").calendar(); // The current time will be pushed back 3 days: Next Tuesday at 16:50moment().add(10, "days").calendar(); // The current time is pushed back10sky: 2024/07/06
Common functions:
//Get the current timemoment();//Sun Jun 04 2023 15:12:11 GMT+0800 //Get today 0:00:00:00moment().startOf('day'); /Sun Jun 04 2024 00:00:00 GMT+0800 //Get the first day of the week (Sunday) 0:00:00moment().startOf("week"); //Mon May 29 2024 00:00:00 GMT+0800 //Get this week's Monday 0:00:00moment().startOf("isoWeek"); //Mon May 29 2024 00:00:00 GMT+0800 //Get the first day of the current month 0:0:00:00:00moment().startOf("month"); //Thu Jun 01 2024 00:00:00 GMT+0800 //Get today at 23:59:59:59moment().endOf("day"); //Sun Jun 04 2024 23:59:59 GMT+0800 //Get the last day of the week (Saturday) 23:59:59moment().endOf("week"); //Sun Jun 04 2024 23:59:59 GMT+0800 //Get this Sunday at 23:59:59moment().endOf("isoWeek"); //Sun Jun 04 2024 23:59:59 GMT+0800 //Get the last day of the current month at 23:59:59 secondsmoment().endOf("month"); //Fri Jun 30 2024 23:59:59 GMT+0800 //Get the total number of days in the current monthmoment().daysInMonth(); //30 //Get the timestamp (in seconds)moment().unix(); //1685863710 moment().format('X'); //1685863669 //Get the timestamp (in milliseconds)moment().valueOf(); //The return value is numerical type: 1685863954482moment().format('x'); // The return value is string type: 1685863897121 //Get yearmoment().year(); //2024 moment().get("year"); //2024 //Get Monthmoment().month(); //5 moment().get("month"); //5 //Get a day of the monthmoment().date(); //4 moment().get("date"); //4 //Get a day of the weekmoment().day(); //4 moment().weekday(); //6 moment().isoWeekday(); //7 moment().get("day"); //0 moment().get("weekday"); //6 moment().get("isoWeekday"); //7 //Get hoursmoment().hours(); //15 moment().get("hours"); //15 //Get minutesmoment().minutes(); //46 moment().get("minutes"); //46 //Get the secondsmoment().seconds(); //24 moment().get("seconds"); //41 //Get the current year, month, day, hour, minute, secondmoment().toArray(); //[ 2024, 5, 4, 15, 48, 40, 288 ] moment().toObject(); //{ "years": 2024, "months": 5, "date": 4, "hours": 15, "minutes": 49, "seconds": 9, "milliseconds": 386 }
//Current timemoment() //China Standard Timemoment().toDate() //Format the current timemoment().format('YYYY-MM-DD') //12-hour system:moment().format('YYYY-MM-DD hh:mm:ss') //24-hour system: //Kk has problems changing to HHmoment().format('YYYY-MM-DD kk:mm:ss') moment().format('YYYY-MM-DD HH:mm:ss') //Early this monthmoment().startOf('month') //At the end of this monthmoment().endOf('month') //N days/month/hour latermoment().add(5, 'month') //Date after 5 months, if the parameter is negative, it means before. The parameter 'month' can also be 'day' and 'hour' //The first 10 days of the current timemoment().subtract(10, "days").format("YYYY-MM-DD"); //The first year of the current timemoment().subtract(1, "years").format("YYYY-MM-DD"); //The first 3 months of the current timemoment().subtract(3, "months").format("YYYY-MM-DD"); //The previous week of the current timemoment().subtract(1, "weeks").format("YYYY-MM-DD");
Attachment: js get what week is the current date, how many weeks are there each year, the start time and end time of the week
//Introduceimport moment from 'moment'; //Get how many weeks of the year, the start and end time of each week are thereconst mapWeeksOfyear = ({ year} = {}) => { const nowYear = year ? year : moment().year(); const timestamp = (new Date()).valueOf() // Set the year being processed let handleYear = moment(new Date(String(nowYear))); // How many weeks have there been in this year const totalWeeks = ('year').isoWeek(); const arryWeek = []; let currentWeek = null; //What week is the current date for(let i = 1;i <= totalWeeks;i++){ let startOf = (i).startOf('week').format('MM-DD'); let endOf = (i).endOf('week').format('MM-DD'); let ednyear = (i).endOf('week').format('YYYY'); let startValue= (i).startOf('week').valueOf(); let endValue= (i).endOf('week').valueOf(); if(startValue<=timestamp&&endValue>=timestamp){ currentWeek = i } ({ value: i, name: `The${i}week ${ednyear>nowYear?nowYear + "-" +startOf :startOf} to ${ednyear>nowYear?ednyear + "-" +endOf:endOf}`, startTime: (i).startOf('week').format('YYYY-MM-DD'),// The start time of this week endTime: (i).endOf('week').format('YYYY-MM-DD'), // The end time of this week }) } return {arryWeek,currentWeek} }
This is the article about the JavaScript date processing library moment() obtaining time. For more related content on obtaining time, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!