moment截取某时间段每个月或每天的开始时间和结束时间

46 篇文章 0 订阅
30 篇文章 0 订阅

moment时间戳转日期=> moment.unix(时间戳).format(“YYYY-MM-DD”)

//先安装moment
//npm i moment
//再安装Lodash,可以用它内部封装好的方法,简单实用
//npm i lodash
const moment = require("moment");
const _ = require("lodash");
//假设需要查询2019-06-25至2019-12-31号之间每一天的00:00至23:59这个时间段的数据
//将时间转换为moment时间格式
const start = "2019-06-25";//开始时间
const start_time = moment(start);
console.log(moment(start_time));//moment("2019-06-25T00:00:00.000")

const end = "2019-12-31";
const end_time = moment(end).endOf("d");
console.log(end_time);//endOf("d")为当天的结束时间 moment("2019-12-31T23:59:59.999")

//开始计算这两个时间段相差的天数
const diff_times = end_time.diff(start_time,"d");
console.log(diff_times);//189

//lodash中内置的循环器,可以指定循环次数
//再定义一个数组,用来存放相差的每一天日期
const arr = [];
_.times(diff_times, i => {
    const new_start_time = moment(start_time);//每次重新初始化开始时间,因为我碰到了深拷贝的问题
    arr.push(new_start_time.add(i, "days").format("YYYY-MM-DD"));//数组下标从0开始,可以用它进行每次的天数递增
});
//console.log(arr);
/**
 * [ '2019-06-25',
  '2019-06-26',
  '2019-06-27',
  '2019-06-28',
  '2019-06-29',
  '2019-06-30',
  '2019-07-01',
  '2019-07-02',
  '2019-07-03',
  '2019-07-04',
  '2019-07-05',
  '2019-07-06',
  '2019-07-07',
  '2019-07-08',
  '2019-07-09',
  '2019-07-10',
  '2019-07-11',
  '2019-07-12',
  '2019-07-13'
  ... more items ]
 */

 //现在我们已经获取了每一天的具体日期,接下来我们就可以对这每一年的日期进行头尾运算了
 //再定义一个map循环用来存储每一天的开始和结束时间的十位时间戳
const map = _.map(arr, v => {
    return {
        time: v,
        start_unix: moment(v).startOf('d').unix(),
        end_unix  : moment(v).endOf('d').unix()
    }
});
//console.log(JSON.stringify(map));
/**
 * [
 * {"time":"2019-06-25","start_unix":1561392000,"end_unix":1561478399},
 * {"time":"2019-06-26","start_unix":1561478400,"end_unix":1561564799},
 * {"time":"2019-06-27","start_unix":1561564800,"end_unix":1561651199},
 * {"time":"2019-06-28","start_unix":1561651200,"end_unix":1561737599},
 * {"time":"2019-06-29","start_unix":1561737600,"end_unix":1561823999},
 * {"time":"2019-06-30","start_unix":1561824000,"end_unix":1561910399},
 * ..... more times ]
 */
//至此已经完成该时间段每天的头尾时间转换了

//如果需要获取某一年每个月的开始和结束时间呢?
//我们同样的以2019-06-25这个日期为例
const year = start.substring(0, 4);//取年份
const clone_year = start.substring(0, 4);//再取一个年份避免moment循环引用自身

//一年有12个月,在这里我们就直接循环十二次了
//定一个用于存放月份日期的数组
const arr_month = [];
_.times(12, i => {
    //moment(`${i+1}/${year}`, "DD/YYYY")
    //这样做的避免了JS时间的偏移 => https://momentjs.com/guides/#/warnings/js-date/
    arr_month.push(
        {   
            month      : `${i+1} 月份`,
            start_month: moment(`${i+1}/${year}`, "MM/YYYY").add(0, "M").format("YYYY-MM-DD"),
            end_month  : moment(`${i+1}/${clone_year}`, "MM/YYYY").endOf("M").add(0, "M").format("YYYY-MM-DD")
        }
    );
});
//console.log(arr_month);

