Use the js moment plug-in to get the timestamp of the last millisecond of the last day of the month where the specified timestamp is located.

You can use endOf()the and valueOf()methods of Moment.js in combination to get the timestamp of the last millisecond of the last day of the month for the specified timestamp. The specific code is as follows:

const timestamp = 1632962400000; // 指定时间戳
const momentObj = moment(timestamp); // 初始化 Moment 对象
const endOfMonth = momentObj.endOf("month"); // 获取所在月份的最后一天
const endOfLastMillisecond = endOfMonth.endOf("millisecond"); // 获取最后一毫秒的时间
const lastMillisecondTimestamp = endOfLastMillisecond.valueOf(); // 获取最后一毫秒的时间戳
console.log(lastMillisecondTimestamp); // 输出最后一毫秒的时间戳

Among them, timestampthe variable is the specified timestamp, momentObjthe variable is initialized as a Moment object, endOfMonththe variable obtains the object of the last day of the month in which the object is located, endOfLastMillisecondthe variable obtains the time of the last millisecond, and lastMillisecondTimestampthe variable obtains the timestamp of the last millisecond.


Other:

You can use the startOf() method and unix() method of the moment plug-in to obtain the timestamp at 00:00:00 on the first day of the month where the specified timestamp is located. The code example is as follows:

var timestamp = 1592928000; // 指定的时间戳,代表2020年6月24日 00:00:00
var firstDayOfMonth = moment.unix(timestamp).startOf('month'); // 获取指定时间戳所在月份的第一天
var firstDayTimestamp = firstDayOfMonth.unix(); // 获取指定时间戳所在月份的第一天的00:00:00时的时间戳
console.log(firstDayTimestamp); // 输出:1590969600,代表2020年6月1日 00:00:00的时间戳

It should be noted that when using the moment plug-in, you need to introduce the relevant language packs and plug-ins after introducing moment.js.

Use the js moment plug-in to get the timestamp at 00:00:00 on the first day of the month where the specified timestamp is located.

Guess you like

Origin blog.csdn.net/u012632105/article/details/132765588