JS Date.Format

原文:JS Date.Format

// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
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;
}  

 

时间: 2024-09-20 12:31:21

JS Date.Format的相关文章

javascript中Date format(js日期格式化)方法小结_javascript技巧

本文实例总结了javascript中日期格式化的方法.分享给大家供大家参考,具体如下: 方法一: // 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字) // 例子: // (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 200

原生JS:Date对象全面解析_javascript技巧

Date对象:基于1970年1月1日(世界标准时间)起的毫秒数 本文参考MDN做的详细整理,方便大家参考MDN 构造函数: 1.new Date(); 依据系统设置的当前时间来创建一个Date对象. 2.new Date(value); value代表自1970年1月1日00:00:00 (世界标准时间) 起经过的毫秒数. 3.new Date(dateString); dateString表示日期的字符串值.该字符串应该能被 Date.parse() 方法识别(符合 IETF-complian

js date:JS Date 转换

js Date(日期)型字符串,要想正确的转换为Date(日期)对象,必须用new Date(str)方式.很多情况下,我们获取到了一大串诸如:Sun Jan 2 00:00:00 UTC+0800 2000 的JS日期,当我们想将其转换成字符串或者其他类型时,一开始我真的不知所措.后来才发现,原来就那么简单,先将日期,new Date(日期)一下,再做转换或者计算. 本文链接http://www.cxybl.com/html/wyzz/JavaScript_Ajax/20130620/3873

Js Date泣血整理

原文:Js Date泣血整理JS Date 对象用于处理日期和时间. 创建 Date 对象的语法: var myDate=new Date() Date 对象会自动把当前日期和时间保存为其初始值. 参数形式有以下5种:   new Date("month dd,yyyy hh:mm:ss"); new Date("month dd,yyyy"); new Date(yyyy,mth,dd,hh,mm,ss); new Date(yyyy,mth,dd); new D

Mysql 日期时间 DATE_FORMAT(date,format)_Mysql

本文转自:http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html#function_date-format DATE_FORMAT(date,format) Formats the date value according to the format string. The following specifiers may be used in the format string. As of MySQL 3.23,

JS Date函数整理方便使用_基础知识

JS Date 对象用于处理日期和时间. 创建 Date 对象的语法: var myDate=new Date() Date 对象会自动把当前日期和时间保存为其初始值. 参数形式有以下5种: 复制代码 代码如下: new Date("month dd,yyyy hh:mm:ss"); new Date("month dd,yyyy"); new Date(yyyy,mth,dd,hh,mm,ss); new Date(yyyy,mth,dd); new Date(m

扩展JS Date对象时间格式化功能的小例子_javascript技巧

在自己JS代码中引入一下代码: 复制代码 代码如下: Date.prototype.format =function(format){    var o = {            "M+" : this.getMonth()+1, //month            "d+" : this.getDate(), //day            "h+" : this.getHours(), //hour            "

js Date对象的扩展函数

日期 http://momentjs.com/ Java代码   <script type="text/javascript">   Date.prototype.format = function(format) {       var o = {           "M+": this.getMonth() + 1,           //month           "d+": this.getDate(),       

js Date自定义函数 延迟脚本执行_javascript技巧

复制代码 代码如下: function delay(numberMillis){ var now = new Date(); var exitTime = now.getTime()+numberMillis; while(true){ now = new Date(); if(now.getTime() > exitTime) return; } } document.write("<script language='javascript' src='js1.js'><