Home > Web Front-end > JS Tutorial > body text

Example sharing jQuery to implement simple date formatting function

小云云
Release: 2017-12-27 16:40:05
Original
1403 people have browsed it

本文主要介绍了jQuery实现简单日期格式化功能,涉及jQuery调用javascript针对日期格式转换扩展实现日期格式化功能相关操作技巧,需要的朋友可以参考下,希望能帮助到大家。

代码如下,引入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")));
})
Copy after login

运行结果如下:

相关推荐:

javascript创建日期对象和日期格式化方法介绍

js的日期格式化功能

AngularJS 日期格式化详解

The above is the detailed content of Example sharing jQuery to implement simple date formatting function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template