在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 ...
随机推荐
- 关于springmvc的配置文件
开发常用的springmvc.xml配置文件,spring 3.0 spring-servlet.xml配置. <?xml version="1.0" encoding=&q ...
- Shell--用户配置
vim /etc/profileexport PS1='\[\e[1;33m\]\h\[\e[m\] \t [\[\e[1;36m\]\w\[\e[m\]] [\u] ' export LANG= ...
- AngularJS的小知识点
小知识点:$scope和$rootScope (1)每次使用ngController指令,都会调用控制器的创建函数,创建出一个控制器对象. (2)每次创建一个控制器对象,AngularJS都会创建一个 ...
- mysql 事件调度
适用于mysql 5.1 及以后版本 1.查看是否开启 show variables like '%scheduler%' 2.查看进程 show processlist 3.事件调度器默认是关闭的, ...
- java基本数据类型取值范围
在JAVA中一共有八种基本数据类型,他们分别是 byte.short.int.long.float.double.char.boolean 整型 其中byte.short.int.long都是表示整数 ...
- iOS 的文字滚动条效果的实现
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(50, 100, 300, 50)]; label.text = @"欢 ...
- CrossApp入门简介
来自9miao社区的CrossApp号称可以极大的简化移动应用的开发,因为开源的时间不长,有效的资料不多. 官方网站: http://crossapp.9miao.com/ 环境搭建说明:http:/ ...
- CSS3学习笔记
1.Tranform 转换(2D,3D) 2D: div { margin:30px; width:200px; height:100px; background-color:yellow; /* R ...
- Firebird数据库的Select语句
select first 10 skip 8 * from t_data //跳过前8行不要,取10行,即取第9行到18行共10行 select first 10 * from t_data ...
- vertica在电信的应用
本文介绍了什么 ´ 电信级大数据分析典型需求 ´ Vertica数据库特点及与其他数据库对比 ´ Vertica核心技术介绍 ´ 基于Vertica的典型分系统架构简介 电信级大数据分析典型需求 ´ ...