[英]How to calculate to second day of the next month?
For unit testing I need the duration, how long it is until the next second day of the month. 对于单元测试,我需要持续时间,到每月第二天的持续时间。 Example: Today => 20. Jan 2018 Next second day of month => 2. Feb 2018
示例:今天=>20。2018年1月20日;第二个月的第二天=>2。2018年2月
What I need: Time in milliseconds until 2. Feb 2018 我需要什么:以毫秒为单位,直到2018年2月2日
You could calculate the time difference until the end of the month and then add an additional 2 days worth of milliseconds to the end of your calculated time ( 2*((60000*60)*24)
). 您可以计算到月底的时差,然后在计算出的时间末尾再加上2天的毫秒数(
2*((60000*60)*24)
)。 I've provided a generalized approach which allows you to perform this calculation for the n th
day in a given month: 我提供了一种通用方法,可让您在给定月份的
n th
天执行此计算:
const get_next_nth = n => today => today.getDate() < n-1 ? (n-(today.getDate()+1))*60000*60*24 : (new Date(today.getFullYear(), today.getMonth() + 1, 0) - today) + n*((60000*60)*24); const get_next_second = get_next_nth(2); const res = get_next_second(new Date()); console.log(res); // m/s until next 2nd date console.log(new Date(new Date().getTime() + res).toDateString()); // string version of next 2nd date
You can solve this problem using moment.js with the flowing process. 您可以使用带有流程的moment.js解决此问题。
let nextMonth = moment(new Date()).add(1, "M").toDate(); ==> Wed Oct 02 2019 18:24:31 GMT+0600
let startOfMonth = moment(nextMonth).startOf("month").toDate(); ==> Tue Oct 01 2019 00:00:00 GMT+0600
let secondDay = moment(startOfMonth).add(1, "d").toDate(); ==> Wed Oct 02 2019 00:00:00 GMT+0600
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.