下面是脚本之家为大家整理的一些格式化时间日期的函数代码,需要的朋友可以参考下。

代码如下:

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 格式化时间日期函数小结的更多相关文章

  1. js 格式化时间日期函数小结3

    function DateUtil(){}/***功能:格式化时间*示例:DateUtil.Format("yyyy/MM/dd","Thu Nov 9 20:30:37 ...

  2. js 格式化时间日期函数小结2

    方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符,  // 年(y)可以用  ...

  3. mysql 常用的时间日期函数小结

    本文主要是总结一些常用的在实际运用中常用的一些mysql时间日期以及转换的函数 1.now()  :返回当前日期和时间 select now(); //2018-04-21 09:19:21 2.cu ...

  4. js 时间日期函数小结

    Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month &quo ...

  5. js 格式化时间日期

    Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month &quo ...

  6. JS格式化时间并比较

    JS格式化时间,然后进行比较.工作遇到的情况,然后网上找到的,记下来,下次用! </head> <body> <button onclick="myFuncti ...

  7. SQLite中的时间日期函数(转)

    SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间date()...........................产生日期tim ...

  8. SQLite中的时间日期函数

    SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间 date()...........................产生日期 t ...

  9. js格式化时间的方法

    方法一:用js格式化时间的方法. Date.prototype.format =function(format) { var o = { "M+" : this.getMonth( ...

随机推荐

  1. cat命令合并多个txt文件

    cat是concatenate的缩写,意为串联,之前经常看到别人在用cat命令,没有细究 cat命令两个常用的用法是: cat file.txt能够将txt中的内容显示出来 cat file1.txt ...

  2. lesson8-图像问答-小象cv

    QA即图像问答:覆盖最全面的AI,ai完备性 动态模型:不同任务需要不同模型 or 不同细分任务需要不同模型参数 数据集: 1)VQA,显示图片+抽象场景:每个问题给10个不同答案:含有无图片答案(考 ...

  3. 洛谷 P4779 :【模板】单源最短路径(标准版)(Dijkstra+堆优化+链式前向星)

    题目背景 2018 年 7 月 19 日,某位同学在 NOI Day 1 T1 归程 一题里非常熟练地使用了一个广为人知的算法求最短路. 然后呢? 100→60: Ag→Cu: 最终,他因此没能与理想 ...

  4. CentOS右键菜单添加新增文件命令

    进入模板文件夹, 增加空白文件即可. cd /home/tom/Templatesvi text01.txt //新增一个名为text01的文本文件. :wq //保存退出. 另外可以用LibreOf ...

  5. Mybatis(四)

    参考网址如下: http://www.cnblogs.com/xdp-gacl/p/4264425.html 向大家推荐 博客园 孤傲苍狼 大哥的博客,东西写的有水平.小弟记录一下自己的学习历程,自勉 ...

  6. Example [mybatis] 的用法

    example.or() .andField1EqualTo() .andField2IsNull(); example.or() .andField3NotEqualTo() .andField4I ...

  7. LeetCode - Rotate String

    We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost ...

  8. WebSocket-Over-HTTP Protocol

    转自:https://pushpin.org/docs/protocols/websocket-over-http/ WebSocket-Over-HTTP Protocol The WebSocke ...

  9. sqler sql 转rest api 的工具试用

    sqler 从开源很快就获取了1k的star,使用起来很方便,而且也很灵活,支持的数据库也比较多. 支持的功能 无需依赖,可独立使用: 支持多种数据可类型,包括:SQL Server, MYSQL, ...

  10. JavaScript编码风格

    最近在看前端大牛Nicbolas C.Zakas的<编写可维护的JavaScript代码>一书.觉得里面的很多知识点都写的很好,所以,就写篇博文,总结一下吧!编码规范对于程序设计来说是很重 ...