当前位置:首页 > 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 命名空间下方便全局调用

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

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

相关文章

vue实现日期选择显示

vue实现日期选择显示

Vue 实现日期选择显示的方法 使用原生 HTML5 input[type="date"] 通过 HTML5 提供的日期输入控件实现基础日期选择功能,适合简单场景。 <template>…

vue中实现节流函数

vue中实现节流函数

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

react项目如何定义全局函数

react项目如何定义全局函数

定义全局函数的方法 在React项目中,可以通过以下几种方式定义全局函数,以便在整个应用中复用。 使用全局变量挂载 将函数挂载到window对象上,使其成为全局可访问的函数。这种方式简单直接,但可能…

react箭头函数如何传参

react箭头函数如何传参

箭头函数传参基础语法 箭头函数通过括号内的参数列表接收参数,单参数时可省略括号。例如: const greet = name => `Hello, ${name}!`; console.log…

react如何阻止函数组件更新

react如何阻止函数组件更新

阻止函数组件更新的方法 在React中,函数组件的更新通常由状态(state)或属性(props)的变化触发。以下是几种阻止函数组件不必要更新的方法: 使用React.memo进行浅比较 React…

react如何让函数组件缓存

react如何让函数组件缓存

缓存函数组件的常用方法 在React中,函数组件本身是无状态的,但可以通过以下方式实现类似类组件的缓存或优化效果: 使用React.memo进行浅比较缓存 React.memo是一个高阶组件,它会记…