@cainiaofe/cn-utils
Version:
菜鸟前端基础工具库
82 lines (81 loc) • 2.27 kB
JavaScript
import { STANDARD_DATE_FORMAT } from '../const';
/**
* 根据语种信息获取日期格式化配置
* @param lang 语种
* @returns
*/
export var getDateFormatConf = function (lang) {
return STANDARD_DATE_FORMAT;
};
/**
* 获取系统时区信息
* @returns
*/
export var getTimezone = function () {
return this.dayjs.tz.guess();
};
/**
* 根据当前语种&时区格式化时间戳
* @param this
* @param num
* @param opts
* @returns
*/
export var dateFormat = function (num, opts) {
return this.dayjs(num).format(opts.format);
};
/**
* 根据当前语种&时区获取时间戳
* @param this
* @param dateStr string
* @param opts FormatOpts
* @returns
*/
export 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();
};
/**
* 根据时间戳获取当前日期偏移量
* @param this
* @param num
* @param timezone
*/
export 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),
};
};
/**
* 根据时间戳获取当前日期偏移量
* @param this
* @param num
* @param timezone
* @param lang
* @returns
*/
export 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;
};
/**
* 设置 dayjs 对象时区
* @param this
* @param day
* @param timezone
* @param keepLocalTime 是否保持本地时间
* @returns 新的 dayjs 对象
*/
export 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);
};