Date.prototype.toString = function (format) {
if (format == null) {
format = "yyyy-MM-dd HH:mm:ss";
} format = format.replace(/yyyy/g, this.getFullYear());
format = format.replace(/yyy/g, this.getYear());
format = format.replace(/yy/g, this.getFullYear().toString().slice(-2)); if (format.indexOf('mi') >= 0) {
format = format.replace(/mi/g, this.getMilliseconds().toString());
} if (format.indexOf('M') >= 0) {
var M = (this.getMonth() + 1).toString();
format = format.replace(/MM/g, ("0" + M).slice(-2));
format = format.replace(/M/g, M);
} if (format.indexOf('ddd') >= 0) {
var xq = ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日"];
format = format.replace(/ddd/g, xq[this.getDay()]);
} if (format.indexOf('d') >= 0) {
var d = this.getDate().toString();
format = format.replace(/dd/g, ("0" + d).slice(-2));
format = format.replace(/d/g, d);
} if (format.indexOf('h') >= 0 || format.indexOf('H') >= 0) {
var h = this.getHours();
format = format.replace(/HH/g, ("0" + h.toString()).slice(-2));
format = format.replace(/H/g, h);
h = h % 12;
format = format.replace(/hh/g, ("0" + h.toString()).slice(-2));
format = format.replace(/h/g, h);
} if (format.indexOf('m') >= 0) {
var m = this.getMinutes().toString();
format = format.replace(/mm/g, ("0" + m).slice(-2));
format = format.replace(/m/g, m);
} if (format.indexOf('s') >= 0) {
var s = this.getSeconds().toString();
format = format.replace(/ss/g, ("0" + s).slice(-2));
format = format.replace(/s/g, m);
} return format;
}

效果

代码段

https://code.csdn.net/snippets/76928

在js中对时间类型格式化字符串的更多相关文章

  1. 由echarts想到的js中的时间类型

    在工作中使用echarts时,偶然发现折线图中对时间类型变量的用法: now前面的+号何解? now = new Date(+now + oneDay); 后来查阅资料,看到一篇博客,解释如下:这是对 ...

  2. 【spring boot】spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [java.util.Date]

    spring boot 前台GET请求,传递时间类型的字符串,后台无法解析,报错:Failed to convert from type [java.lang.String] to type [jav ...

  3. WPF中Binding使用StringFormat格式化字符串方法

    原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...

  4. js中的时间转换—毫秒转换成日期时间

    转自:http://www.javascript100.com/?p=181 前几天,在项目中遇到js时间增加问题,要将js毫秒时间转换成日期时间 var oldTime = (new Date(&q ...

  5. 1 Java中的时间类型

    总结:sql中的时间转 util的时间直接赋值即可:反过来,必须先吧util下的时间转换成毫秒,再通过sql的构造器生成sql的时间格式. 1 Java中的时间类型 java.sql包下给出三个与数据 ...

  6. 小程序 js中获取时间new date()的用法(网络复制过来自用)

    js中获取时间new date()的用法   获取时间: 1 var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获 ...

  7. js 获取当前时间并格式化

      js 获取当前时间并格式化 CreateTime--2018年2月7日11:04:16 Author:Marydon 方式一 /** * 获取系统当前时间并格式化 * @returns yyyy- ...

  8. js中 json对象与json字符串相互转换的几种方式

    以下总结js中 json对象与json字符串相互转换的几种方式: 一.JSON对象转化为JSON字符串 1.使用JSON.stringify()方法进行转换 该方法不支持较老版本的IE浏览器,比如:i ...

  9. Js中获取时间 new date()的用法

    Js中获取时间 new date()的用法 获取时间: var myDate = new Date();//获取系统当前时间 myDate.getYear(); //获取当前年份(2位) myDate ...

随机推荐

  1. 在vCenter5.5中为用户创建角色,管理虚拟机

    在vSphere的使用中,如有只有vCenter+ESXi节点的两级配置,为了达到多租户管理及权限分配,可以在vCenter5.5中为用户创建角色,管理虚拟机 1.以管理员身份登陆vCenter 2. ...

  2. HighCharts中Y轴颜色设置

    yAxis: [{ title: { text: '', style: { color: '#2EBBD9' } }, labels: { formatter: function () { retur ...

  3. DbProviderFactory .net数据库工厂模式

    http://kb.cnblogs.com/page/72789/  工厂模式 http://www.cnblogs.com/Ruiky/archive/2012/04/19/2456784.html ...

  4. javascript里的继承

    js里面继承的方式, 1. 类式继承,通过构造函数的继承 function extend(subClass, superClass){ var F = function(){}; F.prototyp ...

  5. 在安卓下打包cocos2d-js 3.6项目with ProtoBuf.js

    项目用到了cocos2d-js 3.6和ProtoBuf.js,但是打包成apk时运行时总是报错(evaluatedOK == JS_FALSE),没有具体的文件和行号报错信息. 只能一个一个文件排查 ...

  6. swift3.0:associatedtype

    E文:https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Lang ...

  7. Mysql 变量使用

    mysql 中可以将所查询到的数据用变量存储下来,供后面使用 语法:SELECT @变量名 := 变量值 eg: SELECT @cid:=class_id, (SELECT class_name F ...

  8. 解决Web部署 svg/woff/woff2字体 404错误

    在IIS上部署web项目的时候,发现浏览器报找不到woff.woff2字体的错误,导致浏览器加载字体报404错误,由于服务器IIS不认SVG,WOFF/WOFF2 这几个文件类型,只要在IIS上添加M ...

  9. XML解析工具类

    public class XmlUtil { /* * 利用dom4j解析xml文件内容,并返回map数据形式 * path是.xml文件所在的路径 */ public static Map<S ...

  10. gem sources --add http://ruby.taobao.org/

    gem sources  gem sources  gem sources --remove http://ruby.taobao.org/ gem sources --add http://ruby ...