Record some plug-ins used in the project, so that you can look at them in time...
~
Please pay attention, ask for favorites, and ask for likes. If you find that the blogger has written something unreasonable, please let me know in time, thank you~
foreword
In the development of actual projects, a large and medium-sized project often uses a lot of plug-ins and tools. It is obviously unrealistic to say that a project is all developed by a team, not to mention the problem of repeated wheel creation. The development of tools with strong applicability still requires a lot of effort (unless the particularity of the business causes no plug-ins or tools to meet expectations); The main purpose of
this
The protagonist of this article: time-formater ( npm address ), this is a time formatting tool, although it is a bit niche, and there is no @types, which makes it impossible to apply in TypeScript, some people may ask why this is Still use...don't
ask
installation and use
The installation is the conventional installation method, once again, there is no @types/time-formater, you need to make up your own
// 安装
npm i -S time-formater
The usage is similar in Node and browser environment, but the specifications are different
// 可以
const timeFormater = require('time-formater')
// 也可以
import timeFormater from "time-formater";
grammar
Parse parameters
The format function accepts many pre-set formal parameters as parameters (for other parameters, see Chapter 2), such as the most common "YYYY-MM-DD HH:mm:ss", which represents the current time and is accurate to seconds
// 2022-02-09 12:12:12
timeFormater().format("YYYY-MM-DD HH:mm:ss")
This is the current full time, if cropping is supported, for example
// 2022-02-09 12:12
timeFormater().format("YYYY-MM-DD HH:mm")
// 2022-02-09 12
timeFormater().format("YYYY-MM-DD HH")
Even the connector can be changed
// 2022/02/09 12/12/12
timeFormater().format("YYYY/MM/DD HH/mm/ss")
parameter
The parameters used by the format function are far more than the above. It receives many preset parameters. For example, if you want to know the current day of the week, you can enter the parameter "d"
// 三
timeFormater().format("dd")
// // 2022/三月/09 12/12/12
timeFormater().format("YYYY/MMMM/DD HH/mm/ss")
details as follows:
name | parameter | output |
---|---|---|
month | M | 1 2 3 … |
| ||
MM | 01 02 03 … | |
MMM | January February March… | |
MMMM | January February March… | |
quarter | Q | 1 2 3 4 |
date | D | 1 2 3 … |
Do | 1st 2nd 3rd ... | |
DD | 01 02 03 … | |
Week | d | 0 1 2 3 4 5 6 |
dd | day one two three four five six | |
ddd | Sunday Monday Tuesday Wednesday Thursday… | |
dddd | Sunday Monday Tuesday Wednesday | |
years | YYYY | 1970 1971… |
AM/PM etc. | A | early morning morning... afternoon evening |
a | early morning morning... afternoon evening | |
time | H | 0 1 2 3 … 22 23 |
HH | 00 01 … 22 23 | |
h | 1 2 … 11 12 | |
hh | 01 02 … 11 12 | |
minute | m | 0 1 2 … 58 59 |
mm | 00 01 02 … 58 59 | |
Second | s | 0 1 2 … 58 59 |
ss | 00 01 02 … 58 59 | |
millisecond | s | 0 1 2 … 8 9 |
ss | 00 01 … 98 99 | |
sss | 000 001 … 998 999 | |
unix timestamp | X | 1495357559853 |
Unix timestamp milliseconds | x | 1495357559853 |
Other functions
jet lag
fromNow is used to calculate the time difference between the input time and the current time. The usage is as follows:
let fromNow = time('2022-01-01').fromNow()
// 2个月前
console.log(fromNow)
countdown
The example comes from the official description of npm, and the general functions are as follows:
const time = require('time-formater')
let remain = 100000 // 10万秒
let countdown = time.countdown(remain)
let token = '剩余:d天H小时m分钟s秒'
// 浏览器
function step() {
document.title = countdown.format(token) // 剩余:1天3小时46分钟40秒
requestAnimationFrame(step)
}
step()
Used here, two functions, one is countdown, the other is the
format
countdown
Returns the countdown amount of time.
- time <number | string | Date> The type is a number indicating the remaining seconds, a Date instance or a string (in ISO 8601 format), indicating the end time point.
format(token)
Format the amount of time as a string.
- token is used to specify the output format. Example: 'Remaining: d days H hours m minutes s seconds' => "Remaining: 1 day 11 hours 4 minutes 38 seconds".
token | describe |
---|---|
d | Heaven |
H | Hour |
m | minute |
s | Second |
S | millisecond |
# | Prefix, which means to pad zeros up to the specified width. Example: #3d means pad the days to 3 characters, 001. |
TypeScript types
Unfortunately, this plug-in does not provide types, and can only supplement it according to the type. The general types are added as follows:
interface countdownFormat {
format: (params: string) => string;
}
interface FormaterInterface {
format: (params?: string | number) => string;
fromNow: () => string;
countdown: (params: number | string | date) => countdownFormat;
}
declare module "time-formater" {
function timeFormater(params?: string | number): FormaterInterface {
}
export = timeFormater;
}
It may not be very complete, and interested friends can further add
;
summary
This is a time formatting tool that I personally feel is easier to use. The only disadvantage is that the author does not seem to continue to maintain it in the later period, including the support for TypeScript is also very unfriendly. This needs to be made up for by myself. Very good, worth recommending;