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. 确定比赛名次---HDU1285(拓扑排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目大意: 给你每场比赛的成绩,让你根据成绩把排名弄出来 分析: 本来我是用普通方法写的,然后就一直wa, ...

  2. First class ,6 examples anlaysisi

    http://www.fgm.cc/learn/ First class ,6 examples anlaysisi <!DOCTYPE html> <!-- To change t ...

  3. 在ubuntu14.04上部署基于Docker的Gitlab

    首先在一台新的ubuntu上执行更新: sudo apt-get update 然后安装docker(采用国内源) curl -sSL https://get.daocloud.io/docker | ...

  4. Lnux 16.04 VM下安装与汉化

    参考linux-公社: http://www.linuxidc.com/Linux/2016-04/130520.htm  U盘安装linux16.04 http://www.linuxidc.com ...

  5. shell中括号的使用

    在这里我想说的是几种shell里的小括号,大括号结构和有括号的变量,命令的用法,如下: 1.${var} 2.$(cmd) 3.()和{} 4.${var:-string},${var:+string ...

  6. ArcGIS10.1 api for Flex开发用于ArcGIS 9.3时QueryTask 'Error #2032'错误的解决方案

    因客户GIS软件需求,将应用降级到低版本ArcGIS9.3,不仅数据有些样式.配色.字符有些问题,API也要相应“降级”,解决如下: 利用9.3+flex做QueryTask时候出现了[RPC Fau ...

  7. 天气预报API(一):全国城市代码列表(“旧编码”)

    说明 2016-12-09 补充 (后来)偶然发现中国天气网已经有城市ID列表的网页... 还发现城市编码有两种,暂且称中国天气网这些编码为旧标准 "旧编码"的特征是 9个字符长度 ...

  8. [UCSD白板题] Number of Inversions

    Problem Introduction An inversion of a sequence \(a_0,a_1,\cdots,a_{n-1}\) is a pair of indices \(0 ...

  9. MemCached 安装笔记

    安装步骤: 1. 下载libevent & memcached 源码包 分别把memcached和libevent下载回来,放到 /tmp 目录下: # cd /tmp     # wget ...

  10. 《Linux内核分析》之第四章读书笔记

    4.1多任务 多任务操作系统:同时并发地交互执行多个进程的操作系统 多任务操作系统会使多个进程处于堵塞或者睡眠状态.这些任务尽管位于内存,但是并不处于可运行状态.这些进程利用内核堵塞自己,直到某一事件 ...