js 格式化时间日期函数小结
下面是脚本之家为大家整理的一些格式化时间日期的函数代码,需要的朋友可以参考下。
代码如下:
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 now = new Date();
var nowStr = now.format("yyyy-MM-dd hh:mm:ss");
//使用方法2:
var testDate = new Date();
var testStr = testDate.format("YYYY年MM月dd日hh小时mm分ss秒");
alert(testStr);
//示例:
alert(new Date().Format("yyyy年MM月dd日"));
alert(new Date().Format("MM/dd/yyyy"));
alert(new Date().Format("yyyyMMdd"));
alert(new Date().Format("yyyy-MM-dd hh:mm:ss"));
js格式化当前时间为yyyy-mm-dd形式
function getNowFormatDate()
{
var day = new Date();
var Year = 0;
var Month = 0;
var Day = 0;
var CurrentDate = "";
//初始化时间
//Year= day.getYear();//有火狐下2008年显示108的bug
Year= day.getFullYear();//ie火狐下都可以
Month= day.getMonth()+1;
Day = day.getDate();
//Hour = day.getHours();
// Minute = day.getMinutes();
// Second = day.getSeconds();
CurrentDate += Year + "-";
if (Month >= 10 )
{
CurrentDate += Month + "-";
}
else
{
CurrentDate += "0" + Month + "-";
}
if (Day >= 10 )
{
CurrentDate += Day ;
}
else
{
CurrentDate += "0" + Day ;
}
return CurrentDate;
}
js 格式化时间日期函数小结的更多相关文章
- js 格式化时间日期函数小结3
function DateUtil(){}/***功能:格式化时间*示例:DateUtil.Format("yyyy/MM/dd","Thu Nov 9 20:30:37 ...
- js 格式化时间日期函数小结2
方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 ...
- mysql 常用的时间日期函数小结
本文主要是总结一些常用的在实际运用中常用的一些mysql时间日期以及转换的函数 1.now() :返回当前日期和时间 select now(); //2018-04-21 09:19:21 2.cu ...
- js 时间日期函数小结
Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month &quo ...
- js 格式化时间日期
Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month &quo ...
- JS格式化时间并比较
JS格式化时间,然后进行比较.工作遇到的情况,然后网上找到的,记下来,下次用! </head> <body> <button onclick="myFuncti ...
- SQLite中的时间日期函数(转)
SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间date()...........................产生日期tim ...
- SQLite中的时间日期函数
SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间 date()...........................产生日期 t ...
- js格式化时间的方法
方法一:用js格式化时间的方法. Date.prototype.format =function(format) { var o = { "M+" : this.getMonth( ...
随机推荐
- 面向对象ZJ
面向对象: 对象: 万事万物皆为对象. 特征, 动作或功能放在一起的就是一个对象 对象如何创建的. 在python中对象是通过类来创建的. 类是对对象的描述.统称.约束. class Car: def ...
- 【linux基础】CMake如何生成动态链接库文件
CMakeLists.txt SET(LIBRARY_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/lib) ADD_LIBRARY(filename SHARED ${CURREN ...
- leetcode227. Basic CalculatorII
这道题是只有四则运算但是没有括号的,因此可以利用stack来存储的,并且每次使得存储的值与符号有对应的关系,最后在栈中只剩下可以利用加法进行处理的数的,注意在i=n-1的时候到达最后的部分也是需要把数 ...
- php 中使用正则
1.匹配一个由一个小写字母和一位数字组成的字符串,比如”z2″ 用^[a-z][0-9]$ 2.当在一组方括号里使用^是,它表示“非”或“排除”的意思 比如要求第一个字符不能是数字:^[^0- ...
- 动手动脑-Java的继承与多态
一. class Grandparent { public Grandparent() { System.out.println("GrandParent Created."); ...
- Python字符集
字符集: 美国:ASCII 需要8bit表示 英文字母一个字节,不支持中文中国:GBK 英文字母一个字节,汉字占两个字节万国:un ...
- Manjaro启动项目及服务配置备忘
Manjaro启动项目及服务配置备忘 =============== 系统服务GUI管理搜索 systemdgenie 并安装,类似Windows的服务管理. ================ 系统启 ...
- HDU1272小希的迷宫–并查集
上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一个通道连通了 ...
- Redis 开发与运维
Getting Start 高性能 性能优势的体现 C语言实现的内存管理 epoll的I/O多路复用技术+IO连接/关闭/读写通过事件实现异步的非阻塞IO TCP协议 单线程架构,不会因为高并发对服务 ...
- linux解压缩命令之unzip,tar(持续更新)
unzip apache-tomcat-7.0.78.zip -d /usr/local/src/ 解压zip压缩包到/usr/local/src,-d就是指定解压路径的意思