/**
 * [ { month: '1 月份',
    start_month: '2019-01-01',
    end_month: '2019-01-31' },
  { month: '2 月份',
    start_month: '2019-02-01',
    end_month: '2019-02-28' },
  { month: '3 月份',
    start_month: '2019-03-01',
    end_month: '2019-03-31' },
  { month: '4 月份',
    start_month: '2019-04-01',
    end_month: '2019-04-30' },
  { month: '5 月份',
    start_month: '2019-05-01',
    end_month: '2019-05-31' },
  { month: '6 月份',
    start_month: '2019-06-01',
    end_month: '2019-06-30' },
  { month: '7 月份',
    start_month: '2019-07-01',
    end_month: '2019-07-31' },
  { month: '8 月份',
    start_month: '2019-08-01',
    end_month: '2019-08-31' },
  { month: '9 月份',
    start_month: '2019-09-01',
    end_month: '2019-09-30' },
  ...... more times]
 */

//现在我们已经得到某一年每个月份的开始和结束时间了,我们还需要再进行时间戳转换
const map_month = _.map(arr_month, v => {
    return {
        month           : v.month,
        start_month_unix: moment(v.start_month).startOf('d').unix(),
        end_month_unix  : moment(v.end_month).endOf('d').unix()
   }; 
});
console.log(map_month);
/**
 * [ { month: '1 月份',
    start_month_unix: 1546272000,
    end_month_unix: 1548950399 },
  { month: '2 月份',
    start_month_unix: 1548950400,
    end_month_unix: 1551369599 },
  { month: '3 月份',
    start_month_unix: 1551369600,
    end_month_unix: 1554047999 },
  { month: '4 月份',
    start_month_unix: 1554048000,
    end_month_unix: 1556639999 },
  { month: '5 月份',
    start_month_unix: 1556640000,
    end_month_unix: 1559318399 },
  { month: '6 月份',
    start_month_unix: 1559318400,
    end_month_unix: 1561910399 },
  { month: '7 月份',
    start_month_unix: 1561910400,
    end_month_unix: 1564588799 },
  { month: '8 月份',
    start_month_unix: 1564588800,
    end_month_unix: 1567267199 },
    ....... more times]
 */
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
moment是一个JavaScript库,用于处理、解析、格式化和操作日期和时间。它提供了许多方便的方法,使得在JavaScript中处理时间变得更加简单和灵活。 moment可以用于创建、解析和格式化日期和时间。你可以使用moment对象来执行各种操作,如添加或减去时间、比较日期、格式化日期等。 以下是moment库的一些常见用法: 1. 创建moment对象: 你可以使用moment()函数来创建一个表示当前时间moment对象,也可以传入一个日期字符串或日期对象来创建指定时间moment对象。 示例: ``` const now = moment(); // 创建表示当前时间moment对象 const specificDate = moment("2022-01-01"); // 创建表示指定日期的moment对象 ``` 2. 格式化日期和时间moment提供了format()方法,用于将日期和时间格式化为指定的字符串格式。 示例: ``` const formattedDate = moment().format("YYYY-MM-DD"); // 格式化当前日期为"YYYY-MM-DD"格式 const formattedTime = moment().format("HH:mm:ss"); // 格式化当前时间为"HH:mm:ss"格式 ``` 3. 操作日期和时间moment提供了许多方法来操作日期和时间,如添加或减去时间、比较日期等。 示例: ``` const tomorrow = moment().add(1, 'day'); // 添加一天 const nextWeek = moment().add(1, 'week'); // 添加一周 const isBefore = moment("2022-01-01").isBefore("2022-02-01"); // 比较日期,判断是否在指定日期之前 ``` moment还提供了许多其他功能,如解析日期字符串、获取日期的部分信息(年、、日等)、本地化等。你可以根据具体需求查阅moment的官方文档来了解更多用法。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值