在js中对时间类型格式化字符串
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中对时间类型格式化字符串的更多相关文章
- 由echarts想到的js中的时间类型
在工作中使用echarts时,偶然发现折线图中对时间类型变量的用法: now前面的+号何解? now = new Date(+now + oneDay); 后来查阅资料,看到一篇博客,解释如下:这是对 ...
- 【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 ...
- WPF中Binding使用StringFormat格式化字符串方法
原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...
- js中的时间转换—毫秒转换成日期时间
转自:http://www.javascript100.com/?p=181 前几天,在项目中遇到js时间增加问题,要将js毫秒时间转换成日期时间 var oldTime = (new Date(&q ...
- 1 Java中的时间类型
总结:sql中的时间转 util的时间直接赋值即可:反过来,必须先吧util下的时间转换成毫秒,再通过sql的构造器生成sql的时间格式. 1 Java中的时间类型 java.sql包下给出三个与数据 ...
- 小程序 js中获取时间new date()的用法(网络复制过来自用)
js中获取时间new date()的用法 获取时间: 1 var myDate = new Date();//获取系统当前时间 获取特定格式的时间: 1 myDate.getYear(); //获 ...
- js 获取当前时间并格式化
js 获取当前时间并格式化 CreateTime--2018年2月7日11:04:16 Author:Marydon 方式一 /** * 获取系统当前时间并格式化 * @returns yyyy- ...
- js中 json对象与json字符串相互转换的几种方式
以下总结js中 json对象与json字符串相互转换的几种方式: 一.JSON对象转化为JSON字符串 1.使用JSON.stringify()方法进行转换 该方法不支持较老版本的IE浏览器,比如:i ...
- Js中获取时间 new date()的用法
Js中获取时间 new date()的用法 获取时间: var myDate = new Date();//获取系统当前时间 myDate.getYear(); //获取当前年份(2位) myDate ...
随机推荐
- c++11 实现单例模式
C++11出来后,里面新增加了好多好用的功能 下面的单例就是使用了C++11中的标准库中的mutex和unique_prt 进行内存管理的. 此单例模式不用担心内存的释放问题 #pragma once ...
- 《第一本docker书》- 第一章笔记
环境: Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-30-generic i686) 第一章: 1 Docker客户端和服务器 2 Docker镜像 添加一个文件,执行一 ...
- VB中的API详解
一.API是什么? 这个我本来不想说的,不过也许你知道其它人不知道,这里为了照顾一下新手,不得不说些废话,请大家谅解. Win32 API即为Microsoft 32位平台的应用程序编程接口(Appl ...
- Java正则表达式的解释说明
1.字符x 字符 x.例如a表示字符a\\ 反斜线字符.在书写时要写为\\\\.(注意:因为java在第一次解析时,把\\\\解析成正则表达式\\,在第二次解析时再解析为\,所以凡是不是1 ...
- 1500. Prime Gap 11 月 11日
/*本篇为转载,在此申明,具体就是先设定从2以后所有的数都为质数,定为质数的数的倍数则不是质数,慢慢排除后面的数*/ #include<iostream>#include<cstri ...
- ubuntu下python 2.7与python 3.X的转换
ubuntu下python 2.7与python 3.X的转换 由于ubuntu本身自带python 2.7,而python 3.X与2.7有很多不同,所以在使用python 3.X时会带来诸多不便. ...
- oracle方案是什么?
某用户拥有的所有的数据库对象的逻辑集合,就叫方案:方案在名称上和用户名是相同的. 比如有一个Oracle"用户"叫scott,那么一般称Scott用户所拥有的所有数据库对象的集合叫 ...
- JS命名空间实例
var types = new MeetingList.EventList(msg); $(".divclass").html(types.Build(new Date($(&qu ...
- RestSharp简单扩展
using RestSharp; using RestSharp.Deserializers; using RestSharp.Serializers; using System; using Sys ...
- jboss 7 启动问题
19:30:50,176 INFO [org.jboss.modules] JBoss Modules version 1.1.1.GA19:30:50,458 INFO [org.jboss.msc ...