当前位置:首页 > jquery

jquery日期格式化函数

2026-03-17 11:23:57jquery

jQuery 日期格式化函数实现

jQuery 本身没有内置日期格式化函数,但可以通过扩展 jQuery 或直接使用 JavaScript 实现。以下是几种常见的实现方式:

使用原生 JavaScript 扩展

Date.prototype.format = function(format) {
    const o = {
        "M+": this.getMonth() + 1, // 月份
        "d+": this.getDate(), // 日
        "h+": this.getHours(), // 小时
        "m+": this.getMinutes(), // 分
        "s+": this.getSeconds(), // 秒
        "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
        "S": this.getMilliseconds() // 毫秒
    };
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (let k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};

// 使用示例
const date = new Date();
console.log(date.format("yyyy-MM-dd hh:mm:ss"));

作为 jQuery 插件实现

$.formatDate = function(date, format) {
    if (!date) return '';
    if (typeof date === 'string') {
        date = new Date(date.replace(/-/g, '/'));
    }
    const map = {
        "M": date.getMonth() + 1, // 月份
        "d": date.getDate(), // 日
        "h": date.getHours(), // 小时
        "m": date.getMinutes(), // 分
        "s": date.getSeconds(), // 秒
        "q": Math.floor((date.getMonth() + 3) / 3), // 季度
        "S": date.getMilliseconds() // 毫秒
    };
    format = format.replace(/([yMdhmsqS])+/g, function(all, t) {
        let v = map[t];
        if (v !== undefined) {
            if (all.length > 1) {
                v = '0' + v;
                v = v.substr(v.length - 2);
            }
            return v;
        } else if (t === 'y') {
            return (date.getFullYear() + '').substr(4 - all.length);
        }
        return all;
    });
    return format;
};

// 使用示例
const dateStr = $.formatDate(new Date(), 'yyyy-MM-dd');

使用第三方库

对于更复杂的日期处理需求,推荐使用专门的日期处理库:

  1. Moment.js(已停止维护,但仍在广泛使用)

    moment().format('YYYY-MM-DD HH:mm:ss');
  2. Day.js(Moment.js 的轻量替代)

    dayjs().format('YYYY-MM-DD HH:mm:ss');
  3. date-fns

    jquery日期格式化函数

    import { format } from 'date-fns';
    format(new Date(), 'yyyy-MM-dd HH:mm:ss');

注意事项

  • 原生 JavaScript 实现需要注意时区问题
  • 日期字符串解析时,不同浏览器对格式的支持可能不一致
  • 对于复杂的国际化需求,建议使用专业日期库
  • 在 jQuery 项目中,可以将格式化函数添加到 jQuery 命名空间下方便全局调用

以上方法可以根据项目需求选择适合的实现方式,对于简单项目可以使用原生实现,复杂项目推荐使用专业日期库。

标签: 函数日期
分享给朋友:

相关文章

uniapp如何写全局函数

uniapp如何写全局函数

全局函数的定义与使用 在UniApp中定义全局函数可以通过挂载到Vue.prototype或使用模块化导出导入的方式实现。以下是两种常用方法: 方法一:挂载到Vue.prototype 在main.…

vue实现滑动日期

vue实现滑动日期

实现滑动日期选择器的基本思路 在Vue中实现滑动日期选择器可以通过结合第三方库或自定义组件完成。核心逻辑包括日期数据的生成、滑动动画的实现以及用户交互处理。 使用第三方库(如vant) 安装vant…

vue组件实现函数调用

vue组件实现函数调用

实现 Vue 组件函数调用的方法 在 Vue 组件中实现函数调用可以通过多种方式完成,以下是常见的几种方法: 通过 methods 定义并调用函数 在 Vue 组件的 methods 选项中定义函…

vue中实现节流函数

vue中实现节流函数

使用 Lodash 的 _.throttle 方法 安装 Lodash 并导入 throttle 方法: npm install lodash 在 Vue 组件中使用: import { thro…

react如何调用函数

react如何调用函数

React 函数调用的方法 在 React 中调用函数可以通过多种方式实现,具体取决于函数的作用和调用时机。以下是常见的几种方法: 直接在组件中调用函数 函数可以在组件的 render 方法或其他…

react函数组件如何更新

react函数组件如何更新

React 函数组件更新方法 使用 useState Hook 通过 useState Hook 可以定义组件的状态,并触发重新渲染。useState 返回一个状态值和一个更新状态的函数。调用更新函数…