家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发,您的支持是我不断创作的动力。
Chrono 是 Javascript 中的自然语言日期解析器,它旨在处理大多数日期/时间格式并从任何给定文本中提取信息,内容包括:
目前 Chrono 在 Github 上通过 MIT 协议开源,有超过 3.5k 的 star,值得尝试。
首先需要通过 NPM 安装:
npm install --save chrono-node
然后直接在代码中引入即可:
import * as chrono from 'chrono-node';
chrono.parseDate('An appointment on Sep 12-13');
如果是 Node.js 环境,可以使用下面的代码:
const chrono = require('chrono-node');
// or `import chrono from 'chrono-node'` for ECMAScript
对于 Chrono 库来说,只需将字符串传递给函数 chrono.parseDate 或 chrono.parse 即可。
import * as chrono from 'chrono-node';
chrono.parseDate('An appointment on Sep 12-13');
// Fri Sep 12 2014 12:00:00 GMT-0500 (CDT)
chrono.parse('An appointment on Sep 12-13');
/* [{
index: 18,
text: 'Sep 12-13',
start: ...
}] */
今天的“星期五”与上个月的“星期五”不同,引用日期的含义取决于它们被提及的时间和地点。 Chrono 允许开发者将引用定义为 Date 或 ParsingReference 对象:
// (Note: the exmaples run on JST timezone)
chrono.parseDate('Friday', new Date(2012, 8 - 1, 23));
// Fri Aug 24 2012 12:00:00 GMT+0900 (JST)
chrono.parseDate('Friday', new Date(2012, 8 - 1, 1));
// Fri Aug 03 2012 12:00:00 GMT+0900 (JST)
chrono.parseDate('Friday at 4pm', {
// Wed Jun 09 2021 21:00:00 GMT+0900 (JST)
// = Wed Jun 09 2021 07:00:00 GMT-0500 (CDT)
instant: new Date(1623240000000),
timezone: 'CDT',
});
// Sat Jun 12 2021 06:00:00 GMT+0900 (JST)
// = Fri Jun 11 2021 16:00:00 GMT-0500 (CDT)
forwardDate(布尔值)假设结果应该在参考日期之后发生。
const referenceDate = new Date(2012, 7, 25);
// Sat Aug 25 2012 00:00:00 GMT+0900 -- The reference date was Saturday
chrono.parseDate('Friday', referenceDate);
// Fri Aug 24 2012 12:00:00 GMT+0900 (JST) -- The day before was Friday
chrono.parseDate('Friday', referenceDate, { forwardDate: true });
// Fri Aug 31 2012 12:00:00 GMT+0900 (JST) -- The following Friday
timezones 覆盖或添加时区缩写和偏移量之间的自定义映射。 当希望 Chrono 将某些文本解析为给定的时区偏移量时,请使用此选项。 Chrono 支持明确的(正常)时区映射和模糊映射,其中夏令时期间和之外的偏移量不同。
// Chrono doesn't understand XYZ, so no timezone is parsed
chrono.parse('at 10:00 XYZ', new Date(2023, 3, 20))
// "knownValues": {"hour": 10, "minute": 0}
// Make Chrono parse XYZ as offset GMT-0300 (180 minutes)
chrono.parse('at 10:00 XYZ', new Date(2023, 3, 20), { timezones: { XYZ: -180 } })
// "knownValues": {"hour": 10, "minute": 0, "timezoneOffset": -180}
// Make Chrono parse XYZ as offset GMT-0300 outside of DST, and GMT-0200 during DST. Assume DST is between
import { getLastDayOfMonthTransition } from "timezone";
import { Weekday, Month } from "parsing";
const parseXYZAsAmbiguousTz = {
timezoneOffsetDuringDst: -120,
timezoneOffsetNonDst: -180,
dstStart: (year: number) => getLastWeekdayOfMonth(year, Month.FEBRUARY, Weekday.SUNDAY, 2),
dstEnd: (year: number) => getLastWeekdayOfMonth(year, Month.SEPTEMBER, Weekday.SUNDAY, 3)
};
// Parsing a date which falls within DST
chrono.parse('Jan 1st 2023 at 10:00 XYZ', new Date(2023, 3, 20), { timezones: { XYZ: parseXYZAsAmbiguousTz } })
// "knownValues": {"month": 1, ..., "timezoneOffset": -180}
// Parsing a non-DST date
chrono.parse('Jun 1st 2023 at 10:00 XYZ', new Date(2023, 3, 20), { timezones: { XYZ: parseXYZAsAmbiguousTz } })
// "knownValues": {"month": 6, ..., "timezoneOffset": -120}
https://github.com/wanasit/chrono
https://dexlock.com/blog/a-dive-into-nlp-date-parsers/
在前端开发中,多多少少有项目需要 jQuery日期选择/日历的插件,以下就为大家整理出一些 jQuery日期/日历,希望对大家有所帮助!还有更好用的,也不否在下方留言,分享大家。PS:(标题博眼球的,知道大家都知道这些的。呵呵呵)
1.jQuery日历插件 jQuery Week Calendar (是一个 jQuery 的按周显示的日历插件,能够在周日历网格中显示事件)
Web开发中,经常需要获取当前的日期和时间,以便于在页面中显示或进行相应的操作。JavaScript提供了一些内置的方法,可以方便地获取当前的日期和时间。
要获取当前的日期,我们可以使用Date对象的getDate()、getMonth()和getFullYear()方法。具体步骤如下:
下面是一个示例代码:
var now = new Date();
var day = now.getDate();
var month = now.getMonth() + 1;
var year = now.getFullYear();
console.log("当前日期为:" + year + "-" + month + "-" + day);
运行上述代码,控制台将输出当前日期,例如:当前日期为:2023-10-31。
要获取当前的时间,我们可以使用Date对象的getHours()、getMinutes()和getSeconds()方法。具体步骤如下:
下面是一个示例代码:
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
console.log("当前时间为:" + hours + ":" + minutes + ":" + seconds);
运行上述代码,控制台将输出当前时间,例如:当前时间为:13:24:21。
如果需要同时获取当前的日期和时间,可以将上述两个步骤合并。具体步骤如下:
创建一个Date对象,没有传入任何参数,即默认为当前时间。
下面是一个示例代码:
var now = new Date();
var day = now.getDate();
var month = now.getMonth() + 1;
var year = now.getFullYear();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
console.log("当前日期和时间为:" + year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds);
运行上述代码,控制台将输出当前日期和时间,例如:当前日期和时间为:2023-10-31 13:25:13。
通过JavaScript的Date对象,我们可以方便地获取当前的日期和时间。通过使用getDate()、getMonth()、getFullYear()、getHours()、getMinutes()和getSeconds()方法,可以轻松地获取所需的日期和时间信息。
*请认真填写需求信息,我们会在24小时内与您取得联系。