JavaScript 时间格式
方法1:
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + , //月份
"d+": this.getDate(), //日
"h+": this.getHours()%==?:this.getHours()%, //小时
"H+": this.getHours(),
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + ) / ), //季度
"f": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$, (this.getFullYear() + "").substr( - RegExp.$.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$, (RegExp.$.length == ) ? (o[k]) : (("" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
var time1 = new Date().Format("yyyy-MM-dd");
var time2 = new Date().Format("yyyy-MM-dd hh:mm:ss");
var time3 = new Date().Format("yyyy-MM-dd hh:mm:ss:f");
var time4=new Date().toLocaleString();
console.log(time1);
console.log(time2);
console.log(time3);
console.log(time4);
方法2:
Date.prototype.format = function(formatStr)
{
var str = formatStr;
var Week = ['日','一','二','三','四','五','六']; str=str.replace(/yyyy|YYYY/,this.getFullYear());
str=str.replace(/yy|YY/,(this.getYear() % )>?(this.getYear() % ).toString():'' + (this.getYear() % ));
//
this.setMonth(this.getMonth()+);
str=str.replace(/MM/,this.getMonth()>?this.getMonth().toString():'' + this.getMonth());
str=str.replace(/M/g,this.getMonth()); str=str.replace(/w|W/g,Week[this.getDay()]); str=str.replace(/dd|DD/,this.getDate()>?this.getDate().toString():'' + this.getDate());
str=str.replace(/d|D/g,this.getDate()); str=str.replace(/hh|HH/,this.getHours()>?this.getHours().toString():'' + this.getHours());
str=str.replace(/h|H/g,this.getHours());
str=str.replace(/mm/,this.getMinutes()>?this.getMinutes().toString():'' + this.getMinutes());
str=str.replace(/m/g,this.getMinutes()); str=str.replace(/ss|SS/,this.getSeconds()>?this.getSeconds().toString():'' + this.getSeconds());
str=str.replace(/s|S/g,this.getSeconds()); str=str.replace(/f/,this.getMilliseconds()); return str;
}
跟上面的一样调用
但是小时没有12小时制的了
http://www.cnblogs.com/zhangpengshou/archive/2012/07/19/2599053.html
JavaScript 时间格式的更多相关文章
- javascript 时间格式(很方便的原生函数)
原文:http://www.cnblogs.com/yjf512/p/3796229.html --------------------- <html xmlns="http://ww ...
- javascript时间格式转换(今天,昨天,前天)
function transDate() { var $time =document.getElementById("share-time"); var date = $time. ...
- JS时间格式 GMT格式转换
JavaScript时间格式转换总结 1.当前系统区域设置格式(toLocaleDateString和toLocaleTimeString) 例子:(new Date()).toLocaleDateS ...
- 时间操作(JavaScript版)—最简单比較两个时间格式数据的大小
呵呵呵,在软件研发过程中假设遇到要比較两个时间的大小.你会怎么做.嗯嗯嗯,非常直观的做法就是把"-"去掉,再比較大小,真的有必要吗?看以下最简单的时间比較方式: <!DOCT ...
- JavaScript验证时间格式
1. 短时间,形如 (13:04:06) function isTime(str) { var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/) ...
- 关于javascript中时间格式和时间戳的转换
当前时间获取的各种函数: var myDate = new Date();myDate.getYear(); //获取当前年份(2位),已经不推荐使用myDate.getFullYear ...
- Newtonsoft.Json 序列化和反序列化 时间格式
From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...
- javascript 时间操作
javascript时间函数 javascript提供了Date对象来进行时间和日期的计算.Date对象有多种构造函数: 1.dateObj=new Date() //当前时间 2.dateObj=n ...
- 一起Polyfill系列:让Date识别ISO 8601日期时间格式
一.什么是ISO 8601日期时间格式 ISO 8601是国际标准化组织制定的日期时间表示规范,全称是<数据存储和交换形式·信息交换·日期和时间的表示方法>. 示例: 1. 2014-12 ...
随机推荐
- hammerjs wabapp h5 触屏手势一网打尽
hammerjs官网 http://hammerjs.github.io/ 学习文章1 http://www.cnblogs.com/vajoy/p/4011723.html 学习文章2 htt ...
- CentOS代理设置
1.全局的代理设置: vi /etc/profile 添加下面内容 http_proxy = http://username:password@yourproxy:8080/ftp_proxy = h ...
- MYSQL中的CASE WHEN END AS
SELECT id,`NAME`,province,city, phone, CASE sex WHEN 'M' THEN '男' WHEN 'F' THEN '女'END AS sexFROM `p ...
- sql server 2005 使用Log Explorer查看和恢复数据
使用Log Explorer查看和恢复数据 Log Explorer 4.1.可用于SQL Server2005的日志查看工具 下载地址: http://download.csdn.net/ ...
- 使用gradle构建多模块springboot项目,打jar包
官方文档: https://spring.io/guides/gs/rest-service/ 参考:http://blog.csdn.net/u013360850/article/details/ ...
- [py][mx]django课程页显示city和机构封面图
city和课程机构信息展示到前台去 organization/views.py from django.views.generic.base import View from organization ...
- JMS规范与Kafka
一.为什么需要消息队列 消息队列的核心作用就是三点:解耦一个系统中各个子模块的互相绑定与依赖,异步执行后台耗时逻辑,并行处理一个请求中涉及的多个操作. 以我们常见的下订单场景来说明,我们熟悉的淘宝,后 ...
- 安装selenium python
https://pypi.org/project/selenium/#files selenium-3.13.0-py2.py3-none-any.whl 安装成功后才能用 from selenium ...
- 第三章基本的SQl查询语言
---恢复内容开始--- ---恢复内容结束---
- sql server 获取分隔字符串后的长度
--方法1 --sql 分隔字符串,返回个数 CREATE function f_splitLen_1 ( @str varchar(1024), --要分割的字符串 @split varc ...