Home > Web Front-end > JS Tutorial > Summary of javascript date formatting methods_javascript skills

Summary of javascript date formatting methods_javascript skills

WBOY
Release: 2016-05-16 15:25:07
Original
1315 people have browsed it

本文实例总结了javascript日期格式化方法。分享给大家供大家参考,具体如下:

采用Prototype:

Date.prototype.Format = function (fmt) { //author: meizz
  var o = {
    "M+": this.getMonth() + 1, //月份
    "d+": this.getDate(), //日
    "h+": this.getHours(), //小时
    "m+": this.getMinutes(), //分
    "s+": this.getSeconds(), //秒
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度
    "S": this.getMilliseconds() //毫秒
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
  if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}
Copy after login

把这段代码放在<script></script>里面。

然后调用时采用如下语句即可:

复制代码 代码如下:
new Date().Format("yyyy-MM-dd hh:mm:ss")

具体可参考前面一篇《javascript中Date format(js日期格式化)方法小结

另外获取当前日期的函数如下:

<SCRIPT LANGUAGE="JavaScript">
<!--
var myDate = new Date();
  myDate.getYear();    //获取当前年份(2位)
  myDate.getFullYear();  //获取完整的年份(4位,1970-&#63;&#63;&#63;&#63;)
  myDate.getMonth();   //获取当前月份(0-11,0代表1月)
  myDate.getDate();    //获取当前日(1-31)
  myDate.getDay();    //获取当前星期X(0-6,0代表星期天)
  myDate.getTime();    //获取当前时间(从1970.1.1开始的毫秒数)
  myDate.getHours();   //获取当前小时数(0-23)
  myDate.getMinutes();  //获取当前分钟数(0-59)
  myDate.getSeconds();  //获取当前秒数(0-59)
  myDate.getMilliseconds();  //获取当前毫秒数(0-999)
  myDate.toLocaleDateString();  //获取当前日期
  var mytime=myDate.toLocaleTimeString();  //获取当前时间
  myDate.toLocaleString( );    //获取日期与时间
if (mytime<"23:30:00")
{
alert(mytime);
}
//-->
</SCRIPT>
Copy after login

希望本文所述对大家JavaScript程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template