JS-日期和时间
# 格式化日期和时间
扩展 Date:
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
}
}
return format;
}
测试:
var testDate = new Date();
//yyyy-MM-dd hh:mm:ss
var testDateStr = now.format("yyyy-MM-dd hh:mm:ss");
console.log(testDateStr);
//YYYY年MM月dd日hh小时mm分ss秒
var testDateStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
console.log(testDateStr);
console.log(new Date().Format("yyyy年MM月dd日"));
console.log(new Date().Format("MM/dd/yyyy"));
console.log(new Date().Format("yyyyMMdd"));
console.log(new Date().Format("yyyy-MM-dd hh:mm:ss"));
JS-日期和时间的更多相关文章
- js 日期,时间函数 及相关运算大全
一.在js中如何比较两个时间字符串的大小 方法一: function CompareDate(d1,d2){ return ((new Date(d1.replace(/-/g,"/ ...
- js - 日期、时间 Date对象方法
Date 是 JS 内置的日期构造函数 var d = new Date(); // 这个是系统当前时间的日期实例 d.getYear(); // 返回 d 实例年份 - 1900 d.getFul ...
- JS日期、时间 格式化转换方法
Date.prototype.format = function(fmt) { var o = { "M+" : this.getMonth()+1, //月份 "d+& ...
- 推荐几款开源的js日期控件
做为一个正规的网站,经常需要一些日期或时间的筛选,所以我们今天就推荐二十多款javascript的js日期/时间筛选插件.个个经典,绝对有你需要的. My97DatePicker ,国人开发的一款js ...
- Js 日期转换函数(UTC时间转换及日期想加减)
IOS上Js日期转换中new Date("yyyy-mm-dd")不能正常工作,必须使用new Date("yyyy/MM/dd"); 日期相加减: Date. ...
- 移动端lCalendar纯原生js日期时间选择器
网上找过很多的移动端基于zepto或jquery的日期选择器,在实际产品中也用过一两种,觉得都不太尽如人意,后来果断选择了H5自己的日期input表单,觉得还可以,至少不用引用第三方插件了,性能也不错 ...
- JS Date当前时间:获取日期时间方法在各浏览器中的差异
转自:http://www.feiesoft.com/00047/<script type="text/javascript"> // JS Date当前时间获取方法在 ...
- JS获取当前日期时间及JS日期格式化
Js获取当前日期时间: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份( ...
- js日期时间比较函数
转自:http://www.cnblogs.com/zxjyuan/archive/2010/09/07/1820708.html js日期比较(yyyy-mm-dd) function duibi( ...
- js中的时间转换—毫秒转换成日期时间
转自:http://www.javascript100.com/?p=181 前几天,在项目中遇到js时间增加问题,要将js毫秒时间转换成日期时间 var oldTime = (new Date(&q ...
随机推荐
- 网络命令-nc(二)
继续Netcat 这个命令吧 1:远程拷贝文件 在本地输出 文件debian.img 到 192.168.5.40 主机12345端口监听 nc -v 192.168.5.40 12345 < ...
- Modify PDF operators.
1 Depart Process: 2 1. Grep xref and trailer binary position in file. 3 2. Dump xref table and trail ...
- USACO 6.3 章节 你对搜索和剪枝一无所知QAQ
emmm........很久很久以前 把6.2过了 所以emmmmmm 直接跳过 ,从6.1到6.3吧 Fence Rails 题目大意 N<=50个数A1,A2... 1023个数,每个数数值 ...
- POJ 1410 Intersection (计算几何)
题目链接:POJ 1410 Description You are to write a program that has to decide whether a given line segment ...
- Cocos2d-x之定时器
| 版权声明:本文为博主原创文章,未经博主允许不得转载. 每一个游戏程序都有一个循环在不断运行,它是由导演对象来管理与维护.如果需要场景中的精灵运动起来,可以在游戏循环中使用定时器对精灵等对象进行 ...
- 洛谷 P1972 [SDOI2009]HH的项链——树状数组
先上一波题目 https://www.luogu.org/problem/P1972 这道题是询问区间内不同数的个数 明显不是正常的数据结构能够维护的 首先考虑 因为对于若干个询问的区间[l,r],如 ...
- log4j基础配置使用
添加log4j的jar包:可以从maven处下载:https://mvnrepository.com/artifact/log4j/log4j/1.2.17 <!-- https://mvnre ...
- js数据处理-----数据拷贝
一.理解深拷贝与浅拷贝 如下代码,把 a 的值赋给 b ,修改 b 的值会直接修改到 a 的值,这叫浅拷贝.(其实他们修改的是同一个对象) var a = [1,2,3,4,5]; var b ...
- MVC5使用SignalR进行双向通信 (1)
@a604572782 2015-08-10 09:01 字数 2133 阅读 1245 MVC5使用SignalR进行双向通信 (1) 配置SignalR 在NuGet中通过 install-pac ...
- elasticsearch Mapping使用自定义分词器
创建索引及配置分析器 PUT /my_index { "settings": { "analysis": { "char_filter": ...