javascript - 如何使用javascript获取最近的十年/世纪/千年
问题描述
我试图找到一种方法来获取日期的 2 个最近范围可能是十年、世纪、千年
例子
var date = new Date() // Mon Jul 09 2018 17:12:17 GMT-0400
date.getDecadeRange() // [Jan 01 2010 , Dec 31 , 2019]
等等。
解决方案
为了四舍五入到最接近的倍数,我们可以使用小工具:
const round = (n, to) => n - n % to;
现在我们只需要对这些年进行舍入,然后取第一天:
const round = (n, to) => n - n % to;
const now = new Date();
const start = new Date(round(now.getFullYear(), 100), 0, 1);
// Go to the start of the next period ...
const end = new Date(round(now.getFullYear(), 100) + 100, 0, 1);
end.setDate(end.getDate() - 1); // then go one day back
console.log(`${start}\n${end}`);
推荐阅读
- c# - 从 .msg 文件路径创建 MailMessage 对象?
- python - python 正则表达式,其中一组选项在列表中最多可以出现一次,以任何顺序
- kotlin - 如何处理 Kotlin 中延迟列表以获取成功元素的异常?
- javascript - TypeError:message.client.commands.array 不是函数
- azure - 如何使用机器人作曲家将对话集属性存储在 azure 上?
- anaconda - 如果“收集包元数据”需要很长时间,这意味着什么?
- python - Django Rest 框架:drf-renderer-xlsx——如何在返回响应之前在文件中创建第二张工作表
- pine-script - 如何匹配第 N 次出现的布尔条件?
- c - 制作一个列出目录中的文件并对输出进行排序的程序
- azure-data-factory-2 - 使用 POST API 调用为 Azure 数据工厂复制活动设置分页