UNPKG

@cainiaofe/cn-utils

Version:

菜鸟前端基础工具库

92 lines (91 loc) 2.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dayjsTz = exports.getTimezoneName = exports.getTimezoneOffset = exports.dateTimestamp = exports.dateFormat = exports.getTimezone = exports.getDateFormatConf = void 0; var const_1 = require("../const"); /** * 根据语种信息获取日期格式化配置 * @param lang 语种 * @returns */ var getDateFormatConf = function (lang) { return const_1.STANDARD_DATE_FORMAT; }; exports.getDateFormatConf = getDateFormatConf; /** * 获取系统时区信息 * @returns */ var getTimezone = function () { return this.dayjs.tz.guess(); }; exports.getTimezone = getTimezone; /** * 根据当前语种&时区格式化时间戳 * @param this * @param num * @param opts * @returns */ var dateFormat = function (num, opts) { return this.dayjs(num).format(opts.format); }; exports.dateFormat = dateFormat; /** * 根据当前语种&时区获取时间戳 * @param this * @param dateStr string * @param opts FormatOpts * @returns */ var dateTimestamp = function (dateStr, opts) { if (!(opts === null || opts === void 0 ? void 0 : opts.format)) { return this.dayjs(dateStr).valueOf(); } return this.dayjs(dateStr, opts === null || opts === void 0 ? void 0 : opts.format).valueOf(); }; exports.dateTimestamp = dateTimestamp; /** * 根据时间戳获取当前日期偏移量 * @param this * @param num * @param timezone */ var getTimezoneOffset = function (num, timezone) { var timeStr = this.dayjs(num).tz(timezone).format(); return { offset: this.dayjs(num).utcOffset(), offsetText: timeStr.slice(timeStr.length - 6, timeStr.length), }; }; exports.getTimezoneOffset = getTimezoneOffset; /** * 根据时间戳获取当前日期偏移量 * @param this * @param num * @param timezone * @param lang * @returns */ var getTimezoneName = function (num, timezone, lang) { var options = { timeZone: timezone, timeZoneName: 'long' }; var formatter = new Intl.DateTimeFormat(lang, options); var parts = formatter.formatToParts(num ? +num : new Date()); var zoneNamePart = parts.find(function (part) { return part.type === 'timeZoneName'; }); return zoneNamePart ? zoneNamePart.value : null; }; exports.getTimezoneName = getTimezoneName; /** * 设置 dayjs 对象时区 * @param this * @param day * @param timezone * @param keepLocalTime 是否保持本地时间 * @returns 新的 dayjs 对象 */ var dayjsTz = function (day, timezone, keepLocalTime) { if (typeof day === 'object' && day.valueOf) { return this.dayjs(day.valueOf()).tz(timezone, keepLocalTime); } return this.dayjs(day).tz(timezone, keepLocalTime); }; exports.dayjsTz = dayjsTz;