@xtsai/xai-utils
Version:
The xai-utils is an openai nodejs sdk compatible extension library.
70 lines • 2.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertDBTimeString = exports.formatDateTime = exports.formatDateExpr = exports.formatDate = exports.DB_DATETIME_EXPR = void 0;
const date_fns_1 = require("date-fns");
//
// @see https://date-fns.org/v4.1.0/docs/format
exports.DB_DATETIME_EXPR = 'yyyy-MM-dd HH:mm:ss.SSSS';
const formatDate = (date) => {
if (!date)
date = new Date();
if (date instanceof Date) {
return (0, date_fns_1.format)(date, 'yyyy-MM-dd');
}
const _date = new Date(date);
return (0, date_fns_1.format)(_date, 'yyyy-MM-dd');
};
exports.formatDate = formatDate;
/**
*
* @param expr format expression like yyyy-MM-dd,default yyyyMMdd
* @param date Date object or number or date string ,undefined will now
* @returns string
*/
const formatDateExpr = (date, expr = 'yyyyMMdd') => {
if (!date)
date = new Date();
if (date instanceof Date) {
return (0, date_fns_1.format)(date, expr);
}
const _date = new Date(date);
return (0, date_fns_1.format)(_date, expr);
};
exports.formatDateExpr = formatDateExpr;
/**
*
* @param date
* string yyyy-MM-dd
* number ms
* date
* @param expr
* @returns
*/
const formatDateTime = (date, expr = 'yyyy-MM-dd HH:mm:ss') => {
if (!date)
date = new Date();
if (date instanceof Date) {
return (0, date_fns_1.format)(date, expr);
}
const _date = new Date(date);
return (0, date_fns_1.format)(_date, expr);
};
exports.formatDateTime = formatDateTime;
/**
* @see https://date-fns.org/v4.1.0/docs/format
* 2024-06-11 17:15:50.560170
* @param input number |string Date
* @returns string
*/
const convertDBTimeString = (input) => {
if (!input)
throw new Error('input an number,string or date required.');
if (input instanceof Date)
return (0, date_fns_1.format)(input, exports.DB_DATETIME_EXPR);
const d = typeof input === 'string' && /^[\d]{10,}/.test(input)
? new Date(parseInt(input))
: new Date(input);
return (0, date_fns_1.format)(d, exports.DB_DATETIME_EXPR);
};
exports.convertDBTimeString = convertDBTimeString;
//# sourceMappingURL=date.format.js.map