Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { getOwnProperty } from "./get";
/**
* Превращает число в двухзначную строку
* @param {number} number
* @return {string}
*
* @example
* 3 // -> '03'
*/
const dd = (number: number): string => number.toFixed(0).padStart(2, "0");
/**
* Форматирует дату в понятную для QIWI строку:
*
* `ГГГГ-ММ-ДДTЧЧ:ММ:СС+\-ЧЧ:ММ`
*
* @param {Date | number | string} dateTime Аргумент для конструктора
* @return {string}
*/
export function formatDate(dateTime: Date | number | string): string {
const date = new Date(dateTime);
const utc = new Date(
Date.UTC(
date.getFullYear(),
date.getMonth(),
date.getDate(),
date.getHours(),
date.getMinutes(),
date.getSeconds(),
date.getMilliseconds()
)
);
const offset = date.getTimezoneOffset();
/* istanbul ignore next */
const sign = offset > 0 ? "-" : "+";
const base = utc.toISOString().split(".")[0];
const hours = dd(Math.trunc(Math.abs(offset) / 60));
const minutes = dd(offset % 60);
// [ Дата ][ Временная зона ]
return `${base}${sign}${hours}:${minutes}`;
}
export enum TimeSpan {
Millisecond = 1,
Second = Millisecond * 1000,
Minute = Second * 60,
Hour = Minute * 60,
Day = Hour * 24,
Week = Day * 7,
Month = Day * 30,
Year = Day * 365
}
export const TimeSpanMapping = Object.freeze({
ms: TimeSpan.Millisecond,
msec: TimeSpan.Millisecond,
millisecond: TimeSpan.Millisecond,
s: TimeSpan.Second,
sec: TimeSpan.Second,
second: TimeSpan.Second,
m: TimeSpan.Minute,
min: TimeSpan.Minute,
minute: TimeSpan.Minute,
h: TimeSpan.Hour,
hr: TimeSpan.Hour,
hour: TimeSpan.Hour,
d: TimeSpan.Day,
day: TimeSpan.Day,
w: TimeSpan.Week,
week: TimeSpan.Week,
mn: TimeSpan.Month,
mon: TimeSpan.Month,
month: TimeSpan.Month,
y: TimeSpan.Year,
yr: TimeSpan.Year,
year: TimeSpan.Year
});
export type TimeSpanMapping = typeof TimeSpanMapping;
export type TimeSpanKeys = keyof TimeSpanMapping;
/**
*
*
* @export
* @param {number} amount
* @param {(TimeSpan | number | TimeSpanKeys)} unit
* @param {Date} [currentDate=new Date()]
* @return {string} {string}
*/
export function formatOffsetDate(
amount: number,
unit: TimeSpan | number | TimeSpanKeys = TimeSpan.Millisecond,
currentDate = new Date()
): string {
const date = new Date(currentDate);
Iif (typeof unit !== "number") {
unit = getOwnProperty(TimeSpanMapping, unit);
}
const time = Math.round(date.getTime() + amount * unit);
date.setTime(time);
return formatDate(date);
}
|