jQuery实现简单日期格式化功能示例

2019-11-16,,,,

本文实例讲述了jQuery实现简单日期格式化功能。分享给大家供大家参考,具体如下:

代码如下,引入jquery后直接后加入以下代码刷新可测试

Date.prototype.Format = function (fmt) { //author: meizz
  var 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(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
  if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}
$(function(){
  var date=new Date();
  alert(JSON.stringify(date.Format("yyyy年MM月dd hh:mm:ss")));
})

运行结果如下:

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery日期与时间操作技巧总结》、《jQuery拖拽特效与技巧总结》、《jQuery扩展技巧总结》、《jQuery常见经典特效汇总》、《jquery选择器用法总结》及《jQuery常用插件及用法总结》

希望本文所述对大家jQuery程序设计有所帮助。

您可能感兴趣的文章:

  • jquery获取当前日期的方法
  • 基于jQuery的日期选择控件
  • jquery中实现时间戳与日期相互转换
  • JQuery日期插件datepicker的使用方法
  • jquery 操作日期、星期、元素的追加的实现代码
  • 使用jquery如何获取时间
  • jquery 显示*天*时*分*秒实现时间计时器
  • jQuery简单实现根据日期计算星期几的方法

《jQuery实现简单日期格式化功能示例.doc》

下载本文的Word格式文档,以方便收藏与打印。