Show EJ: datecalc - perform date calculations given a simple string pattern

栏目: IT技术 · 发布时间: 6年前

内容简介:Perform date calculations given a simple string pattern.The pattern declaring what calculations to perform.

datecalc

Perform date calculations given a simple string pattern.

Install

$ npm install @tuplo/datecalc

# or with yarn
$ yarn add @tuplo/datecalc

Usage

import datecalc from '@tuplo/datecalc';

Performs calculations assuming now by default

// assuming New Year's Day 2020, 14:00
// in two days, same time
datecalc('2D'); // → new Date('2020-01-03T14:00:00')

// two weeks ago
datecalc('-2W'); // → new Date('2019-12-18T14:00:00')

// in 15 minutes
datecalc('15m'); // → new Date('2020-01-01T14:15:00')

// tomorrow, two hours less than now
datecalc('1D -2h'); // → new Date('2020-01-02T12:00:00')

Performs sucessive calculations

// 58 years and 2 months from now, less 4 hours
datecalc('58Y 2M -4h'); // → new Date('2078-03-01T10:00:00')

Calculates from a given date

const birthday = new Date('1961-07-04T11:00:00');
// 58 years from birthday
datecaclc('58Y', birthday); // → new Date('2019-07-04T11:00:00')

Accepted patterns

Pattern Period
D Day
W Week
M Month
Y Year
h Hour
m Minute
s Second

API

datecalc(format: string, fromDate?: Date): Date

pattern: string

The pattern declaring what calculations to perform.

Format: /([0-9-]+[DWMYhms])/g

fromDate: Date

The starting moment from when to start calculating.

Default: new Date(Date.now())

Return value: Date

Returns a new Date

Contribute

Contributions are always welcome!

License

MIT


以上所述就是小编给大家介绍的《Show EJ: datecalc - perform date calculations given a simple string pattern》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

领域驱动设计

领域驱动设计

埃文斯 / 赵俐、盛海艳、刘霞 / 人民邮电出版社 / 2010-11 / 69.00元

《领域驱动设计:软件核心复杂性应对之道》是领域驱动设计方面的经典之作。全书围绕着设计和开发实践,结合若干真实的项目案例,向读者阐述如何在真实的软件开发中应用领域驱动设计。书中给出了领域驱动设计的系统化方法,并将人们普遍接受的一些最佳实践综合到一起,融入了作者的见解和经验,展现了一些可扩展的设计最佳实践、已验证过的技术以及便于应对复杂领域的软件项目开发的基本原则。《领域驱动设计:软件核心复杂性应对之......一起来看看 《领域驱动设计》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具