欢迎光临散文网 会员登陆 & 注册

碎片时间学编程「285]:返回指定日期所属的季度和年份

2023-03-14 12:20 作者:路条编程  | 我要投稿


返回指定日期所属的季度和年份。

  • 使用Date.prototype.getMonth()方法获取范围 (0, 11) 中的当前月份,获取值加1以将其映射到范围 (1, 12)。

  • 使用Math.ceil()方法并将月份除以3得到当前季度。

  • 使用Date.prototype.getFullYear()从给定的 date 中获取年份。

  • 省略参数 ,date默认使用当前日期。

JavaScript

const quarterOfYear = (date = new Date()) => [  Math.ceil((date.getMonth() + 1) / 3),  date.getFullYear()];

示例:

quarterOfYear(new Date('07/10/2018')); // [ 3, 2018 ]quarterOfYear(); // [ 4, 2020 ]

更多内容请访问我的网站:https://www.icoderoad.com


碎片时间学编程「285]:返回指定日期所属的季度和年份的评论 (共 条)

分享到微博请遵守国家法律