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. MongoDB基本管理命令

    MongoDB是一个NoSQL数据库系统:一个数据库可以包含多个集合(Collection),每个集合对应于关系数据库中的表:而每个集合中 可以存储一组由列标识的记录,列是可以自由定义的,非常灵活,由 ...

  2. 【转】js判断手机访问网页

    原理:原理有两个,第一:通过游览器(browser)判断是否是手机:第二:通过访问终端判断是否是手机(userAgent):但是通常考虑到兼容性,需要这两种原理同时使用:具体的实现如下: JS实现: ...

  3. linux系统编程之错误处理机制

    在讲解liunx错误处理机制之前我们先来看一段代码: #include<sys/types.h> #include<sys/stat.h> #include<fcntl. ...

  4. LinkedHahsMap和HashMap的比较

    http://www.cnblogs.com/hubingxu/archive/2012/02/21/2361281.html#commentform 一般情况下,我们用的最多的是HashMap,在M ...

  5. yii使用createCommand()增删改查

    查询单条数据$sql = "SELECT `name` FROM `table` WHERE id='7'";$users=Yii::$app->db->createC ...

  6. c#利用WebClient和WebRequest获取网页源代码的比较

    前几天举例分析了用asp+xmlhttp获取网页源代码的方法,但c#中一般是可以利用WebClient类和WebRequest类获取网页源代码.下面分别说明这两种方法的实现. WebClient类获取 ...

  7. 1500. Prime Gap 11 月 11日

    /*本篇为转载,在此申明,具体就是先设定从2以后所有的数都为质数,定为质数的数的倍数则不是质数,慢慢排除后面的数*/ #include<iostream>#include<cstri ...

  8. webpack配置

    1.初始化webpack npm install webpack -g   (全局安装) npm init   (初始化packge.json) npm install webpack --save- ...

  9. Samba的安装与配置

    Samba的安装与配置: 准备:关闭其他虚拟设备 #/etc/init.d/libvirtd stop #/etc/init.d/xend stop #chkconfig libvirtd off # ...

  10. 使用jigdo下载debian [windows环境下]

    使用jigdo下载debian  本文地址:http://www.cnblogs.com/yhLinux/p/4104451.html 准备工作: 下载jigdo:http://atterer.org ...